-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload-portfolio-lambda.py
49 lines (38 loc) · 1.65 KB
/
upload-portfolio-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
import io
import zipfile
import mimetypes
def lambda_handler(event, context):
sns = boto3.resource('sns')
topic = sns.Topic('arn:aws:sns:us-east-1:917917241244:deployPortfolioTopic')
location = {
"bucketName": 'portfoliobuild.cloudwalkerdigital.com',
"objectKey": 'portfoliobuild.zip'
}
try:
job = event.get("CodePipeline.job")
if job:
for artifact in job["data"]["inputArtifacts"]:
if artifact["name"] == "MyAppBuild":
location = artifact["location"]["s3Location"]
print ("Building portfolio from " + str(location))
s3 = boto3.resource('s3')
portfolio_bucket = s3.Bucket('portfolio.cloudwalkerdigital.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')
print ('Job Done!')
topic.publish(Subject="Portfolio Deployed", Message="Portfolio deployed successfully!")
if job:
codepipeline = boto3.client('codepipeline')
codepipeline.put_job_success_result(jobId=job["id"])
except:
topic.publish(Subject="Portfolio Deploy Failed", Message="The Portfolio was not deployed successfully!")
raise
return 'Hello from Lambda'