Create an AWS EC2 Instance Using AWS CLI

In this blog post, I will show you how to deploy a Windows Server 2019 EC2 Virtual Machine using AWS CLI.

This blog post will work with Linux EC2 instances, and all you need to do is change the AMI image to a Linux AMI.

Before we can go ahead and create EC2 instances, we need to make sure we have all the minimum requirements sorted.


The requirements to create an EC2 instance using AWS CLI are:

  • Key pair to log in to the VM
  • VPC (You can use the default VPC or use an existing one)
  • AWS CLI installed and configured – please visit this blog post for more details

Get Started

You will notice that I am using AWS CLI to retrieve all the information to run the AWS CLI command. However, you can also use the AWS Management Console to retrieve the needed information.

Create a Key Pair

If you don’t have a Key Pair for to login to a Windows Server EC2 instance, go ahead and create a new one from the AWS EC2 management console.

Note – You will need to use the name of the key later on.

From the Key Pairs screen, click on the Create Key Pair and follow the prompts.

Create VPC or use an existing one

If you have a VPC, you can skip this step and go to the next one to retrieve the information of your VPCs.

In the command below, I create a default VPC since I have deleted all the VPC from my tenant.

aws ec2 create-default-vpc

To view all the VPCs in my tenants and copy the VpcId I will use the AWS command below.

Note – Copy the value of the VpcId

aws ec2 describe-vpcs

Subnet ID

Next, I need to find the subnet ID I would like to use for my EC2 instance and get all my AWS subnets, I will run the command below.

aws ec2 describe-subnets

Note – Copy the SubnetID.

Security Group

Next, I will do the same to find the Security Group ID for the VPC using the command below:

aws ec2 describe-security-groups

Note: Copy the GroupId value.

AMI image

The last step in the information gathering stage is finding the AMI ID, and in my case, I am using the AWS Management portal.

Note – I’ll use the Windows Server 2019 June 2019 image with the following ID ami-067db9db9a69322de

If you need to create a Linux VM, simply use a Linux AMI.

Create EC2 Instance

Now, that I have all the needed information all I need to do is add the details from the above steps to the command and run it.

aws ec2 run-instances --image-id ami-067db9db9a69322de --count 1 --instance-type t2.micro --key-name EC2WindowsHost --security-group-ids sg-0d6633d22184b1f40 --subnet-id subnet-0301abbdbd07826f5

After running the command, The instance is deployed within 20 seconds and can be viewed using the command below or the management portal.

aws ec2 describe-instances

You can see below that the instance appears under the EC2 console.

To create multiple instances using a single command, change the –count value to the desired number of instances you would like to create.

--count 1