Search code examples
amazon-web-servicesamazon-s3amazon-cloudfrontaws-cli

Is it possible to find cloudfront distributions from Origin via AWS CLI?


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>

Solution

  • 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;