How to launch EC2 instance using AWS CLI
In AWS there are 3 ways we can connect to AWS
- using AWS Console (WebGUI/WebAPP)
- AWS SDK
- AWS CLI
AWS CLI is a command line interface & we can download latest version i.e. version 2 from AWS site. URL to download AWS CLI :
After downloading it we can verify the version as below
In order to connect to AWS from AWS CLI we need to create user using IAM service.
Now we can connect to AWS using above Access Key ID & Secret Access Key
In order to launch EC2 instance we’ll need following
-AMI (image id)
-key pair name
we’ll first create key-pair using below AWS CLI command
In AWS Console we can verify its creation
Now we need to create security group
Before that we need to get the subnet id using below command
Let’s say we need to launch instance in “SubnetId”: “subnet-b14d49d9”within “AvailabilityZone”: “ap-south-1a”
Pls note that VPC id is “VpcId”: “vpc-7666791e”
Security group can be created as under
Verify Security Group in AWS Console as follows
Now we have all the details except AMI image id which we can get from AWS Console
Now we are all set to launch AWS EC2 instance.
Run the following command in CLI
aws ec2 run-instances — image-id ami-0e306788ff2473ccb — instance-type t2.micro — count 1 — subnet-id subnet-b14d49d9 — security-group-ids sg-0e56c19dc1bb20795 — key-name awscsa
It displays following
Also, we can note that security group is attached to the instance which is currently in pending state
We can confirm the details of running EC2 instance as follows
We have to now create an EBS volume of size 1 GIB & attach to this instance.
EBS Volume is regional specific i.e. we need to create EBS volume in the same Availability Zone in which the instance is running. In our case Availability Zone is ap-south-1a
To create EBS volume from AWS CLI we need to fire below command
Pls note the volumeId which we require while attaching this volume to EC2 instance.
In AWS Console we can verify this volume as follows
Now the next step is to attach this EBS volume to our EC2 instance.
Since above EBS volume is available we can attach to EC2 instance.
Before attaching the newly created EBS volume we can verify the storage of our EC2 instance as under
The AWS CLI command to attach EBS Volume to EC2 instance is :
aws ec2 attach-volume — volume-id vol-031c3e1041c7d1573 — instance-id i-0d1c40a3e1a0e1c67 — device /dev/sdf
Now , we can verify whether EBS volume has been attached to our instance using AWS Console as follows
We can see that EBS volume state has been changed from Available to In Use.
Let’s verify the storage of EC2 instance
Here , we can see that EBS volume has been successfully attached to our instance as a device name /dev/sdf
So, we saw how to create Key-pair, Security Group as well as launch EC2 Instance using AWS CLI commands.
Also , we were able to create an EBS Volume of size 1 GiB and attach to our Ec2 instance using AWS CLI commands.
So, this way we can create ’n’ number of Ec2 instances when there is a requirement to launch hundreds or even thousands of EC2 instances.
It saves a lot of time in such scenario as we don’t have to manually select all the options which we generally do while launching the instance in AWS Console.
Hope you like the article..!!