-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupload-portfolio-lambda.py
More file actions
52 lines (39 loc) · 1.77 KB
/
upload-portfolio-lambda.py
File metadata and controls
52 lines (39 loc) · 1.77 KB
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
50
51
52
import boto3
from botocore.client import Config
import StringIO
import zipfile
import mimetypes
def lambda_handler(event, context):
sns = boto3.resource('sns')
topic = sns.Topic('arn:aws:sns:us-east-1:378091754354:depoy_portfolio_topic')
location = {
"bucketName": 'build.chrisdodds.info',
"objectKey": 'portfolio_build.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', config=Config(signature_version='s3v4'))
site_bucket = s3.Bucket('chrisdodds.info')
build_bucket = s3.Bucket(location["bucketName"])
site_zip = StringIO.StringIO()
build_bucket.download_fileobj(location["objectKey"], site_zip)
with zipfile.ZipFile(site_zip) as myzip:
for nm in myzip.namelist():
obj = myzip.open(nm)
site_bucket.upload_fileobj(obj, nm,
ExtraArgs={'ContentType': mimetypes.guess_type(nm)[0]})
site_bucket.Object(nm).Acl().put(ACL='public-read')
print "Job Done!"
topic.publish(Subject='Portfolio Deployed', Message='Portfolio site 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 site deployment failed.')
raise
return 'Job Done'