This Bash script is a simple but powerful utility for finding and displaying key information about an AWS CloudFront distribution. It works by searching for a distribution that uses a specific S3 bucket as its origin.
This is particularly useful when you know which S3 bucket your website is in but have forgotten the long, unique ID of the CloudFront distribution that serves its content.
This script is for anyone who needs to quickly:
- Find a CloudFront Distribution ID without having to log in to the AWS Console.
- Look up the details of a distribution, such as its CNAMEs (custom domains) or current status.
- Verify which distribution is connected to a particular S3 bucket.
- Automate information gathering as part of a larger administrative workflow.
The Scenario:
You have multiple projects deployed on AWS, each with its own CloudFront distribution. You need to perform an action on a specific distribution—like adding a custom domain or clearing the cache—but you can't remember its ID (e.g., E123ABCDEFGHIJ). You do, however, remember the S3 bucket name associated with the project (e.g., my-awesome-app-bucket).
The Challenge:
- CloudFront IDs are not human-friendly and are difficult to memorize.
- Searching for the right distribution in the AWS Management Console can be slow and inefficient if you have many of them.
- You need a quick, command-line way to get the information you need to proceed with other tasks.
The Solution: This script provides a direct lookup method. You provide the S3 bucket name you know, and it queries AWS to find the exact distribution linked to it, instantly giving you the ID and other relevant details.
- Gathers Information - Prompts the user for their AWS Access Key, Secret Key, Region, and the S3 Bucket Name to search for.
- Configures AWS CLI - Sets up the necessary environment variables for the AWS CLI to authenticate for the current session.
- Lists Distributions - It calls the
aws cloudfront list-distributionscommand to get a list of all distributions in the account. - Filters Results - It pipes the JSON output of the list to the
jqutility.jqfilters the list to find the one distribution whose originDomainNamematches the S3 bucket endpoint derived from the user's input. - Displays Information - If a match is found, it parses the JSON for that distribution and prints out the most important details in a clean, readable format. If no match is found, it informs the user.
cloudfront-info.sh- The main Bash script.README.md- This documentation file.
- AWS CLI - Must be installed and accessible in your system's
PATH. - jq - A command-line JSON processor. Install it via Homebrew (
brew install jq) or your package manager. - AWS Credentials with sufficient permissions.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"cloudfront:ListDistributions"
],
"Resource": "*"
},
{
"Effect": "Allow",
"Action": "sts:GetCallerIdentity",
"Resource": "*"
}
]
}- Navigate to this script's directory.
- Make the script executable (you only need to do this once):
chmod +x cloudfront-info.sh
- Execute the script:
./cloudfront-info.sh
- Follow the on-screen prompts to provide your credentials and the target S3 bucket name.
"No CloudFront distribution found"
- Verify you entered the correct S3 bucket name and AWS region.
- The distribution may have been deleted, or it may be configured with a different origin.
"jq: command not found"
- You need to install the
jqutility. On macOS with Homebrew, the command isbrew install jq. On other systems, use your respective package manager (e.g.,sudo apt-get install jqon Debian/Ubuntu).
"AWS credentials are invalid"
- You may have entered the Access Key ID or Secret Access Key incorrectly.
If you have questions or run into problems, please contact me.
Fred Lackey
📧 Email: Fred.Lackey@gmail.com
🌐 Website: @FredLackey.com