Launch EC2 instance using AWS CLI

Launch a ec2 instance and add security groups and EBS using cli.

What is AWS CLI?

So as we know to interact any program we have two ways GUI (graphical user interface) which is AWS Console and another one is CLI (command line interface) for this we have AWS CLI.

What am I gone do in this Blog, let see…

  • Create a key pair
  • Create a security group
  • Launch an instance using the above created key pair and security group
  • Create an EBS volume
  • The final step is to attach the above created EBS volume to the instance you created in the previous steps

Per-requisites for this :

  • AWS CLI should be installed in the OS
  • AWS CLI should be configured in the OS

Lets start…..

Configure the AWS user Profile in Aws Cli.

In this image we setup a user (named as tridha) profile in aws cli. And also this is my default user for AWS Cli. We can add, many profile as we can in AWS cli.

Profiles saved in AWS cli.
$ aws configure --profile <profile name>
Command to show profiles
  • This is my EC2 dashboard summary. In this you can see I have a single key-pair and security groups. Lets come to our topic now create a key-pair.

Now create a Key-pair

This is the key which is already created in AWS Account lets create a new one for this we have command

$ aws ec2 create-key-pair --key-name <keynmae>
this command is only for creating key-pair
$ aws ec2 create-key-pair --key-name <keynmae> --query KeyMaterial
--output text > filename.pem
this command is for saving data in a file.

In this Image you can see First I run the command then I show Key-Pair.

Now Its time to create a Security Group.

Let first we see that, does we have already any SG or not?

$ aws ec2 describe-security-groups
Command to describe all security groups we have.
$ aws ec2 create-security-group --group-name <group name> --description <" any thing">
Command to create a SG

In above Image You can see I create a SG named as tridha_sg.

Yet only SG created. Let add some Ingress rule i.e. inbound rule so any one can reach to or instance.

$ aws ec2 authorize-security-group-ingress --group-name <group name> --cidr
Command for adding inbound rule in SG

We add a rule that all traffic is allow in this SG.

Lets Launch A EC2 Instance.

First let see, does any instance running or not?

Look there is no instances running .

$ aws ec2 run-instances --image-id ami-06a0b4e3b7eb7a300 --count 1 --instance-type t2.micro --key-name tridha_keypair --security-group-ids sg-071b2cac2055d34af --subnet-id subnet-26b8494dCommand to launch a instance on AWS.

Finally Our instance is launch.

Create an EBS volume

In image you can see a single root volume is attached with the instance.

Lets create one more EBS volume…

$ aws ec2 create-volume --volume-type gp2  --size 2 --availability-zone ap-south-1a
command to create a EBS vloume

So here I created a EBS volume. Make sure your AZ is same as where your instance running.

Still you can see only root volume is attached. New created volume is not attached for this we have to attach that volume.

Let attach extra EBS volume of size 2 Gib.

$ aws ec2 attach-volume --instance-id i-070f999452b4c9f58 --volume-id vol-080126e0751decde3 --device /dev/sdfCommand to attach volume to instance

Look new 2 Gib EBS volume attached along with root volume.

So all done!!!

For deleting all the SETUP

You can delete it by GUI or For CLI run these command one by one.

$ aws ec2 detach-volume --instance-id <id-> --volume-id <vol-id>
$ aws ec2 delete-volume --volume-id <vol-id>
$ aws ec2 terminate-instances --instance-id <id->
$ aws ec2 delete-security-group --group-name <SG_NAME>
$ aws ec2 delete-key-pair --key-name <key_name>

Thanks for reading , I hope you like the Blog!!!




Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How To Enable Safe Mode in Windows 10 with Command Prompt || Step by Step Guide

How To Enable Safe Mode in Windows 10 with Command Prompt || Step by Step Guide

UENC Project Weekly Report#74 (May 31, 2021 — June 5, 2021)

Volta DAO — Governance, Referral Marketing and many more!

Modifying the Python language in 6 minutes

Series 3: Continuous Integration using Fastlane and Travis CI

Software Testing: Different categories of Testing

The Benefits Of A Healthy Lifestyle For Developers

How To Use SQL Subqueries

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Mukul Jeveriya

Mukul Jeveriya

More from Medium

Automating Deploy WordPress with MySQL on Kubernetes cluster on AWS.

Install Jenkins Using Docker

DocuSign JWT Authentication in webMethods CloudStreams