Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit be455a8

Browse filesBrowse files
author
100daysofdevops
committed
adding code for ec2 instance conversion and ebs volume conversion
1 parent 5c796f4 commit be455a8
Copy full SHA for be455a8

4 files changed

+167Lines changed: 167 additions & 0 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎ebs_volume_conversion/README.md‎

Copy file name to clipboard
+35Lines changed: 35 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Python script to convert EBS Volume
2+
3+
Python script to convert EBS volume from gp2 to gp3 or from io1 to io2 and read input from csv file with a format volume_id,volume_type
4+
5+
## Usage
6+
7+
```python3 ebs_volume_conversion.py -h
8+
usage: ebs_volume_conversion.py [-h] [-f FILE] volume_id volume_type
9+
10+
positional arguments:
11+
volume_id The EBS volume id of the Volume to be convert
12+
volume_type The new EBS volume type to be converted gp3 or io2
13+
14+
optional arguments:
15+
-h, --help show this help message and exit
16+
-f FILE, --file FILE A csv file containing ebs volume id and volume type to convert
17+
```
18+
19+
# Example
20+
21+
1. To convert an ebs volume from gp2 to gp3
22+
23+
```
24+
python3 ebs_volume_conversion.py -volume_id <vol-id> -volume_type gp3
25+
```
26+
27+
2. To convert an ebs volume from io1 to io2
28+
```
29+
python3 ebs_volume_conversion.py -volume_id <vol-id> -volume_type io2
30+
```
31+
32+
3. You can pass a csv file containing ebs volume id and volume type to convert
33+
```
34+
python3 ebs_volume_conversion.py -f ebs_volume.csv
35+
```
Collapse file
+54Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#!/usr/bin/env python3
2+
# Python script to convert EBS volume from gp2 to gp3 or from io1 to io2 and read input from csv file with a format volume_id,volume_type
3+
4+
import boto3
5+
import argparse
6+
import csv
7+
import sys
8+
9+
def convert_ebs_vol(volume_id,volume_type):
10+
# Create an EC2 client
11+
ec2 = boto3.client('ec2')
12+
# Ask user if he want to take the snapshot before volume conversion
13+
snapshot = input(f'Do you want to take the snapshot of the following volume {volume_id} before conversion? (y/n)')
14+
# Take the snapshot if user confirms
15+
if snapshot.lower() == 'y':
16+
try:
17+
snapshot = ec2.create_snapshot(VolumeId=volume_id,Description="Snapshot before converting the EBS volume")
18+
# Wait for the snapshot to be completed
19+
waiter = ec2.get_waiter('snapshot_completed')
20+
waiter.wait(SnapshotIds=[snapshot['SnapshotId']])
21+
print(f'Sucessfully created the snapshot {snapshot["SnapshotId"]} of volume {volume_id}')
22+
except Exception as e:
23+
print(f'An error occurred while creating the snapshot for volume {volume_id}: {e} ')
24+
exit(1)
25+
try:
26+
# Modify the EBS Volume to the new volume type
27+
ec2.modify_volume(VolumeId=volume_id, VolumeType=volume_type)
28+
print(f'Started converting EBS volume {volume_id} to {volume_type}')
29+
# Wait until the EBS volume is available
30+
ec2.get_waiter('volume_available').wait(VolumeIds=[volume_id])
31+
print(f'Sucessfully converted EBS volume {volume_id} to {volume_type}')
32+
except Exception as e:
33+
print(f'An error occurred while converting the EBS volume {volume_id}: {e} ')
34+
exit(1)
35+
36+
if __name__ == "__main__":
37+
parser = argparse.ArgumentParser()
38+
parser.add_argument("-volume_id", help="The EBS volume id of the Volume to be convert")
39+
parser.add_argument("-volume_type", help="The new EBS volume type to be converted gp3 or io2")
40+
parser.add_argument("-f","--file", help="A csv file containing ebs volume id and volume type to convert")
41+
args = parser.parse_args()
42+
if not any(vars(args).values()):
43+
parser.print_help()
44+
sys.exit()
45+
46+
if args.file:
47+
with open(args.file, 'r') as f:
48+
reader = csv.reader(f)
49+
for row in reader:
50+
volume_id = row[0]
51+
volume_type = row[1]
52+
convert_ebs_vol(volume_id, volume_type)
53+
else:
54+
convert_ebs_vol(args.volume_id, args.volume_type)
Collapse file

