Invalidating a CloudFront distribution cache through the command line can be a more automated and efficient way to ensure your content is always up-to-date. Let’s go through the steps to achieve this:
Step 1: Install AWS Command Line Interface (CLI)
If you haven’t already, make sure you have the AWS CLI installed on your system. You can download and install it from the official AWS CLI website.
Step 2: Configure AWS CLI
Run the following command to set up your AWS CLI with the necessary access keys, secret keys, and AWS region. Ensure you have the required permissions to invalidate CloudFront distributions:
aws configure
Step 3: Create an Invalidation Request File
Create a JSON file (e.g., invalidation-request.json) to specify the paths you want to invalidate. Here’s an example content:
{
"Paths": {
"Quantity": 2,
"Items": [
"/path/to/invalidate/file1.jpg",
"/path/to/invalidate/file2.css"
]
},
"CallerReference": "unique-id" // You can set your unique reference here
}
Step 4: Run the Invalidation Command
Use the create-invalidation command to initiate the cache invalidation request. Replace
aws cloudfront create-invalidation --distribution-id <DistributionID> --invalidation-batch file://invalidation-request.json
Step 5: Monitor Invalidation Status
You can check the status of the invalidation by using the list-invalidations command:
aws cloudfront list-invalidations --distribution-id <DistributionID>
This will provide information about the progress and completion status of your cache invalidation.
Step 6: Validation
Once the invalidation is complete, CloudFront will start serving the updated content to your users.
By using the AWS CLI, you can automate the process of cache invalidation for your CloudFront distribution, making it more efficient and suitable for scripting or integration into your deployment pipeline.