Amazon Web Services: “How to launch Instances & attaching additional EBS Volume using AWS CLI 2?”​

Before getting started? First, let me tell you the concept “CLI”. The CLI stands for Command Line Interface which allows users to directly interact with the particular program or system using “Black Screen”. Black Screen is also known as “Shell”. It prompt user to input something or commands to do some operations. The AWS Command Line Interface (CLI) is a unified tool to manage AWS services. With just one tool to download and configure, you can control multiple AWS services from the command line and automate them through scripts.

Installation of Amazon Web Services Command Line Interface (AWS CLI) 2

The AWS Command Line Interface has two different versions: 1 & 2.

The AWS CLI version 2 is the most recent major version of the AWS CLI and supports all of the latest features.

Learn more about AWS CLI 2 & Download it from here

Creating User for CLI using IAM Service

To enable access of AWS Services from CLI, we need to create specific account for CLI Access from service called “IAM”.

In Identity and Access Management, we need to add new user for specific permission for this operation. In IAM Dashboard, under Access Management, we need to click on Users and the steps are following:

After creating the user, the IAM Service will provide two major things, the “Access key ID” and “Secret access key”. We can download it as CSV file for future use.

Configuration of “AWS CLI 2”

After the successful installation of AWS CLI 2, we need to open the console or command prompt in Operating System.

Steps to configure the AWS CLI 2

Creating Key Pairs for EC2 Instances

To Create Key Pairs using CLI, we need to run this command

aws ec2 create-key-pair --key-name shobhitKeyPair

Creating Security Group

To Create Security Groups using CLI, we need to run this command

aws ec2 create-security-group --group-name shobhitFirewall --description "Shobhit Sharma, Security Group"

The output will be

{
"GroupId": "sg-0e6e60daXXXXXXXXX"
}

Creating New Amazon Linux Instance using AWS CLI 2

Before creating the instance using CLI, we need to collect some of the information either WebUI or references of Amazon Web Services.

To Create Instance using CLI, we need to run this command

aws ec2 run-instances --image-id ami-03657b56516ab7912 --count 1 --instance-type t2.micro --key-name shobhitKeyPair --security-group-ids sg-0e6e60daXXXXXXXXX --subnet-id subnet-ffeXXXXX

Output will be

{
"Groups": [],
"Instances": [
{
"AmiLaunchIndex": 0,
"ImageId": "ami-03657b56516ab7912",
"InstanceId": "i-01521fd1XXXXXXXXX",
"InstanceType": "t2.micro",
"KeyName": "shobhitKeyPair",
"LaunchTime": "2020-10-13T07:04:07+00:00",
"Monitoring": {
"State": "disabled"
},
"Placement": {
"AvailabilityZone": "us-east-2a",
"GroupName": "",
"Tenancy": "default"
},
"PrivateDnsName": "ip-172-31-0-218.us-east-2.compute.internal",
"PrivateIpAddress": "172.31.0.218",
"ProductCodes": [],
"PublicDnsName": "",
"State": {
"Code": 0,
"Name": "pending"
},
"StateTransitionReason": "",
"SubnetId": "subnet-ffXXXXXX",
"VpcId": "vpc-81XXXXXX",
"Architecture": "x86_64",
"BlockDeviceMappings": [],
"ClientToken": "aff01bea-7b9f-4560-a8eb-af0XXXXXXXXX",
"EbsOptimized": false,
"EnaSupport": true,
"Hypervisor": "xen",
"NetworkInterfaces": [
{
"Attachment": {
"AttachTime": "2020-10-13T07:04:07+00:00",
"AttachmentId": "eni-attach-004f52c3XXXXXXXXX",
"DeleteOnTermination": true,
"DeviceIndex": 0,
"Status": "attaching"
},
"Description": "",
"Groups": [
{
"GroupName": "shobhitFirewall",
"GroupId": "sg-0e6e60daXXXXXXXXX"
}
],
"Ipv6Addresses": [],
"MacAddress": "02:73:cc:00:26:58",
"NetworkInterfaceId": "eni-0c2d1d44XXXXXXXXX",
"OwnerId": "934204XXXXXX",
"PrivateDnsName": "ip-172-31-0-218.us-east-2.compute.interna
l",
"PrivateIpAddress": "172.31.0.218",
"PrivateIpAddresses": [
{
"Primary": true,
"PrivateDnsName": "ip-172-31-0-218.us-east-2.compute
.internal",
"PrivateIpAddress": "172.31.0.218"
}
],
"SourceDestCheck": true,
"Status": "in-use",
"SubnetId": "subnet-ffXXXXXX",
"VpcId": "vpc-81d77eea",
"InterfaceType": "interface"
}
],
"RootDeviceName": "/dev/xvda",
"RootDeviceType": "ebs",
"SecurityGroups": [
{
"GroupName": "shobhitFirewall",
"GroupId": "sg-0e6e60daXXXXXXXXX"
}
],
"SourceDestCheck": true,
"StateReason": {
"Code": "pending",
"Message": "pending"
},
"VirtualizationType": "hvm",
"CpuOptions": {
"CoreCount": 1,
"ThreadsPerCore": 1
},
"CapacityReservationSpecification": {
"CapacityReservationPreference": "open"
},
"MetadataOptions": {
"State": "pending",
"HttpTokens": "optional",
"HttpPutResponseHopLimit": 1,
"HttpEndpoint": "enabled"
}
}
],
"OwnerId": "934204XXXXXX",
"ReservationId": "r-079af78eXXXXXXXXX"
}

After creation of instance, we need to run this optional command for identification.

aws ec2 create-tags --resources i-01521fd1XXXXXXXXX --tags Key=Name,Value="Shobhit Sharma CLI"

Creating EBS Additional Volume

Before creating EBS Volume, we need to check the availability zone first.

To Create EBS Volume using CLI, we need to run this command

aws ec2 create-volume --size 1 --availability-zone us-east-2a

The output will be

{
"AvailabilityZone": "us-east-2a",
"CreateTime": "2020-10-13T07:26:05+00:00",
"Encrypted": false,
"Size": 1,
"SnapshotId": "",
"State": "creating",
"VolumeId": "vol-00abe3aaXXXXXXXXX",
"Iops": 100,
"Tags": [],
"VolumeType": "gp2"
}

Attaching Additional EBS Volume to the EC2 Instance

To Attach EBS Volume to the EC2 Instance using CLI, we need to run this command “

aws ec2 attach-volume --volume-id vol-00abe3aaXXXXXXXXX --instance-id i-01521fd1XXXXXXXXX --device /dev/sdf

The output will be

{
"AttachTime": "2020-10-13T07:34:55.869000+00:00",
"Device": "/dev/sdf",
"InstanceId": "i-01521fd1XXXXXXXXX",
"State": "attaching",
"VolumeId": "vol-00abe3aaXXXXXXXXX"
}

This is how we can create CLI User, key pairs, security group, instance, additional EBS Volume and also how can we attach additional EBS Volume to the instance.

This Article is originally written, edited and published by Shobhit Sharma.

Shobhit Sharma (born 8 December 2000) is an Indian Technology Journalist, Computer Engineer, EDM Artist, Blogger, and Entrepreneur from Agra, Uttar Pradesh.