-
Notifications
You must be signed in to change notification settings - Fork 0
/
lambda-back
38 lines (35 loc) · 1.38 KB
/
lambda-back
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
import json
import boto3
import time
from botocore.exceptions import ClientError
def lambda_handler(event, context):
try:
# EC2 Client
client = boto3.client('ec2', region_name='us-east-1')
# Get Volume ID of EBS attached to EC2 Instnace
response = client.describe_volumes()
if len(response['Volumes']) > 0:
for k in response['Volumes']:
print("EBS Volume ID : ",k['VolumeId'], " of EC2 Instance : ", k['Attachments'][0]['InstanceId'])
try:
# Create a Snapshot of Volume
responsesnapsnot = client.create_snapshot(VolumeId= k['VolumeId'])
print("Snapshot Created with ID : ", responsesnapsnot['SnapshotId'])
except Exception as e:
print("some error :", e)
return {
'statusCode': 200,
'body': json.dumps("sucess")
}
except ClientError as e:
print("Detailed error: ",e)
return {
'statusCode': 500,
'body': json.dumps("error")
}
except Exception as e:
print("Detailed error: ",e)
return {
'statusCode': 500,
'body': json.dumps("error")
}