Thursday, May 18, 2023
HomeSoftware EngineeringThe best way to Resize an AWS EBS Quantity in Bash

The best way to Resize an AWS EBS Quantity in Bash


If it’s essential to resize an EBS quantity in AWS, you are able to do so utilizing bash.

Step 1 – Create a bash file

Create a bash file referred to as resize.sh:

#!/bin/bash

SIZE=${1:-20}

INSTANCEID=$(curl http://169.254.169.254/newest/meta-data/instance-id)
REGION=$(curl -s http://169.254.169.254/newest/meta-data/placement/availability-zone | sed 's/(.*)[a-z]/1/')

VOLUMEID=$(aws ec2 describe-instances 
  --instance-id $INSTANCEID 
  --query "Reservations[0].Cases[0].BlockDeviceMappings[0].Ebs.VolumeId" 
  --output textual content 
  --region $REGION)

aws ec2 modify-volume --volume-id $VOLUMEID --size $SIZE

whereas [ 
  "$(aws ec2 describe-volumes-modifications 
    --volume-id $VOLUMEID 
    --filters Name=modification-state,Values="optimizing","completed" 
    --query "length(VolumesModifications)"
    --output text)" != "1" ]; do
sleep 1
carried out

if [[ -e "/dev/xvda" && $(readlink -f /dev/xvda) = "/dev/xvda" ]]
then
  sudo growpart /dev/xvda 1

  STR=$(cat /and many others/os-release)
  SUB="VERSION_ID="2""
  if [[ "$STR" == *"$SUB"* ]]
  then
    sudo xfs_growfs -d /
  else
    sudo resize2fs /dev/xvda1
  fi

else
  sudo growpart /dev/nvme0n1 1

  STR=$(cat /and many others/os-release)
  SUB="VERSION_ID="2""
  if [[ "$STR" == *"$SUB"* ]]
  then
    sudo xfs_growfs -d /
  else
    sudo resize2fs /dev/nvme0n1p1
  fi
fi

Step 2 – Run the bash file specifying the brand new measurement

Now that you’ve the bash file, you may run the bash file together with specifying the brand new measurement of the specified quantity:

bash resize.sh 50

The above command will try to resize the EBS quantity to 50GB.

Further Enhancements

In its place execution methodology, you too can change the bash file to be executable. It will assist you to name it immediately with out having to cross bash to the previous command.

First it’s essential to make the script and executable:

chmod +x resize.sh

Now you may merely run the file with the parameters required:

./resize.sh 20

That is doable as a result of the primary line of the file specifies the hashbang required to execute the code:

#!/bin/bash
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments