How to use AWS CLI to Launch an EC2 Instance

Mrunali Gorde
4 min readMar 18, 2021
Amazon Web Services

What is AWS CLI?

  • AWS CLI stand for Amazon Web Services Command Line Interpreter, in which we can carry out all the activities which we do on GUI just by using CLI.
  • Sometimes we need to launch more than one instances but on GUI , it is quite time consuming process, to make way out of it AWS allows use to overcome that drawback by using CLI.
  • AWS CLI acts as a powerful tool of AWS which makes using AWS more easier.

Steps for launching an instance:-

Most important prerequisite for this is you need to have an

  • AWS root Account
  • Install AWS CLI
  • Create an IAM user.

Following are the steps to create IAM user Account:-

  1. Login through root user and select IAM service
  2. Add user , set user details , provide password

Click on next and finally create the IAM user.

  • Configure AWS CLI :- After installing configure CLI aws configure if you are the first time user. Use command aws --version to check the version of CLI. Check the present instances aws ec2 describe-instances
  • Create a security group:- Here we are going to create a security group for our ec2 instance. We need to attach it while launching. We can take help from the command line whenever required using help . Create SG using aws ec2 create-security-group --description Security_group_using _AWS_CLI — grup-name MyFirstSecurityGroup . Note down the group id we will need it further.

By default, the security groups blocks the traffic from outside world. We need to allow SSH and HTTP services. So use this command to customize the security group and write inbound rules for the it.

aws ec2 authorize-security-group-ingress --group-id sg-05c85dfe898db58 --protocol tcp --port 22 --cidr 0.0.0.0/0

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

We can verify by visiting AWS Management Console:-

  • Create a key pair:- We can login the console using that key. Use this following command aws ec2 create-key-pair --key-name awscli . This key should be kept secure.
  • Launch an EC2 instance:- Now we are launching ec2 instance using IAM user and we will mention entire configuration of our instance in single command. aws ec2 run-instances --image-id ami-0947d2ba12ee1ff75 --instance-type t2.micro --count 1 --key-name awscli --security-group-id sg-05c85dfe899db758

You can check whether the instance is launched by visiting management console or your using aws ec2 describe-instances .

  • Create EBS volume:- Create a volume which we will attach to our instance. Use this command aws ec2 create-volume — availability-zone us-east-1d — — volume-type gp2
  • Attach Volume:-Attach this created volume to the ec2 instance created. aws ec2 attach-volume --instance-id 04d03f824766ba3516 --device /dev/xvd

In the above image we can clearly see that our volume is successfully attached. Check on management console too.

We have successfully launched an ec2 instance using command line and attached EBS volume too!!!

--

--