Skip to content

Instantly share code, notes, and snippets.

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": '',
"objectKey": ''
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('')
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 =
portfolio_bucket.upload_fileobj(obj, nm,
ExtraArgs={'ContentType': mimetypes.guess_type(nm)[0]}
sns.publish(TopicArn="arn:aws:sns:...", Message="Portfolio deployed")
if job:
codepipeline = boto3.client('codepipeline')
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