Creating an EC2 Instance with AWS CLI: A Simple Tutorial
Last Updated :
08 Oct, 2024
Amazon EC2 (Elastic Compute Cloud) is a core service in AWS that allows users to launch and manage virtual machines in the cloud. While the AWS Management Console is commonly used to create and manage EC2 instances, the AWS Command Line Interface (CLI) offers a powerful way to automate the process, especially in scripts and infrastructure-as-code setups. This guide walks you through launching an EC2 instance using the AWS CLI.
Prerequisites: Setting Up AWS CLI and Configuring Credentials
Before launching an EC2 instance, would you like to know What is AWS CLI?
You can refer to this article What is AWS CLI?
In this article, we will look into the process of launching instances that is through AWS CLI(Command Line Interface).
Step-by-Step Guide to Creating an EC2 Instance Using AWS CLI
So, let's begin with AWS CLI by launching an EC2 Instance using it. Creating an instance with AWS CLI is the same as launching one with AWS console. Open your command prompt as administrator by right-clicking on it.
Creating a VPC
The first thing to do is to create a VPC(virtual private cloud) under which an EC2 instance will be launched. For creating a VPC in CLI type the given command on the cmd.
aws ec2 create-vpc --cidr-block 10.0.0.0/16
Here the CIDR block I have taken is 10.0.0.0/16, you can change it as per your need. After running this command following output will be given in a JSON format. Note the vpcId .

Creating Subnets
Next, create two subnets and make one as public to make it accessible from the internet. To do so use the below command:
aws ec2 create-subnet --vpc-id <vpcId> --cidr-block 10.0.1.0/24
Note the SubnetId generated here, so that this subnet can be made as public later on. The CIDR block we have used here is 10.0.1.0/24.
Now create a second subnet with CIDR block 10.0.0.0/24. (CIDR block values can be changed as per user needs):
aws ec2 create-subnet --vpc-id <vpcId> --cidr-block 10.0.0.0/24

Creating Internet Gateway
Internet gateway is used by the private subnet to access the internet for its updates and other packages installations. Create an internet gateway by using the following command:
aws ec2 create-internet-gateway
After the internet gateway is created, note the InternetGatewayId and to attach this internet gateway to the already created VPC. To do so use the below command:
aws ec2 attach-internet-gateway --vpc-id <vpcId> --internet-gateway-id <InternetGatewayId>
Here type the noted vpcId (in place of <vpcId>) and InternetGatewayId (in place of <InternetGatewayId>)
Creating Route Table
The next step is to create a route table and assigning it to the already created VPC. After creating the route table assign the route to this route table. Commands for the same are as given.
aws ec2 create-route-table --vpc-id <vpcId>
Now, use the RouteTableId and use it in the next step:
aws ec2 create-route --route-table-id <RouteTableId>
--destination-cidr-block 0.0.0.0/0 --gateway-id <nternetGatewayI>
Here we have used the 0.0.0.0/0 as destination CIDR block.


Viewing the Route Table and Subnets
To check whether route table and subnets are created and assigned successfully use the below commands:
aws ec2 describe-route-tables --route-table-id <RouteTableId>
aws ec2 describe-subnets --filters "Name=vpc-id,Values=<vpcId>"
--query "Subnets[*].{ID:SubnetId,CIDR:CidrBlock}"
Here replace your vpcId in place of <vpcId>.

Associating Route Table and Modifying Subnet
The next step is to associate the route table with the subnet and making the same subnet as public by mapping the public IP address to it. Enter the SubnetId and RouteTableId that you noted earlier. To associate route table type...
Type:
aws ec2 associate-route-table --subnet-id <SubnetId> --route-table-id <RouteTableId>
To map the public IP to the subnet, use the below command:
aws ec2 modify-subnet-attribute --subnet-id <SubnetId> --map-public-ip-on-launch

Creating Key Pair and Security Group
The most important step is to create a key pair. This key pair must be kept safe and secure with the user so that the person can access the EC2 instance created using this key pair.
Now, create the key-pair using the below command:
aws ec2 create-key-pair --key-name AWS-Keypair --query "KeyMaterial"
--output text > "C:\AWS\AWS_Keypair.pem"
Here we have named the key pair file(.pem file) as AWS-Keypair and the path where our file will be downloaded is C:\AWS\AWS_Keypair.pem. Both these things can be changed by the user.
For security group use the below commands:
aws ec2 create-security-group --group-name <security-group-name> --description "<description>"
--vpc-id <vpcId>
Here provide name and description to the security group and add it in place of <security-group-name> and <description> respectively. Note the GroupId and use it in the next step.
aws ec2 authorize-security-group-ingress --group-id <GroupId>
--protocol tcp --port 22 --cidr 0.0.0.0/0
The protocol/port we use here is TCP/22.

