Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
AWS Lambda function of Deployment of static website to s3
import boto3
import zipfile
from botocore.client import Config
import io
import mimetypes
def lambda_handler(event, context):
sns = boto3.client('sns')
location = {
"bucketName": 'portfoliobuild.urzhumtsev.me',
"objectKey": 'portfoliobuild.zip'
}
try:
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))
s3 = boto3.resource('s3', config=Config(signature_version='s3v4'))
portfolio_bucket = s3.Bucket('portfolio.urzhumtsev.me')
bucket = s3.Bucket(location["bucketName"])
zip_obj = bucket.Object(location["objectKey"])
buffer = io.BytesIO(zip_obj.get()["Body"].read())
with zipfile.ZipFile(buffer) 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')
sns.publish(TopicArn="arn:aws:sns:...", Message="Portfolio deployed")
if job:
codepipeline = boto3.client('codepipeline')
codepipeline.put_job_success_result(jobId=job["id"])
except:
sns.publish(TopicArn="arn:aws:sns:...", Message="FAILURE: The Portfolio was not deployed")
return 'Done'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.