‎ec2_instance_conversion/README.md‎

Copy file name to clipboard
+35Lines changed: 35 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Python script to convert EC2 instance
2+
3+
Python script to convert EC2 instance from one type to another and read input from csv file with a format instance_id,instance_type
4+
5+
6+
## Usage
7+
8+
```python3 ec2_instance_conversion.py
9+
usage: ec2_instance_conversion.py [-h] [-instance_id INSTANCE_ID]
10+
[-new_instance_type NEW_INSTANCE_TYPE] [-f FILE]
11+
12+
Convert an EC2 instance from one type to another
13+
14+
optional arguments:
15+
-h, --help show this help message and exit
16+
-instance_id INSTANCE_ID
17+
The ID of the EC2 instance to convert
18+
-new_instance_type NEW_INSTANCE_TYPE
19+
The new instance type for the EC2 instance
20+
-f FILE, --file FILE A CSV file containing a list of instances and new instance types
21+
```
22+
23+
# Example
24+
25+
1. To convert an EC2 instance to t2.medium
26+
27+
```
28+
python3 ec2_instance_conversion.py -instance_id <instance-id> -new_instance_type t2.medium
29+
```
30+
31+
32+
3. You can pass a csv file containing instance id and instance type to convert
33+
```
34+
python3 ec2_instance_conversion.py -f ec2_conversion.csv
35+
```
Collapse file
+43Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/env python3
2+
# Python script to convert EC2 instance from one type to another and read input from csv file with a format instance_id,instance_type
3+
4+
import argparse
5+
import boto3
6+
import sys
7+
import csv
8+
9+
def convert_instance(instance_id, new_instance_type, csv_file=None):
10+
# Create an EC2 client
11+
ec2 = boto3.client('ec2')
12+
13+
# Check if the instance is in a stopped state
14+
response = ec2.describe_instances(InstanceIds=[instance_id])
15+
instance_state = response['Reservations'][0]['Instances'][0]['State']['Name']
16+
if instance_state == 'stopped':
17+
snapshot_response = input("The instance is in a stopped state. Please don't forget to take the ebs snapshot of the instance volume before the conversion y/n")
18+
try:
19+
# Modify the instance
20+
ec2.modify_instance_attribute(InstanceId=instance_id, Attribute='instanceType', Value=new_instance_type)
21+
print("Successfully converted instance", instance_id, "to", new_instance_type)
22+
except Exception as e:
23+
print("Error converting instance:", e)
24+
25+
if __name__ == '__main__':
26+
# Set up the argument parser
27+
parser = argparse.ArgumentParser(description='Convert an EC2 instance from one type to another')
28+
parser.add_argument('-instance_id', help='The ID of the EC2 instance to convert')
29+
parser.add_argument('-new_instance_type', help='The new instance type for the EC2 instance')
30+
parser.add_argument("-f","--file", help='A CSV file containing a list of instances and new instance types')
31+
args = parser.parse_args()
32+
if not any(vars(args).values()):
33+
parser.print_help()
34+
sys.exit()
35+
if args.file:
36+
with open(args.file, 'r') as f:
37+
reader = csv.reader(f)
38+
for row in reader:
39+
instance_id = row[0]
40+
new_instance_type = row[1]
41+
convert_instance(instance_id, new_instance_type)
42+
else:
43+
convert_instance(args.instance_id, args.new_instance_type)

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.