-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupload-schlaetzer-lambda.py
More file actions
67 lines (51 loc) · 2.09 KB
/
upload-schlaetzer-lambda.py
File metadata and controls
67 lines (51 loc) · 2.09 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env python
# coding: utf-8
import json
import boto3
import zipfile
import io
import mimetypes
def lambda_handler(event, context):
# Topic for SNS
sns = boto3.resource('sns')
topic = sns.Topic('arn:aws:sns:eu-central-1:948929446452:deploySchlaetzerWebsite')
location={
"bucketName": "build.schlaetzer.com",
"objectKey": "buildschlaetzer.zip"
}
try:
# get the build job
job = event.get("CodePipeline.job")
if job:
for artifact in job["data"]["inputArtifacts"]:
if artifact["name"] == "BuildArtifact":
location = artifact["location"]["s3Location"]
print("Building portfolio from "+str(location))
# define s3
s3 = boto3.resource('s3')
# Buckets
build_bucket = s3.Bucket(location["bucketName"])
schlaetzer_bucket = s3.Bucket('schlaetzer.com')
# download zip from s3
build_zip = io.BytesIO()
build_bucket.download_fileobj(location["objectKey"], build_zip)
# upload each of the files to the other s3
with zipfile.ZipFile(build_zip) as myzip:
for nm in myzip.namelist():
obj = myzip.open(nm)
schlaetzer_bucket.upload_fileobj(obj, nm,
ExtraArgs={'ContentType': mimetypes.guess_type(nm)[0]})
schlaetzer_bucket.Object(nm).Acl().put(ACL='public-read')
# tell codepipeline that we have been successful with deploying
if job:
print("sending pipeline info: "+job["id"])
codepipeline = boto3.client('codepipeline')
codepipeline.put_job_success_result(jobId=job["id"])
except:
topic.publish(Subject='Error when deploying Schlaetzer.com', Message='Website couldn\'t be successfully deployed...')
raise
topic.publish(Subject='Schlaetzer.com has been deployed', Message='Website has been successfully deployed')
return {
'statusCode': 200,
'body': json.dumps('worked')
}