Skip to content

Instantly share code, notes, and snippets.

@deepak7093
Created October 2, 2022 06:13
Show Gist options
  • Select an option

  • Save deepak7093/6d9f9502c4ed6bb0e207c5a59c222ae0 to your computer and use it in GitHub Desktop.

Select an option

Save deepak7093/6d9f9502c4ed6bb0e207c5a59c222ae0 to your computer and use it in GitHub Desktop.

Revisions

  1. deepak7093 created this gist Oct 2, 2022.
    38 changes: 38 additions & 0 deletions aws-cloud-front-invalidatie.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    import json
    import boto3

    code_pipeline = boto3.client("codepipeline")
    cloud_front = boto3.client("cloudfront")

    def lambda_handler(event, context):
    job_id = event["CodePipeline.job"]["id"]
    try:
    user_params = json.loads(
    event["CodePipeline.job"]
    ["data"]
    ["actionConfiguration"]
    ["configuration"]
    ["UserParameters"]
    )
    cloud_front.create_invalidation(
    DistributionId=user_params["distributionId"],
    InvalidationBatch={
    "Paths": {
    "Quantity": len(user_params["objectPaths"]),
    "Items": user_params["objectPaths"],
    },
    "CallerReference": event["CodePipeline.job"]["id"],
    },
    )
    except Exception as 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,
    )