Elnur Mammadov
Elnur Mammadov

Reputation: 162

AWS Cloudfront distribution ID by tag

is that possible to get aws cloudfront dist id by tag via awscli or aws sdk for powershell. I could only get only ID by ARN number of resource

aws cloudfront list-tags-for-resource --resource XXX
{
   "Tags": {
       "Items": [
           {
               "Value": "TEST_APP",
               "Key": "CLIENT_APP"
           }
       ]
   }
}

UPDATE

Solved via

cloudfrontdistids=$(aws cloudfront list-distributions | jq -r ".DistributionList.Items[].ARN")
for dist in $cloudfrontdistids
do
        if [ $(aws cloudfront list-tags-for-resource --resource $dist | jq -r ".Tags.Items[].Value") == $VALUE ]
        then
                CLOUDFRONT_DISTRIBUTION_ID=${dist:(-14)}
        fi
done

Upvotes: 7

Views: 2474

Answers (1)

Tolis Gerodimos
Tolis Gerodimos

Reputation: 4400

The answer provided by the original poster seems to be almost correct. But the CLOUDFRONT_DISTRIBUTION_ID is not always 14 characters, so in order to avoid pulling aditional characters use this instead:

CLOUDFRONT_DISTRIBUTION_ID=${dist##*/}

In bash, it removes a prefix pattern. Here, it's basically giving you everything after the last path separator /, by greedily removing the prefix */

Upvotes: 3

Related Questions