Created
September 12, 2017 16:30
-
-
Save jmturwy/8ecda3eb010a92af460f4e7a887d9e3d to your computer and use it in GitHub Desktop.
AWS - AutoTag Snapshot's from Cloudwatch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"Version": "2012-10-17", | |
"Statement": [ | |
{ | |
"Action": [ | |
"cloudtrail:LookupEvents" | |
], | |
"Resource": [ | |
"*" | |
], | |
"Effect": "Allow", | |
"Sid": "Stmt1458923097000" | |
}, | |
{ | |
"Action": [ | |
"ec2:CreateTags", | |
"ec2:Describe*", | |
"logs:CreateLogGroup", | |
"logs:CreateLogStream", | |
"logs:PutLogEvents" | |
], | |
"Resource": [ | |
"*" | |
], | |
"Effect": "Allow", | |
"Sid": "Stmt1458923121000" | |
} | |
] | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"source": [ | |
"aws.ec2" | |
], | |
"detail-type": [ | |
"EBS Snapshot Notification" | |
], | |
"detail": { | |
"event": [ | |
"createSnapshot" | |
], | |
"result": [ | |
"succeeded" | |
] | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Create Lamda Function.Python2.7 | |
import boto3 | |
import json | |
def lambda_handler(event, context): | |
# Get volumeid and snapid from json | |
volumeid = str(json.dumps(event['detail']['source'])) | |
volumeid = volumeid.split('/')[1] | |
volumeid = volumeid.replace('"','') | |
snapid = str(json.dumps(event['detail']['snapshot_id'])) | |
snapid = snapid.split('/')[1] | |
snapid = snapid.replace('"','') | |
#Get Tags from VolumeID | |
ec2 = boto3.resource('ec2') | |
volume = ec2.Volume(volumeid) | |
v = volume.tags | |
# apply tags to snapshot | |
for tag in v: | |
response = ec2.create_tags( | |
Resources=[ | |
snapid, | |
], | |
Tags=[ | |
{ | |
'Key': tag['Key'], | |
'Value': tag['Value'], | |
}, | |
], | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment