Resize & Delete EC2 Instance on AWS vendor how to execute a script in Linux

This tutorial shows steps to write and execute a script in Linux using the Terminal app.

I/ Steps resize instance by executing a script in Linux

The procedure is as follows:

1.Open the terminal app by clicking on SSH button in Connect column each EC2 instance resource on page detail service EC2 Underutilized

<bổ sung ảnh khi làm xong FE>

2. With OS compatibility of Instance is Linux or Suse Linux or Red hat, you create a file .sh the following code:

#!/bin/bash
vmSize=$1

volumeId=$2

xvdaPos=$3
#resize the volume os

aws configure

aws ec2 modify-volume --size $vmSize --volume-id $volumeId
# sleep 8 seconds

sleep 8

#update the volume

sudo df -h

sudo growpart /dev/xvda $xvdaPos

sudo xfs_growfs -d /

# call validate memory after update

sudo df -h

3. Write the script using the following code:

cd .ssh/

nano <file_name> (COPY INFORMATION OF FILE .PEM)

then, press enter

sudo ssh -i "<file_name>" (2)

With:

file_name: Name of file Keypair correspond with instance is resizing.

COPY INFORMATION OF FILE .PEM: File Keypair created when creating key pair on AWS vendor

Go to EC2 instance on AWS website => View detail instance => Choose Connect (Instance is running)

Copy the code in (2) in the image below

You login successfully on server of this instance.

4. Execute the command to support resize

nano <file_name> .sh <copy code in step 2>

With: <file_name>: File name created in step 2

Call command-line lsblk for get position of xvdaPos

5. Execute resize instance with command:

sh direct_resize_vm_linux_aws.sh $vmSize $volumeId $xvdaPos

With:

vmSize: size of the VM that we want to update

volumeId: volume-id of the VM xvdaPos: position of the partition that we want update size

xvdaPos: position of the partition that we want update size

E.g: you will update size of the VM 55G to 88G by

sh direct_resize_vm_linux_aws.sh 88 vol-0c4acb6427ad86b69 1

Output:

II/ Steps delete instance by executing a script in Linux

The procedure is as follows:

1.You create a file .sh the following code:

#!/bin/bash
instanceId=$1
aws configure

aws ec2 terminate-instances --instance-ids $instanceId

2. Run the script with the format:

nano <file_name> .sh (COPY CODE IN STEP 1)

then, press Enter

Run command delete instance with format:

sh <file_name> .sh $instanceId

With:

<file_name>: File name created in step 1

InstanceId: instanceId of EC2

E.g: you can delete EC2

Then, you copy script to the server Linux before run script

sh delete_vm_linux_window_aws.sh i-0f7eceb36a34f8bf5

Output:

Leave a Reply