You are currently viewing How To Create An AWS EC2 Instance Using AWS CLI

How To Create An AWS EC2 Instance Using AWS CLI

How to create an AWS EC2 instance using AWS CLI

Hello Everyone

Welcome to CloudAffaire and this is Debjeet.

In the last blog post, we have discussed how to create a custom VPC using AWS CLI.

In this blog post, we will discuss how to create an AWS EC2 instance using AWS CLI. We will create a Key-Pair and an EC2 instance with LAMP configuration using user_data. This is a direct continuation of the previous blog post and cannot be performed without creating the VPC 1st.

Prerequisite for this demo:

  • One Linux instance with AWS CLI installed and configured.
  • One custom VPC as per previous blog post.

How to create an AWS EC2 instance using AWS CLI:

Step 1: Get Amazon Linux 2 latest AMI ID.

Step 2: Create a key-pair.

Step 3: Create user data for a LAMP stack.

Step 4: Create an EC2 instance.

Step 5: Add a tag to the ec2 instance.

Step 6: Check if the instance is running.

Step 7: Get the public ip address of your instance.

Step 8: Try to connect to the instance.

Note: You can check your web server by opening your public IP in your favorite browser.


Hope you get the idea of using AWS CLI. There is some limitation for using AWS CLI like passing output values of one code block to input value of another code block. Though this can be overcome by using variables like it has been done in the last two blogs. In my experience AWS CLI can be used for ad-hoc purpose. But if you want to build your infrastructure with DevOps methodology, SDK like Python Boto3 or external tools like terraform has much better options.

In the next blog post, we will start with a new AWS service. You can explore other AWS service related CLI using below link.

To get more details on AWS CLI, please refer below AWS documentation