Short script for creating and attaching an EBS Volume to an EC2 instance. This is useful in cases where you want to dynamically create your EBS volumes outside of the cloud formation auto provisioning process.
#!/usr/bin/env bash
echo -e "Creating Disk \n\tDevice ID: $1 \n\tMount Point: $2 \n\tKMS-ID: $3 \n\tSize: $4 \n\tType: $5 \n\tTags: $6"
availability_zone=`curl http://169.254.169.254/latest/meta-data/placement/availability-zone`
region="`echo \"$availability_zone\" | sed -e 's:\([0-9][0-9]*\)[a-z]*\$:\\1:'`"
instanceId=`curl http://169.254.169.254/latest/meta-data/instance-id`
deviceId="$1"
mountPoint="$2"
kmsId="$3"
sizeGb=$4
volType="$5"
additionalTags="$6"
if [ ! -f /root/.aws/config ]; then
mkdir -p /root/.aws
echo -e "[default]\nregion = ${region}" > /root/.aws/config
fi;
tags="ResourceType=volume,Tags=[${additionalTags}]"
echo "Tags: ${tags}"
volumeCmd="aws ec2 create-volume --region ${region} --availability-zone ${availability_zone} --encrypted --kms-key-id ${kmsId} --size ${sizeGb} --volume-type ${volType} --tag-specifications ${tags}"
echo "Executing: ${volumeCmd}"
volume=`${volumeCmd}`
volumeId=`echo ${volume}|jq '.VolumeId'`
volumeId="${volumeId%\"}"
volumeId="${volumeId#\"}"
echo "Volume:ID ${volumeId}"
if [ -z ${volumeId+x} ]; then
echo "Could not create drive"
exit -1;
fi
echo "Executing: aws ec2 wait volume-available --volume-ids ${volumeId}"
aws ec2 wait volume-available --volume-ids ${volumeId}
echo "Executing: ec2 attach-volume --volume-id ${volumeId} --instance-id ${instanceId} --device ${deviceId}"
aws ec2 attach-volume --volume-id ${volumeId} --instance-id ${instanceId} --device ${deviceId}
echo "Executing: while [ ! -e ${deviceId} ]; do echo Waiting for EBS volume to attach; sleep 5; done"
while [ ! -e ${deviceId} ]; do echo Waiting for EBS volume to attach; sleep 5; done
echo "Executing: mkfs -t ext4 ${deviceId}"
mkfs -t ext4 ${deviceId}
echo "${deviceId} ${mountPoint} ext4 defaults,nofail 0 0" >> /etc/fstab
mkdir -p ${mountPoint}
mount ${mountPoint}
2 responses to “Short script to create and attach an EBS Volume in AWS”
Hi this is not working when i am running from AWS instance to launch EBS instance and attach the same to instance..error i am getting is
Creating Disk
Device ID:
Mount Point:
KMS-ID:
Size:
Type:
Tags:
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 10 100 10 0 0 10917 0 –:–:– –:–:– –:–:– 10000
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 19 100 19 0 0 23370 0 –:–:– –:–:– –:–:– 19000
Tags: ResourceType=volume,Tags=[testing_ebs]
Executing: aws ec2 create-volume –region eu-west-1 –availability-zone eu-west-1a –size 40 –volume-type gp2 –tag-specifications ResourceType=volume,Tags=[testing_ebs]
./ebs.sh: line 21: aws: command not found
./ebs.sh: line 22: jq: command not found
Volume:ID
Executing: aws ec2 wait volume-available –volume-ids
./ebs.sh: line 32: aws: command not found
Executing: ec2 attach-volume –volume-id –instance-id i-0b10df31a59d7576d –device /dev/sdf
./ebs.sh: line 34: aws: command not found
Executing: while [ ! -e /dev/sdf ]; do echo Waiting for EBS volume to attach; sleep 5; done
Waiting for EBS volume to attach
Waiting for EBS volume to attach
Waiting for EBS volume to attach
Waiting for EBS volume to attach
Waiting for EBS volume to attach
Waiting for EBS volume to attach
Waiting for EBS volume to attach
Waiting for EBS volume to attach
Waiting for EBS volume to attach
Waiting for EBS volume to attach
Waiting for EBS volume to attach
Waiting for EBS volume to attach
Kindly let
LikeLike
Your EC2 instance will need aws client tools, and the jq CLI utility, and an IAM role capable of executing these command
LikeLike