Creating an Amazon Machine Image (AMI) in AWS involves several steps. An AMI is a template that contains a software configuration (e.g., an operating system, an application server, and applications) and is used to launch instances. Below is a detailed step-by-step guide to creating an AMI.
1. Prerequisites
Before creating an AMI, ensure you have the following:
- An AWS account.
- AWS CLI installed and configured.
- An EC2 instance running.
2. Preparing the EC2 Instance
Ensure the EC2 instance is configured with the desired state you want to capture in the AMI. This includes:
- Installed and configured software.
- Applied updates and patches.
- Necessary security settings and configurations.
3. Creating an AMI from an EC2 Instance
3.1. Using AWS Management Console
-
Navigate to the EC2 Dashboard:
- Sign in to the AWS Management Console.
- Open the EC2 Dashboard.
-
Select the Instance:
- In the left navigation pane, choose "Instances."
- Select the instance you want to create an AMI from.
-
Create Image:
- Click on "Actions," hover over "Image and templates," and then click "Create image."
-
Configure Image:
- Provide a unique name and description for the AMI.
- (Optional) Specify additional configuration options like instance volumes and tags.
-
Create Image:
- Click on "Create image."
- AWS will start creating the AMI. This process can take several minutes.
-
View AMI Status:
- In the left navigation pane, choose "AMIs" to view the status of the newly created AMI.
3.2. Using AWS CLI
You can also create an AMI using the AWS CLI by running the following command:
aws ec2 create-image --instance-id i-1234567890abcdef0 --name "My server" --no-reboot
- --instance-id: The ID of the instance you want to create an image from.
- --name: A unique name for the AMI.
- --no-reboot: Optional flag to prevent the instance from rebooting during the image creation process.
4. Managing the Created AMI
Once the AMI is created, you can use it to launch new instances, share it with other AWS accounts, or copy it to other regions.
4.1. Launching Instances from an AMI
-
Navigate to AMIs:
- In the EC2 Dashboard, click on "AMIs" in the left navigation pane.
-
Select AMI:
- Select the AMI you want to use.
-
Launch Instance:
- Click on "Launch" to start the instance launch wizard.
- Follow the wizard steps to configure the instance, including instance type, network settings, storage, and tags.
-
Review and Launch:
- Review the configuration and click "Launch."
4.2. Sharing an AMI
You can share your AMI with other AWS accounts.
-
Select AMI:
- In the EC2 Dashboard, go to "AMIs."
- Select the AMI you want to share.
-
Modify Image Permissions:
- Click on "Actions" and then "Modify Image Permissions."
- Add the AWS account IDs with which you want to share the AMI.
- Click "Save."
4.3. Copying an AMI to Another Region
To copy an AMI to another region, use the AWS Management Console or CLI.
Using AWS Management Console:
-
Select AMI:
- Go to "AMIs" in the EC2 Dashboard.
- Select the AMI you want to copy.
-
Copy AMI:
- Click on "Actions," then "Copy AMI."
- Specify the destination region and optionally, provide a new name for the copied AMI.
- Click "Copy AMI."
Using AWS CLI:
Run the following command to copy the AMI to another region:
aws ec2 copy-image --source-image-id ami-12345678 --source-region us-west-2 --region us-east-1 --name "My copied AMI"
- --source-image-id: The ID of the source AMI.
- --source-region: The region where the source AMI is located.
- --region: The destination region.
- --name: A unique name for the copied AMI.
5. Example Codes for AWS CLI
Below are example commands for creating and managing AMIs using AWS CLI.
5.1. Create AMI
aws ec2 create-image --instance-id i-1234567890abcdef0 --name "My server" --no-reboot
5.2. Describe AMIs
To list all AMIs owned by your account:
aws ec2 describe-images --owners self
5.3. Modify Image Permissions
To share an AMI with another AWS account:
aws ec2 modify-image-attribute --image-id ami-12345678 --launch-permission "Add=[{UserId=123456789012}]"
5.4. Copy AMI
To copy an AMI to another region:
aws ec2 copy-image --source-image-id ami-12345678 --source-region us-west-2 --region us-east-1 --name "My copied AMI"
Creating an AMI in AWS is a straightforward process, whether you use the AWS Management Console or the AWS CLI. AMIs provide a flexible and efficient way to manage and deploy instances with predefined configurations, making them a powerful tool for automating infrastructure and application deployment.