Overview:
In this quick write up, we will go over how to create an Amazon Machine Image (hereafter “AMI”) from a previously created Instance using the AWS console first. Next we’ll go through the process of creating an image via the AWS CLI.
Step 1: Create an AMI image
To create an AMI image, we’ll navigate to EC2 -> Instances -> Select the instance you want to create an image of -> right-click ->Image and Templates -> Create Image.
Alternatively you can access this from the Actions button.
On the next screen, we will create a name for our image and set our instance volumes.
We will leave the defaults and leave “No reboot” option unchecked. If this box is checked, it will attempt to create the image without shutting down your instance. Now, click on “Create Image”.
Step 2: Launching our Instance from our AMI image
Now, If we go back to our AWS Console under EC2 -> Instances -> Launch instance.
Now click on My AMIs and you will see your AMI that you created an image of.
I’m going to use an existing key pair and set ssh to my ip address and allow traffic from HTTP and HTTPS
Once these are configured, we can click on Launch Instance.
Step 3: Verify access to NGINX
We will now go back to our AWS console for EC2 and to grab the public ip address from the new instance to see if we can access our webserver.
We have successfully created an image from our previous instance and launched it via the console!
Now let’s go through the steps of creating an AMI via the AWS CLI.
We’ll first you’ll need to make sure that you have AWS CLI installed and configured. You can follow my previous guide for setting AWS CLI Here.
Step 1: Finding our instance id
First we need to find out InstanceID. We can run the command below that will show us all our instances.
aws ec2 describe-instances
Step 2: Creating our AMI
To create our AMI we’ll use the following command.
aws ec2 create-image \
--instance-id i-1234567890abcdef0 \
--name "My server" \
--description "An AMI for my server"
Now we have our AMI created and it’s ImageId.
Step 3: Create Instance with image
Now we can create an instance with the image we created.
aws ec2 run-instances \--image-id <AMI_Name> \--instance-type t2.micro \--security-group-ids <Security_Group_Name> \--key-name <Key_pair>\--count 1
We now our instance created.
Step 4: Check if NGINX is running
Now we can check to see if Security groups are applied successfully and NGINX is installed. We first need to grab the public ip address.
aws ec2 describe-instances --filters --instance-id <Your_instance_ID>
Now that we have our public ip address of this instance. We can test out to see if our webserver is working.
Success!! We have created an image from an existing instance all from CLI!!
Thanks for reading and don’t forget to stop your instance!!