Introduction
In this project, I will go over how to create an AWS EC2 instance, use that instance to create an Amazon Machine Image (AMI), and launch another instance using the AMI created. I will also show how to perform these actions using the Command Line Interface (CLI)
Key terms you should be aware of:
- EC2 — Elastic Compute Cloud, an AWS service
- EC2 Instance — This is a virtual server that can be used to host and run applications
- Amazon Machine Image (AMI) — This is a supported and maintained image that provides the information required to launch an instance.
- Command Line Interface (CLI) — A tool used to manage AWS services using a command line terminal. It offers the ability to control multiple AWS services through scripts
Moving their applications from on-premise server(s) to EC2 instance(s) in the cloud provides various advantages for organizations. Some of these advantages are:
- Scalability: Ability to take advantage of the cloud’s scalability. Instances can be easily scaled up or down depending on demand without the need to invest in additional hardware.
- Reliability: Ability to ensure applications are highly available using AWS’s multiple Availability Zones and failover features.
- Security: Ability to use the security features AWS provides such as encryption, access controls, network security, etc, that will help improve the security of apps and other data.
- Cost-Savings: Pay-as-you-go pricing is used in AWS which will reduce upfront costs of purchasing and maintaining hardware. AWS also has other cost-optimization tools to optimize infrastructure in the cloud.
- Flexibility: Ability to spin up new instances, test features, and experiment as AWS constantly provides features and services that their customers need to adapt quickly to market conditions and customer needs.
Without further ado, let’s get into it.
Steps
- Log in to your AWS account and navigate to the EC2 console
- Launch an instance with the following:
- Name : myserver
- Select a free tier Ubuntu AMI
- Instance type: t2.micro
- Select or create a key pair
- Select or create a security group that allows SSH and HTTP traffic from anywhere
- Click on Advanced details and in the user data field, enter the following code block
#!/bin/bash
apt-get update -y
apt-get install apache2 -y
systemctl start apache2
- Launch the instance
- Wait for the instance state to show as “Running” and “Status check” to show as “2/2 checks
- Copy the public IP address of the instance and paste it in a browser tab. You will see the Apache2 default page
3. Next step is to create an AMI from the instance we just created. Do the following:
- Select the “myserver” instance just created. Click on Actions -> Image and templates -> Create image
- Enter an image name: “myserver-image”, leave all other settings as default and click Create image
- Wait for the status of the AMI to show as “Available”
- Click on “myserver-image” and select Launch instance from AMI
- Select a key pair, security group for SSH and HTTP access and launch the instance.
- Wait for the instance state to show as “Running” and “Status check” to show as “2/2 checks
NOTE: The image we took has the configurations of the instance we launched earlier. There is no need to enter the user data when launching the instance from the AMI
- Copy the public IP of the instance and paste into a browser. You will get the same Apache2 default page as seen above
Clean-Up
- Terminate all created instances
- Deregister the AMI you created
- Delete snapshots taken
Taking it up a notch, this can all be done by using the AWS CLI
Various commands will need to be run. The first thing that needs to be done is ensuring that the AWS CLI is configured with your access key and secret access key. You can find out how to do that here.
Items needed prior to using CLI
- AMI ID in the needed region (e.g. us-east-1)
- Security Group ID
- Key Pair Name
- A file on your local computer containing the user data script (same commands used in user data above)
- Subnet ID
Steps
- Open your CLI and navigate to the directory that has your user data script.
For this project, I named the script “userdata.sh” and it is in my downloads folder.
2. Use the following command syntax to create an Ubuntu Server in the us-east-1 region. Make sure to replace and use the intended AMI ID, Security group ID, Subnet ID, Key Name, and User Data file name
aws ec2 run-instances --image-id ami-053b0d53c279acc90 --instance-type t2.micro --security-group-ids sg-076bb0e72e17189b9 --subnet-id subnet-03b5ab71efe6df419 --key-name server-kp --user-data file://userdata.sh
Copy the InstanceId and wait for a few minutes. Use the following command syntax to check if the instance is up and running (Make sure to edit the instance ID value to reflect the one you create)
aws ec2 describe-instances --instance-id i-0691c7fd857aedf3b
3. Use the following command syntax and replace with the InstanceId copied in the last step
aws ec2 create-image --instance-id i-0691c7fd857aedf3b --name "myAMI"
Copy the ImageID
4. Use the same command in step 1 to launch an instance. Replace the image ID just copied in step 3. Remove the user data option
aws ec2 run-instances --image-id ami-0fb64101f28c8f517 --instance-type t2.micro --security-group-ids sg-076bb0e72e17189b9 --subnet-id subnet-03b5ab71efe6df419 --key-name server-kp
To test go into your EC2 console and you should see two running instances. Copy the Public IP addresses of both and paste into a browser, you will see the Apache2 default page on both pages.
To clean up, terminate the instances in the console, deregister the AMI and delete the snapshots taken.
If you choose, you can use the following syntax to terminate instances in the CLI.
aws ec2 terminate-instances --instance-ids i-0691c7fd857aedf3b i-00a96c56155455130
That is it for this project. Well done and thank you if you made it this far.
Please let me know if you have any questions!