-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlambda_function.py
107 lines (90 loc) · 3.14 KB
/
lambda_function.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import boto3
def get_volume_id_from_arn(volume_arn):
# Split the ARN string using the ':' separator
parts = volume_arn.split(':')
# Get the last part of the ARN
last_part = parts[-1]
# Split the last part using the '/' separator and get the volume ID
volume_id = last_part.split('/')[-1]
return volume_id
def lambda_handler(event, context):
print(f"Event received: {event}")
try:
resource_arn = event['resources'][0]
except KeyError:
return {
'statusCode': 400,
'body': 'Invalid event format: missing "resources".'
}
resource_id = resource_arn.split('/')[-1]
client = boto3.client('ec2', region_name='eu-north-1')
# Check if the resource is a volume or a snapshot
if 'volume' in resource_arn:
volume_id = get_volume_id_from_arn(resource_arn)
try:
response = client.modify_volume(
VolumeId=volume_id,
VolumeType='gp3',
)
print(f"Modify volume response: {response}")
except Exception as e:
return {
'statusCode': 500,
'body': f'Failed to modify volume: {str(e)}'
}
# Create the snapshot and add the desired tag in one step
tag_key = 'Description'
tag_value = 'This is a test snapshot creation by Salman'
try:
response_snapshot = client.create_snapshot(
VolumeId=volume_id,
Description='Snapshot created by Lambda function',
TagSpecifications=[
{
'ResourceType': 'snapshot',
'Tags': [
{
'Key': tag_key,
'Value': tag_value
},
]
},
]
)
print(f"Create snapshot response: {response_snapshot}")
except Exception as e:
return {
'statusCode': 500,
'body': f'Failed to create snapshot: {str(e)}'
}
snapshot_id = response_snapshot['SnapshotId']
elif 'snapshot' in resource_arn:
snapshot_id = resource_id
# Add the desired tag to the snapshot
tag_key = 'Description'
tag_value = 'This is a test snapshot creation by Salman'
try:
response = client.create_tags(
Resources=[snapshot_id],
Tags=[
{
'Key': tag_key,
'Value': tag_value
},
]
)
print(f"Create tags response: {response}")
except Exception as e:
return {
'statusCode': 500,
'body': f'Failed to create tag: {str(e)}'
}
else:
return {
'statusCode': 400,
'body': 'Invalid resource type. This function only supports volumes and snapshots.'
}
return {
'statusCode': 200,
'body': 'Operation completed successfully.'
}