Introduction
In the dynamic world of cloud computing, efficient management of cloud resources is a key concern for DevOps professionals. Particularly for those managing AWS environments, the ability to quickly assess and organize information about EC2 instances is invaluable. In this article, I will share a custom script that I developed to streamline this process – a handy tool for any AWS DevOps engineer's toolkit.
The Challenge: Managing AWS EC2 Instances
As AWS environments grow, so does the complexity of managing numerous EC2 instances. Whether it's for compliance, cost management, or migration planning, having a quick and easy way to list instances, along with their AMIs and tags, is crucial. The standard AWS Management Console interface, while robust, can sometimes be cumbersome for these tasks, especially when dealing with a large number of instances.
The Solution: A Custom AWS CLI Script
To address this, I've created a Bash script that utilizes the AWS Command Line Interface (CLI) to fetch and display detailed information about EC2 instances in a structured and readable format. This script not only lists the instances but also fetches the names of the AMIs used and the custom tags assigned to each instance. Furthermore, it outputs this data both as a neatly formatted table in the terminal and as a CSV file for further analysis or record-keeping.
Script Features
- List EC2 Instances: Retrieves details of all instances in your default AWS region.
- AMI Information: Displays the AMI ID and the corresponding AMI name for each instance.
- Instance Tagging: Shows the 'Name' tag of each instance, aiding in easy identification.
- Formatted Output: Presents the information in a clear, table-like format in the terminal.
- CSV Export: Generates a CSV file with all the gathered data for documentation or further analysis.
Prerequisites
Before running the script, ensure that:
- AWS CLI is installed and configured with the necessary permissions.
- You have a basic understanding of Bash scripting and command-line operations.
#!/bin/bash
# Output file name
output_file="instance_details.csv"
# Function to print in a formatted table style
print_table_header() {
printf "%-20s %-20s %-50s %-30s\n" "Instance ID" "AMI ID" "AMI Name" "Instance Name"
printf "%s\n" "-----------------------------------------------------------------------------------------------------------------------"
}
print_table_row() {
printf "%-20s %-20s %-50s %-30s\n" "$1" "$2" "$3" "$4"
}
# Write the header row to the CSV file
echo "Instance ID,AMI ID,AMI Name,Instance Name" > "$output_file"
# Print table header
print_table_header
# Fetch instance details and iterate over them
aws ec2 describe-instances \
--query "Reservations[*].Instances[*].[InstanceId, ImageId, Tags[?Key=='Name'].Value|[0]]" \
--output text | while read -r instance_id ami_id instance_name
do
# Fetch the name of the AMI
ami_name=$(aws ec2 describe-images --image-ids "$ami_id" \
--query "Images[*].Name" --output text)
# Write the details to the CSV file
echo "\"$instance_id\",\"$ami_id\",\"$ami_name\",\"$instance_name\"" >> "$output_file"
# Print the details in a table format
print_table_row "$instance_id" "$ami_id" "$ami_name" "$instance_name"
done
echo "Output written to $output_file"
Usage Guide
- Copy the script into a file, such as
aws-instances.sh
. - Make it executable:
chmod +x aws-instances.sh
. - Run the script:
./aws-instances.sh
.
Conclusion
This script is a testament to the flexibility and power of the AWS CLI, combined with the simplicity of Bash scripting. It's a perfect example of how a little automation can go a long way in making cloud management tasks more manageable. By integrating such tools into your DevOps practices, you can significantly enhance productivity and maintain better control over your AWS environment.
About the Author
Alan is a seasoned DevOps Engineer with extensive experience in cloud computing and automation. He currently works at Scibite/Elsevier, where he focuses on developing innovative solutions to streamline cloud operations and enhance system efficiency. His passion for technology extends beyond his professional life, as he actively contributes to various tech blogs and forums.