Skip to content

Instantly share code, notes, and snippets.

@maguzzi
Created January 1, 2024 19:13
Show Gist options
  • Save maguzzi/cc8eace18a5f0931bf317f54ab6f5dae to your computer and use it in GitHub Desktop.
Save maguzzi/cc8eace18a5f0931bf317f54ab6f5dae to your computer and use it in GitHub Desktop.
Cloudfront cache invalidation lambda
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