Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the JSON for batch invalidation of a Cloudfront distribution look like?

I'm trying to invalidate a single file on a AWS Cloudfront distribution using the (as of now) experimental aws cloudfront CLI tool. I cannot figure out how to format the JSON it expects for passing to the --invalidation-batch parameter.

The only documentation I found only explains the XML it expects, yet I am having a hard time translating that into JSON: http://docs.aws.amazon.com/AmazonCloudFront/latest/APIReference/CreateInvalidation.html

I tried:

{
   "Paths" : {
       "Quantity" : 1,
       "Items" : ["/foobar.js"]
   },
   "CallerReference" : "foo-bar-baz"
}

Has anyone yet used this and figured out to format the JSON?

like image 986
m90 Avatar asked Mar 06 '15 08:03

m90


People also ask

What is an invalidation in CloudFront?

PDFRSS. If you need to remove a file from CloudFront edge caches before it expires, you can do one of the following: Invalidate the file from edge caches. The next time a viewer requests the file, CloudFront returns to the origin to fetch the latest version of the file.

How long does an invalidation take CloudFront?

Object invalidations typically take from 10 to 100 seconds to complete. You can check the status of an invalidation by viewing your distribution from the CloudFront console.


2 Answers

You can generate sample JSON using the following command.

$ aws cloudfront create-invalidation --generate-cli-skeleton | vi -

Output:

{
"DistributionId": "",
"InvalidationBatch": {
    "Paths": {
        "Quantity": 0,
        "Items": [
            ""
        ]
    },
    "CallerReference": ""
}
}
like image 185
imperalix Avatar answered Oct 05 '22 11:10

imperalix


Here's a complete, working example of doing it from a Bash script, including handling the need to create a unique id for each invalidation (unique enough here for my purposes):

INVALIDATION_ID=$(date +"%S")
INVALIDATION_JSON="{
    \"DistributionId\": \"YOUR_ID\",
    \"InvalidationBatch\": {
        \"Paths\": {
            \"Quantity\": 2,
            \"Items\": [
                \"/foo.png\",
                \"/bar.jpg\"
            ]
        },
        \"CallerReference\": \"$INVALIDATION_ID\"
    }
}"

aws cloudfront create-invalidation --cli-input-json "$INVALIDATION_JSON"
like image 25
Clay Fowler Avatar answered Oct 05 '22 13:10

Clay Fowler