Running the EC2 Instance
Finally, after all the setup completed successfully now the time is to run the instance. For running the EC2 Instance use the command as given below.
aws ec2 run-instances --image-id <ami-id> --count 1 --instance-type t2.micro
--key-name <Keypair-name> --security-group-ids <SecurityGroupId>
--subnet-id <SubnetId>
At this step, you will need an AMI(Amazon Machine Image) image ID. For this login to your AWS Console and choose any AMI of your type. Copy the image id and replace it here in place of <ami-id>. Also use your key pair name, security group id, and subnet id at the correct place in the above command. Also, make a note of the InstanceId.



Viewing the Instance
Now after the instance status is "running" type the command to view the complete details of the EC2 instance that you just created:
aws ec2 describe-instances --instance-id <InstanceId>
Enter the InstanceId you noted at the above step.

Verifying the EC2 Instance
To verify whether the EC2 instance created using the AWS CLI is created as per need, log in to your AWS Console and open the EC2 service and check for the instance.

Conclusion
Launching an EC2 instance using AWS CLI provides greater control and flexibility for automating cloud infrastructure. By following this step-by-step guide, you can quickly spin up virtual machines, manage them efficiently, and automate recurring tasks, all while gaining a deeper understanding of how AWS operates behind the scenes. Whether you're managing a single instance or deploying an entire fleet of servers, AWS CLI is an invaluable tool for streamlining your cloud operations.
Similar Reads
Create EC2 Instance in AWS (Amazon): Complete Tutorial
Amazon EC2 (Elastic Compute Cloud) is a cloud computing service provided by AWS that allows users to rent virtual machines (VMs) to run applications on-demand. EC2 Offers a scalable, cost-efficient, and flexible computing environment without the need for users to manage physical hardware. ]Users can
11 min read
Creating VPC With A Private Subnet And Transferring Files To AWS Instances
Virtual Private Cloud is a logically isolated portion of the AWS ecosystem and allows resources within it logically isolated from the resources outside it. There are availability zones in these VPCs that can be used to assign private subnets or public subnets. AWS instance is a virtual server that i
7 min read
AWS EC2 On Demand and Spot Instances
In this article, we are going to understand difference between EC2 On demand and Spot instances. We are going to learn their usage and working. it is important to understand the difference between AWS EC2 On Demand and Spot Instances so that you can choose right instances for your business requireme
6 min read
Create Ubuntu Server on AWS EC2 Instance
In this article, we'll take you through the entire process of creating an Ubuntu server on an AWS EC2 instance from scratch. Whether you're new to Amazon Web Services (AWS) or just looking to set up a new server, this step-by-step tutorial will cover everything you need to know. We'll start right fr
6 min read
How To Create Spot Instance In Aws-Ec2 In Aws Latest Wizards?
Spot instances are available at up to 90% discount because when instances are not used then the instance available in spot instance at a cheaper rate so that people can utilize. it. When the demand increases then amazon sent a notification your spot instance will disappear after two minutes. We can
6 min read
How To Get An AWS EC2 Instance ID From Within That EC2 Instance?
Amazon EC2 is an important service of AWS that provides users a scalable and virtual computing resources in the cloud. Each EC2 instance is assigned a unique identifier called Instance ID. Instance ID is an alphanumeric code(which means a combination of alphabets and numbers) that is used for variou
4 min read
How to Install an SSL/TLS Certificate In Amazon EC2 (AWS)
AWS EC2 is one of the most used and convenient solutions for hosting web servers and applications accessible worldwide. Securing your applications with an SSL certificate is vital for safeguarding user data and building trust. In this article, we will be hosting a basic web application on EC2 and wi
6 min read
How to Create a Flask API with MariaDB on AWS EC2
In this article, We are going to create an ec2 instance, install a MySQL database using MariaDB on the ec2 instance, and create an API of Login using the flask python framework. Flask is a micro web framework written in Python. MariaDB is one of the most popular open-source relational database manag
6 min read
How To Create Redhat EC2 Instance in AWS
provisioning the Red Hat Enterprise Linux (RHEL) instances on Amazon Web Services (AWS) offers a powerful and versatile solution for hosting and running applications, overseeing the jobs, and utilizing the abilities of both platforms. Red Hat Enterprise Linux is a main Linux distribution eminent for
5 min read
How To Scanning Amazon EC2 Instances With Amazon Inspector
Amazon Inspector is a vulnerability management service that continuously scans your running Amazon EC2 instances, container images in Amazon Elastic Container Registry (Amazon ECR), and AWS Lambda functions. Now here we see how to scan Amazon ec2 instances with Amazon Inspector. Amazon Inspector EC2
3 min read