forked from sophiaciocca/react-portfolio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload-lambda.py
49 lines (37 loc) · 1.66 KB
/
upload-lambda.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import boto3
from botocore.client import Config
import io
import zipfile
import mimetypes
def lambda_handler(event, context):
sns = boto3.resource('sns')
topic = sns.Topic('arn:aws:sns:ap-southeast-1:202433929926:DeployPortfolio')
location = {
"bucketName": 'reactbuild.patrikmolnar.com',
"objectKey": 'reactbuild.zip'
}
try:
job = event.get("CodePipeline.job")
if job:
for artifact in job["data"]["inputArtifacts"]:
if artifact["name"] == "MyAppBuild":
location = artifact["location"]["s3Location"]
s3 = boto3.resource('s3')
portfolio_bucket = s3.Bucket('react.patrikmolnar.com')
build_bucket = s3.Bucket(location["bucketName"])
portfolio_zip = io.BytesIO()
build_bucket.download_fileobj(location["objectKey"], portfolio_zip)
with zipfile.ZipFile(portfolio_zip) as myzip:
for nm in myzip.namelist():
obj = myzip.open(nm)
portfolio_bucket.upload_fileobj(obj, nm,
ExtraArgs={'ContentType': mimetypes.guess_type(nm)[0]})
portfolio_bucket.Object(nm).Acl().put(ACL='public-read')
topic.publish(Subject="Project", Message="Project deployed successfully!")
if job:
codepipeline = boto3.client('codepipeline')
codepipeline.put_job_success_result(jobId=job["id"])
except:
topic.publish(Subject="Portfolio Deploy Failed", Message="There was an error while trying to deploy the project.")
raise
return 'Deploy Pass!'