Created
January 14, 2020 16:15
-
-
Save Urzhumtsev1/da202e669d2bfaf99ee43d55b4257a1b to your computer and use it in GitHub Desktop.
AWS Lambda function of Deployment of static website to s3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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