AWS CLI to list CloudFront distributions

Using AWS CLI command to list AWS CloudFront distributions and invalidating CloudFront cache.

Jan 26, 2023

Using AWS CLI command to list AWS CloudFront distributions and invalidating CloudFront cache.

CLI Command1 to list CloudFront2 distributions

aws cloudfront list-distributions \
  --query 'DistributionList.Items[*].{Id: Id, Name: DomainName, Aliases: Aliases}' \
  --output yaml

Sample: CloudFront distributions list output

- Aliases:
    Items:
    - www.example.com
    Quantity: 1
  Id: A0MA0XA12AB8AB
  Name: a0abcdefg0abcd.cloudfront.net

Invalidating CloudFront cache

aws cloudfront create-invalidation \
  --distribution-id A0MA0XA12AB8AB \
  --paths "/"

Sample: Invalidation output

{
    "Location": "https://cloudfront.amazonaws.com/2020-05-31/distribution/A0MA0XA12AB8AB/invalidation/I1OLTGXS6RIYZ7",
    "Invalidation": {
        "Id": "I1OLTGXS6RIYZ7",
        "Status": "InProgress",
        "CreateTime": "2023-01-27T00:45:55.580000+00:00",
        "InvalidationBatch": {
            "Paths": {
                "Quantity": 1,
                "Items": [
                    "/"
                ]
            },
            "CallerReference": "cli-1674780355-566141"
        }
    }
}

Get invalidation status

aws cloudfront get-invalidation \
  --distribution-id A0MA0XA12AB8AB \
  --id I1OLTGXS6RIYZ7

Sample: Invalidation status output

{
    "Invalidation": {
        "Id": "I1OLTGXS6RIYZ7",
        "Status": "Completed",
        "CreateTime": "2023-01-27T00:45:55.580000+00:00",
        "InvalidationBatch": {
            "Paths": {
                "Quantity": 1,
                "Items": [
                    "/"
                ]
            },
            "CallerReference": "cli-1674780355-566141"
        }
    }
}