Cloud infrastructure management can be a challenging task, particularly for system recovery, backups, and rapid installments. Imagine having a solution that automates the process and eliminates manual backups—it seems impossible, right.? That’s where one crucial practice of AWS comes into play—Amazone machine images (AMI) that ensure automatic backups, quick deployments, and system recovery methods.
The AWS console also allows users to create AMI manually, and by leveraging the bash script, they can automate the processes. This automation saves a lot of time and effort by eliminating repetitive tasks and human errors, allowing for more productive tasks. This guide will cover what AWS, EC2, and AMIs are, with a brief description of cloud computing and a step-by-step process of creating and using AMIs. You will also learn about the automatic creation and deletion of AMI using bash scripts that help businesses automate workflow.
What is AWS?
Amazon Web Service (AWS) is a leading cloud computing platform that provides a variety of services, computer resources, and storage. It is the most essential and widely used platform for businesses and developers to code, build, deploy, and manage applications without the need for physical infrastructure. AWS allows you to scale the resource as required, ensuring efficiency, lower cost, and becoming more agile. A wide range of industries uses AWS, including startups, enterprises, and government agencies, to utilize a variety of services. AWS offers 200+ fully featured services, including building databases using emerging technologies like machine learning, analytics, and security.
What Is Cloud Computing?
Cloud computing is like renting an office space instead of buying an entire building. Similarly, instead of using physical servers, businesses can leverage cloud service providers such as AWS, Microsoft Azure, and Google Cloud to host their applications. Cloud computing refers to the on-demand delivery of computing services, such as databases, storage, software, and networking over the Internet. Cloud computing enhances scalability, minimizes costs, improves operational efficiency, and eliminates the dependencies on physical hardware.
What is EC2 (Elastic Compute Cloud)?
EC2 refers to Elastic Compute Cloud, a web service that allows users to launch their instances (virtual machines) in the Amazon web service (AWS) cloud. Unlike traditional servers, EC2 is a bit more flexible and offers virtual instances that can be resized to meet the ever-changing demands. EC2 allows you to choose pre-built templates and create custom AMIs tailored to your needs—whether you need a high-performance machine or instance for a simple web application. It provides various instances, such as
- General Purpose
- Memory Optimised
- Storage Optimised
- Compute Optimised
What is an AMI?
Amazon Machine Image (AMI) is a template containing the software configuration required to run EC2 instances (virtual machines). AMI is pre-configured with operating systems, servers, applications, and other configurations needed to create new instances on the elastic compute cloud. In short, AMI is used to deploy virtual machines using preconfigured environments in the cloud instead of managing everything manually. An AMI consists of,
- Root Volume
- Launch Permissions
- Block Device Mapping
Why Create an AMI?
Think of an AMI as an insurance policy for your cloud infrastructure. Creating AMI ensures that you can quickly restore instances in case of failure, deploy new instances without repetitive setup, and maintain reliable infrastructure across AWS environments. Here are some key benefits of deploying AMI:
- Backup and recovery: AMI offers point-in-time backup capabilities, allowing you to easily restore your EC2 instances in case of an outage.
- Scaling Applications: Quickly deploy identical configurations across multiple instances from a single AMI to manage load increase.
- Environment Consistency: Ensure uniform configurations across deployments by standardizing server configurations for making seamless and reliable backups.
- Cost-effectiveness: Deploying multiple EC2 instances from AMI can be more cost-effective than manually setting up each instance.
Creating an AMI using AWS Console
While AMI focuses on automation, understanding the manual API approach provides valuable contexts. Using the AWS management console, you can create AMI seamlessly—- Here are the steps:
- Open an AWS management console and log in
- Navigate to the EC2 dashboard
- Select the instance for creating an AMI in the navigation panel
- Click on Actions > Image and Template. Create Image
- Enter an image name and description (optional)
- Click Create Image
- All done! You can access AMI under Images > AMIs.
This process takes snapshots of EBS volumes through API calls and registers them as an AMI.
Using AMI for launching Instances
Once the AMI is created, it is registered in the AWS EC2 service. Now, you can use AMI to launch new instances without configuring everything manually.
- Go to EC2 > AMIs
- Select the AMI from the list
- Click Launch Instance
- Choose an instance type
- Configure additional parameters like storage and security groups
- Click Launch to deploy new instances
AMIs play an invaluable role in automating deployments and ensuring that new instances start with exactly the same data and consistent configuration.
How to Deregister an AMI?
When you don't need it, you can accumulate AMIs for optimizing the storage cost. Deregietring an AMI is a two-step process in AWS architecture: deregistering the AMI and deleting the snapshots. Choose the preferred option based on your needs—for complete cleanup, both steps must be performed. For deregistering an AMI, you need to follow these steps:
- Navigate to AWS EC2 > AMIs
- Select the AMI you want to remove
- Go to actions > Deregister AMI
- Confirm Deregistration
- Delete the associated EBS snapshots to free up storage space
What is the Cost of AMI?
Although creating an AMI is free, there are some associated costs based on AWS’s standard infrastructure pricing models. Here are some key components that require cost:
- AMIs are stored as EBS snapshots, which require storage charges based on the total size of EBS snapshots.
- The newly launched EC2 instance from an AMI needs to pay charges based on its type and usage.
- The large amount of data transfer between regions may require additional charges
Moreover, AWS offers a pricing calculator to estimate expenses to manage the overall cost of creating AMI efficiently.
Automating AMI Creation with Bash Scripts
For automating the entire AMI creation process, infrastructure-as-code principles are used with bash scripts for consistent AMI management.
1. Creating an AMI Automatically
For creating an AMI with a date-stamped name, use the below-given script to automate the process:
#!/bin/bash
# Replace 'i-000b9189*******' with your actual instance ID
INSTANCE_ID="i-0c2d17a80*******"
AMI_NAME="CRM-EVENING-$(date +\%d-%B-%Y)"
REGION="us-east-1"
aws ec2 create-image \
--instance-id $INSTANCE_ID \
--name "$AMI_NAME" \
--description "AMI created via cron" \
--region $REGION \
--no-reboot
This script uses the AWS Cli to integrate the CreateImage API.
2. Automating the Deletion of Old AMIs
To free up storage space and costs, use this script to automatically delete AMIs older than three days:
#!/bin/bash
REGION="us-east-1"
AMI_NAME="RDS-EVENING-$(date -d "3 days ago" +\%d-%B-%Y)"
echo "Fetching AMI IDs for $AMI_NAME"
AMI_IDS=$(aws ec2 describe-images \
--region $REGION \
--filters "Name=name,Values=$AMI_NAME" \
--query "Images[*].ImageId" \
--output json | jq -r '.[]')
for ami_id in $AMI_IDS; do
echo "Deregistering AMI: $ami_id"
snapshot_ids=$(aws ec2 describe-images --image-ids $ami_id --region $REGION --query 'Images[*].BlockDeviceMappings[*].Ebs.SnapshotId' --output text)
aws ec2 deregister-image --image-id $ami_id --region $REGION
for snapshot_id in $snapshot_ids; do
echo "Deleting Snapshot: $snapshot_id"
aws ec2 delete-snapshot --snapshot-id $snapshot_id --region $REGION
done
done
3. Setting up the Automation Cycle
For setting up complete infrastructure lifecycle management, schedule the scripts using cron:
# For daily AMI creation at 1:00 AM
0 1 * * * /path/to/create-ami.sh >> /var/log/ami-creation.log 2>&1
# For daily cleanup of 3-day-old AMIs at 2:00 AM
0 2 * * * /path/to/clean-ami.sh >> /var/log/ami-cleanup.log 2>&1
Conclusion:
Automating AMI creation and deletion with bash scripts transforms manual infrastructure management into a systematic process. This approach uses advanced DevOps practices, ensuring backups are always up-to-date—saving time and effort and eliminating human intervention. As far as businesses use this approach, this enhances the cloud management strategy and makes the AWS infrastructure more efficient and cost-effective. So why are you spending long hours on repetitive tasks? Leverage the automation and create the EC2 instances across AMIs with bash scripts.