Launch An EC2 Instance Using The AWS CLI

Emmanuel
4 min readMay 14, 2024

--

Introduction

This article outlines a step-by-step guide to launching an EC2 instance using the AWS CLI.

Using the AWS CLI has many benefits. This tool streamlines AWS management through automation, scripting, and unified command control, speeding up tasks and enhancing efficiency over graphical interfaces.

It's consistent across environments, and the feature simplifies script transfer and integration into DevOps.

The AWS CLI supports advanced configurations and detailed, customizable outputs, which makes it especially beneficial for command-line proficient users managing remote operations.

Prerequisites

  • You need an account with AWS (Amazon Web Services). Click here to get going on that.
  • You will need to install the AWS CLI on your local machine. Find the documentation to do here.

What is an EC2 Instance?

An EC2 (Elastic Compute Cloud) instance is a virtual server on Amazon’s AWS platform, offering scalable computing power in the cloud.

It provides a wide range of instance types tailored to different needs, such as CPU, memory, storage, and networking capacities, making it suitable for everything from small websites to large enterprise applications.

Users have full control over their virtual servers, including root access to install and configure software as needed.

EC2 ensures secure operations within a customizable virtual network environment similar to traditional data centers, backed by the scalability of AWS infrastructure.

It supports seamless integration with other AWS services, facilitating complex applications and data workflows.

With flexible pricing models like On-Demand, Reserved, and Spot Instances, EC2 is cost-effective, catering to diverse budgetary and workload demands.

Here’s a video outlining what we cover in this article!

How to launch an EC2 using the AWS CLI

Step 1: Create a Key Pair

aws ec2 create-key-pair --key-name nginx --query 'KeyMaterial' --output text > nginx.pem

The command creates a new key pair named ‘nginx’. The private key file is saved to nginx.pem.

Step 2: Change Permissions of the Key Pair

chmod 400 nginx.pem

This will adjust the file permissions to ensure only the owner can read the private key, enhancing security.

Step 3: Create a Security Group

aws ec2 create-security-group --group-name t2s-nginx-sg --description "Security group for SSH and HTTP" --vpc-id vpc-ngs34dvxsb3f5

This command creates a new security group for SSH and HTTP traffic in a specific VPC. Make sure to replace the vpc ID with the desired ID.

Step 4: Authorize SSH Access

aws ec2 authorize-security-group-ingress --group-id sg-071a908845ca --protocol tcp --port 22 --cidr 172.31.0.0/16
aws ec2 authorize-security-group-ingress --group-id sg-071a908845ca --protocol tcp --port 22 --cidr 256.229.219.207/32

The first command configures the security group to allow SSH access from a specific IP range (172.31.0.0/16).

The second command configures the security group to allow SSH access from only one IP address (56.229.219.207/32).

To find the IP address of your local machine, you can use one of the following commands on the CLI:

curl ifconfig.me
curl icanhazip.com

Step 5: Allow HTTP Access

aws ec2 authorize-security-group-ingress --group-id sg-071a908845ca --protocol tcp --port 80 --cidr 0.0.0.0/0

The command above opens HTTP access to the EC2 instance from anywhere on the internet.

Depending on the requirements, if you want to expose the web application to everyone on the Internet, you will use this option as part of your command: — cidr 0.0.0.0/0.

However, if you want to use a more secure protocol for your web app, you must use HTTPS, which requires attaching a TLS/SSL certificate to the instance hosting the Web App.

Step 6: Installing NGINX (Web Server)

Use the following commands to accomplish the task. You could also add the script to your user data.

But that’s something I will cover in another article.

sudo apt update
sudo apt install -y nginx
sudo systemctl start nginx
sudo systemctl enable nginx

Step 7: Verify the Instance and NGINX Installation

aws ec2 describe-instances --filters "Name=tag:Name,Values=webserver-nginx"

This command will check the status of instances tagged as ‘webserver-nginx’.

Step 8: Check NGINX Status

SSH into your instance using the command below.

ssh -i /path/to/your-key.pem ubuntu@<Public-IP>

Use this command to check if your NGINX web server is running.

systemctl status nginx

You can verify if your Web Server is running through the browser. Copy and paste the Public IP from the EC2 dashboard on the browser.

If you do not see something like this (image below), you may need to ensure the address is like this: HTTP://Public_IP_Address.

You get to see this after you remove the “s” from “https” since we only allowed HTTP (and not HTTPS) as a rule we attached to the security group.

In Conclusion

This detailed guide on using the AWS CLI to launch an EC2 instance, from setting up a key pair to installing and verifying NGINX, emphasizes the CLI's benefits, such as automation, scriptability, and efficient resource management. It is suitable for both beginners and advanced users, making it an invaluable tool for developers and administrators working with AWS.

--

--

Emmanuel
Emmanuel

Written by Emmanuel

Certified Cloud Solutions Architect passionate about Cloud & DevOps, Technology Evangelism, Pastoral Ministry, Educator and Mentor, and World Travel.

No responses yet