In this blog, we will write a script to expand the AWS Ec2 instance filesystem after modifying the EBS volume size. In this script, we will use the Boto3 and Paramiko modules. Boto3 is an AWS Software Development Kit for Python using which we use various AWS services like EC2 and S3. Paramiko module helps us to establish Python SSH connections with servers.
We can divide the below script into the following steps:
- Listing the Instance and volume IDs present in a region.
- Modifying the volume size.
- Connecting to the EC2 instance.
- List the partitions and filesystems.
- Expand the partition and filesystem.
import boto3
import time
import paramiko
import colorama
from colorama import Fore
try:
region= input(Fore.BLUE+"Enter region name:"+Fore.RESET)
ec2_client = boto3.client('ec2', region_name=region)
response = ec2_client.describe_instances()
print(Fore.BLUE+f"\nList of instances and volumes in {region} region:"+Fore.RESET)
for reservation in response['Reservations']:
for instance in reservation['Instances']:
volumes = ec2_client.describe_volumes(
Filters=[{'Name':'attachment.instance-id','Values':[instance['InstanceId']]}]
)
for disk in volumes['Volumes']:
print(instance['InstanceId'], disk['VolumeId'], disk['VolumeType'], disk['Size'])
VOLUME_ID = input(Fore.BLUE+"\nEnter volume id:"+Fore.RESET)
Modified_volume_size = int(input(Fore.BLUE+"\nEnter new ebs volume size in GB:"+Fore.RESET))
def get_modification_state(volume_id):
response = ec2_client.describe_volumes_modifications(
VolumeIds=[
VOLUME_ID
]
)
return response['VolumesModifications'][0]['ModificationState']
modify_volume_response = ec2_client.modify_volume(
VolumeId=VOLUME_ID,
Size=Modified_volume_size
)
while True:
state = get_modification_state(VOLUME_ID)
if state == 'completed' or state == None:
break
elif state == 'failed':
raise Exception('Failed to modify volume size')
else:
time.sleep(60)
print(Fore.BLUE+f'\nVolume {VOLUME_ID} successfully resized'+Fore.RESET)
print("\nFor expanding filesystem, we need to SSH to EC2 server")
instanceip= input(Fore.BLUE+"\nEnter instance IP:"+Fore.RESET)
instance_ip=instanceip
key = paramiko.RSAKey.from_private_key_file("/path/to/keypairfile")
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname= instance_ip, username='ubuntu', pkey=key)
print(Fore.RED+"\nList of partition:\n"+Fore.RESET)
cmd = "lsblk"
stdin, stdout, stderr = client.exec_command(cmd)
stdout = stdout.readlines()
for line in stdout:
print(line)
print(Fore.RED+"List of filesystem in instance:\n"+Fore.RESET)
cmd = "df -T"
stdin, stdout, stderr = client.exec_command(cmd)
stdout = stdout.readlines()
for line in stdout:
print(line)
while True:
partition= input(Fore.RED+"\nEnter partition to expand:\n"+Fore.RESET)
cmd = f"sudo growpart {partition}"
stdin, stdout, stderr = client.exec_command(cmd)
exitcodeone=stdout.channel.recv_exit_status()
print(exitcodeone)
if exitcodeone==0:
stdout = stdout.readlines()
for line in stdout:
print(line)
break
elif exitcodeone==1:
print("Partition can not be extended")
else:
print("Invalid partition name")
while True:
fs=input(Fore.RED+"\nEnter filesystem type:"+Fore.RESET)
if fs=="ext4":
while True:
filesys= input(Fore.RED+"\nEnter filesystem to expand:"+Fore.RESET)
cmd = f"sudo resize2fs {filesys} "
stdin, stdout, stderr = client.exec_command(cmd)
exitcodetwo=stdout.channel.recv_exit_status()
if exitcodetwo==0:
stdout = stdout.readlines()
for line in stdout:
print(line)
break
else:
print("Invalid fliesystem")
break
elif fs=="xfs":
while True:
filesys= input(Fore.RED+"\nEnter filesystem to expand:"+Fore.RESET)
cmd = f"sudo xfs_growfs {filesys}"
stdin, stdout, stderr = client.exec_command(cmd)
exitcodethree=stdout.channel.recv_exit_status()
if exitcodethree==0:
stdout = stdout.readlines()
for line in stdout:
print(line)
break
else:
print("Invalid filesystem")
break
else:
print(Fore.RED+"Invalid filesystem type"+Fore.RESET)
client.close()
except Exception as e:
print (e)
For more information about product engineering, MVP development, Cloud management, Data Engineering. Please contact with our technical team.