Launch AWS instance and attach volume using AWS CLI & script

AWS — Amazon Web Services

Goals or steps:

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

Let’s see some prerequisites

For doing this task, you need to know about AWS public Cloud, AWS CLI program, AWS ec2 service, etc. So, let’s take the above things first:

What is the AWS CLI?

The AWS Command Line Interface (CLI) is a unified tool to manage your 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. For more info:

What is AWS EC2 Service?

Amazon Elastic Compute Cloud (Amazon EC2) is a web service that provides secure, resizable compute capacity in the cloud. For more info:

What is Amazon EBS (Elastic Block Storage)?

Amazon Elastic Block Store (Amazon EBS) provides block-level storage volumes for use with EC2 instances. For more info:

Now, Let’s jump to the project:

Note: Github repo where the code is available:

Part I of the project: Doing it manually

Step 1: Creating the key pairs from the command line

To create the key-pair from the AWS CLI, we will be using the AWS command. Key-pair comes under the umbrella of EC2 services. You can also check all the commands and services under AWS EC2 using the following command:

aws ec2 help
aws ec2 create-key-pair --key-name myclikey --tag-specification ResourceType=key-pair,Tags=[{Key=Name,Value=myclikey}] --query "KeyMaterial" --output text > myclikey.pem
aws ec2 describe-key-pairs
myclikey created successfully

Step 2: Creating the security group

Security groups in the AWS act as the virtual firewall and help secure the instance or other resources that you have created in the AWS.

aws ec2 create-security-group --group-name mycligrp --description "created from CLI"  --vpc-id vpc-50f0e456
aws ec2 create-security-group help
aws ec2 describe-security-groups
security group created successfully

Step 3: Add the inbound rules to the security group

For allowing the specific IP or allowing the specific port connection from the outside world, you need to add the inbound rules in the security group. For instance, for allowing everyone from the outside world to be able to access the resource, but only in port 22 (which is used for ssh), you can run the following command:

aws ec2 authorize-security-group-ingress --group-id sg-0186840bb87671260 --protocol tcp --port 22 --cidr
aws ec2 describe-security-groups

Step 4: Launching the EC2 instance

For launching the EC2 instance, you can use the following command:

aws ec2 run-instances --image-id ami-0e306788ff2473ccb --count 1 --instance-type t2.micro --key-name myclikey --security-group-ids sg-0186840bb87671260

Step 5: Creating the EBS volume

To create the EBS volume, you can make use of the create-volume command available in AWS CLI. Along with this command, you need to provide the availability zone that you want to use. So, here, you must need to specify the same availability zone as that of your instance’s AZ because only then, you will be able to attach the volume to the instance.

aws ec2 create-volume --availability-zone ap-south-1a --size 1 --tag-specification ResourceType=volume,Tags=[{Key=Name,Value=clivolume}]

Step 6: Attaching that EBS Volume to the EC2 instance that we have already created

For attaching the volume, you need the device name and instance-id to which you need to attach the volume and obviously the volume-id of the volume that is to be attached to the instance.

aws ec2 describe-volumes  --query "Volumes[*].{ID:VolumeId,AZ:AvailabilityZone,Size:Size}" --filters "Name=tag:Name,Values=clivolume"
For getting the volume id, which we have created
aws ec2 attach-volume --device /dev/xvdb --instance-id i-0159851f2556c1a04 --volume-id vol-0536d4b73a5937242

Part II — Automating the things using scripting

Tip: To read the full code at once, go to the end of the article

So, Let’s start the scripting part

The first thing, you need to do is to declare the variables for the tags or the name of resources.

#!/bin/bash#Declaring Variableskeyname=testingclikey
#creating the key pairaws ec2 create-key-pair --key-name $keyname --tag-specification "ResourceType=key-pair,Tags=[{Key=Name,Value=$keyname}]" --query "KeyMaterial" --output text > $keyname.pem
#Creating the security groupaws ec2 create-security-group --group-name $sgname --description "created from CLI"  --vpc-id vpc-50f0e456

Now, the heart of Scripting

Till now, everything was really fine, but when you are going to create the inbound rule for the security group, you need to retrieve the group-id of the security group that you have created and then pass that group-id to another command.

aws ec2 describe-security-groups --query "SecurityGroups[].GroupId" --filters Name=group-name,Values=$sgname
sgid=`aws ec2 describe-security-groups  --query "SecurityGroups[].GroupId" --filters "Name=group-name,Values=$sgname" | sed -n 2p | tr -d \"`
#Adding the inbound rules to the security group createdaws ec2 authorize-security-group-ingress --group-id $sgid --protocol tcp --port 22 --cidr
#Launching the instanceaws ec2 run-instances --image-id ami-0e306788ff2473ccb --count 1 --instance-type t2.micro --key-name $keyname --security-group-ids $sgid --tag-specification "ResourceType=instance,Tags=[{Key=Name,Value=$instancename}]"
#Creating the Volume
aws ec2 create-volume --availability-zone ap-south-1a --size 1 --tag-specification "ResourceType=volume,Tags=[{Key=Name,Value=$ebsname}]"
#storing the value of Volume ID in variable volumeID
volumeID=`aws ec2 describe-volumes --query "Volumes[*].VolumeId" --filters "Name=tag:Name,Values=$ebsname" | sed -n 2p | tr -d \"`
#Storing the value of instance ID in the Variable instanceid
instanceID=`aws ec2 describe-instances --query "Reservations[*].Instances[].InstanceId" --filter "Name=key-name,Values=$keyname" | sed -n 2p | tr -d \"`
#Attaching the volume to the instance
aws ec2 attach-volume --device /dev/xvdb --instance-id $instanceID --volume-id $volumeID

Hey Everone, I am DevOps Practitioner, Cloud Computing, BigData, Machine Learning are my favorite parts. Connect me on LinkedIn to know more about me.