Launch AWS instance and attach volume using AWS CLI & script

Image for post
Image for post
AWS — Amazon Web Services

Goals or steps:

Let’s see some prerequisites

What is the AWS CLI?

What is AWS EC2 Service?

What is Amazon EBS (Elastic Block Storage)?

Now, Let’s jump to the project:

Part I of the project: Doing it manually

Step 1: Creating the key pairs from the command line

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
Image for post
Image for post
myclikey created successfully

Step 2: Creating the security group

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
Image for post
Image for post
security group created successfully

Step 3: Add the inbound rules to the security group

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

Step 4: Launching the EC2 instance

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

Step 5: Creating the EBS volume

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

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

aws ec2 describe-volumes  --query "Volumes[*].{ID:VolumeId,AZ:AvailabilityZone,Size:Size}" --filters "Name=tag:Name,Values=clivolume"
Image for post
Image for post
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

So, Let’s start the scripting part

#!/bin/bash#Declaring Variableskeyname=testingclikey
instancename=testinginstance
sgname=testingsg
ebsname=testingebs
#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

aws ec2 describe-security-groups --query "SecurityGroups[].GroupId" --filters Name=group-name,Values=$sgname
Image for post
Image for post
Image for post
Image for post
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 0.0.0.0/0
#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

Written by

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

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