I've multiple Cloudfront distributions pointing to a single S3 Bucket to create different URLs. Now when I deploy it is difficult to clear cache of all the buckets manually one by one. So I thought there should be an option from where I can find all the ids and clear the cache but all I could find was
aws cloudfront get-distribution-config
--id <value>
[--cli-input-json <value>]
[--generate-cli-skeleton <value>]
Where id takes the id of cloudfront distribution itself which I want to find out.
I can't use this as well as I don't want to clear cache of all the distributions
aws cloudfront list-distributions
[--max-items <value>]
[--cli-input-json <value>]
[--starting-token <value>]
[--page-size <value>]
[--generate-cli-skeleton <value>]
I'm trying to find something like but so far this doesn't seem to be the right approach
aws cloudfront --origing <value>
https://stackoverflow.com/a/64264887/5773416
While this solution is not exactly what I wanted it helped me to find the exact answer. I'm posting my answer which finally helped me achieve this.
aws cloudfront list-distributions --query "DistributionList.Items[*].{id:Id,origin:Origins.Items[0].Id}[?origin=='S3-BUCKET_NAME'].id" --output text
Which will give a result like this
EXXXXXXXXXXX1 EXXXXXXXXXXX2
and in order to clear the cache of multiple distributions
for id in $(aws cloudfront list-distributions --query "DistributionList.Items[*].{id:Id,origin:Origins.Items[0].Id}[?origin=='S3-BUCKET_NAME'].id" --output text);do aws cloudfront create-invalidation --distribution-id $id --paths "/*";done;
You can use the query argument to fetch only Ids
aws cloudfront list-distributions --query "DistributionList.Items[*].Origins.Items[*].Id" --output text
S3-test1.example.com
S3-Website-test2.example.com.s3-website-us-west-1.amazonaws.com
then you can filter the list using grep
aws cloudfront list-distributions --query "DistributionList.Items[*].Origins.Items[*].Id" --output text | grep test2
S3-test1.example.com
You can return multiple values using query argument
aws cloudfront list-distributions --query "DistributionList.Items[*].Origins.Items[*].{id:Id,name:DomainName}" --output text
S3-test1.example.com test1.example.com.s3.amazonaws.com
S3-Website-test2.example.com.s3-website-us-west-1.amazonaws.com test2.example.com.s3-website-us-west-1.amazonaws.com
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With