0
0
AwsHow-ToBeginner · 3 min read

How to Create an AMI from Snapshot in AWS

To create an AMI from an existing snapshot, register a new AMI using the snapshot as the root device. This can be done via AWS CLI with aws ec2 register-image by specifying the snapshot ID and required parameters.
📐

Syntax

Use the AWS CLI command aws ec2 register-image to create an AMI from a snapshot. You must specify the snapshot ID as the root device and provide a name for the AMI.

  • --name: The name for your new AMI.
  • --block-device-mappings: Defines the root device using the snapshot ID.
  • --root-device-name: The device name for the root volume (e.g., /dev/xvda).
  • --virtualization-type: Usually hvm for modern instances.
bash
aws ec2 register-image \
  --name "MyAMIFromSnapshot" \
  --block-device-mappings DeviceName=/dev/xvda,Ebs={SnapshotId=snap-1234567890abcdef0} \
  --root-device-name /dev/xvda \
  --virtualization-type hvm
💻

Example

This example shows how to create an AMI named MyAMIFromSnapshot from a snapshot with ID snap-1234567890abcdef0. It sets the root device to /dev/xvda and uses HVM virtualization.

bash
aws ec2 register-image \
  --name "MyAMIFromSnapshot" \
  --block-device-mappings DeviceName=/dev/xvda,Ebs={SnapshotId=snap-1234567890abcdef0} \
  --root-device-name /dev/xvda \
  --virtualization-type hvm
Output
{ "ImageId": "ami-0abcdef1234567890" }
⚠️

Common Pitfalls

  • Not specifying the --block-device-mappings correctly will cause the AMI creation to fail.
  • Using an incorrect root-device-name that does not match the snapshot device can cause boot issues.
  • For snapshots of encrypted volumes, ensure you have the right permissions and specify encryption options if needed.
  • Trying to create an AMI from a snapshot that is not a root volume snapshot will not work as expected.
bash
Wrong way (missing block device mapping):
aws ec2 register-image --name "BadAMI"

Right way:
aws ec2 register-image \
  --name "GoodAMI" \
  --block-device-mappings DeviceName=/dev/xvda,Ebs={SnapshotId=snap-1234567890abcdef0} \
  --root-device-name /dev/xvda \
  --virtualization-type hvm
📊

Quick Reference

Remember these key points when creating an AMI from a snapshot:

  • Use aws ec2 register-image command.
  • Specify --block-device-mappings with snapshot ID.
  • Set the correct --root-device-name.
  • Use hvm virtualization for modern instances.
  • Check permissions for encrypted snapshots.

Key Takeaways

Use the AWS CLI command 'aws ec2 register-image' to create an AMI from a snapshot.
Always specify the snapshot ID in the block device mappings with the correct root device name.
Ensure the snapshot is of a root volume and matches the device name to avoid boot issues.
For encrypted snapshots, verify permissions and encryption settings before creating the AMI.
Use 'hvm' virtualization type for compatibility with most modern EC2 instance types.