neutreno
neutreno

Reputation: 743

AWS CLI CloudFront Invalidate All Files

I am attempting to invalidate an entire static website. The following command does not seem to invalidate /index.html and gives an odd output of items to be invalided, as shown below. Is this AWS CLI behaviour normal or am I missing something? Thanks!

aws cloudfront create-invalidation --distribution-id $DISTRIBUTION_ID --paths /*

Output:

{
    "Invalidation": {
    "Status": "InProgress", 
    "InvalidationBatch": {
        "Paths": {
            "Items": [
                "/lib32", 
                "/home", 
                "/vmlinuz", 
                "/core", 
                "/proc", 
                "/var", 
                "/dev", 
                "/usr", 
                "/etc", 
                "/initrd.img", 
                "/cdrom", 
                "/lost+found", 
                "/root", 
                "/tmp", 
                "/lib", 
                "/dead.letter", 
                "/lib64", 
                "/boot", 
                "/sys", 
                "/run", 
                "/bin", 
                "/sbin", 
                "/mnt", 
                "/opt", 
                "/snap", 
                "/media", 
                "/copyright", 
                "/srv"
            ], 
            "Quantity": 28
        }, 

Upvotes: 68

Views: 42185

Answers (6)

Michael - sqlbot
Michael - sqlbot

Reputation: 179114

That's your shell doing expansion of local filenames.

That's what you're essentially asking for since the * isn't quoted.

Either --paths '*' or Specifying --paths '/*'¹ will do what you intend. Quoting the wildcard keeps it as a literal string rather than what you're seeing.

The complete command is:

aws cloudfront create-invalidation --distribution-id $DISTRIBUTION_ID --paths '/*'


¹The CloudFront console allows you to specify either * or /* to invalidate the entire distribution; by contrast, the CLI expects /*. This, in turn, is because the underlying API also expects /*. When you use * in the console, the leading slash is silently added by the console before the console makes the request to the CloudFront API.

Upvotes: 89

Eloy Ruiz
Eloy Ruiz

Reputation: 767

If you are executing a script from your package.json like the one I followed in this tutorial remove the path enclosing quotes. Here's an example usign (and correcting) the one in the tutorial.

"scripts": {
  "deploy:live": "npm run build && aws s3 sync dist/ s3://<YOUR_S3_BUCKET_NAME> --delete && npm run-script invalidate-cache:live",
  "invalidate-cache:live": "aws configure set preview.cloudfront true && aws cloudfront create-invalidation --distribution-id <CLOUDFRONT_DISTRIBUTION_ID> --paths /*"
}

Upvotes: 0

David Borges
David Borges

Reputation: 76

In my case, surprisingly, quoting the wildcard didn't work. To solve this, I had to temporarily disable globbing, create the invalidation and then reenable globbing with:

set -f
aws cloudfront create-invalidation --distribution-id $DISTRIBUTION_ID --paths "/*"
set +f

This shouldn't be your first solution. Use it just in case nothing else works.

Upvotes: 1

Given the above answers, you can use this one command:

aws cloudfront create-invalidation --distribution-id $(aws cloudfront list-distributions --query 'DistributionList.Items[*].Id | [0]' | tr -d '"') --paths "/*"

This basically takes the first CloudFront Distribution in your environment, retrieves the ID, removes the double quotes, and requests the invalidation.

You should see a response similar to:

{
"Location": "https://cloudfront.amazonaws.com/2020-05-31/distribution/E8D4M8HG5JSRS/invalidation/I87QDOK5CWC6O4KWOWBZX75EWN",
"Invalidation": {
    "Id": "I87QDOK5CWC6O4KWOWBZX75EWN",
    "Status": "InProgress",
    "CreateTime": "2023-03-15T00:21:40.285000+00:00",
    "InvalidationBatch": {
        "Paths": {
            "Quantity": 1,
            "Items": [
                "/*"
            ]
        },
        "CallerReference": "cli-1678839700-773660"
    }
}

}

Upvotes: 3

Carlos Roberto
Carlos Roberto

Reputation: 131

Maybe on windows (using cmd) you can use the path without quotes, but on bash environment (linux, mac) the character * it's a special char. You need to pass the path inside quotes to work cross-platform:

aws cloudfront create-invalidation --distribution-id $DISTRIBUTION_ID --paths '/*'

Upvotes: 13

Vladyslav Didenko
Vladyslav Didenko

Reputation: 1641

Example of invalidation of cloudfront distribution via aws cli :

aws cloudfront create-invalidation --distribution-id <DistributionID> --paths "/*"

Example :

aws cloudfront create-invalidation --distribution-id E1B1A4GHK9TTE --paths "/*"

To list or get cloudfront distribution id you can use console or via cli :

aws cloudfront list-distributions 
aws cloudfront list-distributions | grep Id

Upvotes: 55

Related Questions