How to Create an EC2 Instance using AWS CLI

How to Create an EC2 Instance using AWS CLI

·

2 min read

CLI stands for Command Line Interface. It is a user interface that allows users to interact with the device/system through commands using only the keyboard. This is unlike the GUI (Graphical User Interface) which lets a user interact with the device/system with the help of graphical elements like icons, menus, etc. Many network engineers prefer the CLI because of its faster processing speed than the GUI.

AWS CLI is a tool that allows the automation of common and repetitive administrative tasks in AWS. This automation of tasks can assist developers to manage and run various AWS services from the command line, without the need to sign in to the AWS management console.

In this article, I'll show you the steps to launch an AWS EC2 Instance using CLI.

Step 1: Download and Install AWS CLI

Using the link below, you can download and install the AWS CLI on Windows or Linux.

https://awscli.amazonaws.com/AWSCLIV2.msi

You can confirm if it is properly installed by running "aws --version"

Step 2: Create an IAM user to get access and a secret key for login

On your AWS management console, go into the IAM service and do the following steps:

Click on "Users"

Click on "Add users", create a user name, tick the boxes and create a custom password as seen below

Click on permissions and attach policies as seen

Next, create user

Download the CSV file. It contains your access ID and secret key ID to enable you log in, so keep it safe.

Step 3: Configure AWS

Step 4: Create Key Pairs

aws ec2 create-key-pair --key-name MyKeyPair

Key-Pairs are necessary for accessing your EC2 instances using a CLI.

It should appear like this on your terminal

To be sure it was created properly, you can confirm from your AWS console. Select "key pairs" under Network and Security.

Step 5: Create a Security Group

aws ec2 create-security-group --group-name MysecurityGroup --description "SecurityGroup"

A security group acts as a virtual firewall to filter incoming and outgoing traffic from an EC2 Instance.

The security group should appear on your console like this

Step 6: Launch your EC2 Instance

aws ec2 run-instances --image-id ami-0c0950d66b1670a5c --count 1 --instance-type t2.micro --key-name MyKeyPair --security-group-ids sg-0c1cf468b65396fc9 --subnet-id subnet-0fd73b99568743db1

Remember to replace the necessary parameters with your own.

Congratulations!!! you have successfully created and launched an EC2 instance using AWS CLI.