Created
January 1, 2024 19:13
-
-
Save maguzzi/cc8eace18a5f0931bf317f54ab6f5dae to your computer and use it in GitHub Desktop.
Cloudfront cache invalidation lambda
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 os | |
code_pipeline = boto3.client("codepipeline") | |
cloud_front = boto3.client("cloudfront") | |
def lambda_handler(event, context): | |
print(event) | |
job_id = event["CodePipeline.job"]["id"] | |
try: | |
print("distribution id : "+os.environ.get("CLOUDFRONT_DISTRIBUTION_ID")) | |
cloud_front.create_invalidation( | |
DistributionId=os.environ.get("CLOUDFRONT_DISTRIBUTION_ID"), | |
InvalidationBatch={ | |
"Paths": { | |
"Quantity": 1, | |
"Items": ["/*"], | |
}, | |
"CallerReference": event["CodePipeline.job"]["id"], | |
}, | |
) | |
except Exception as e: | |
print("error: "+str(e)) | |
code_pipeline.put_job_failure_result( | |
jobId=job_id, | |
failureDetails={ | |
"type": "JobFailed", | |
"message": str(e), | |
}, | |
) | |
else: | |
code_pipeline.put_job_success_result( | |
jobId=job_id, | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment