-
-
Save jesperalmstrom/3da8d22fa7104f0a48f92f29d54f9c9d 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", | |
"ec2:CreateTags", | |
"ec2:Describe*", | |
"logs:CreateLogGroup", | |
"logs:CreateLogStream", | |
"logs:PutLogEvents" | |
], | |
"Resource": [ | |
"*" | |
], | |
"Effect": "Allow", | |
"Sid": "CloudWatchCreateSnapshotRole" | |
} | |
] | |
} |
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
import boto3 | |
import json | |
def lambda_handler(event, context): | |
# Get volumeid and snapid from json | |
print(event) | |
volume_id = str(json.dumps(event['detail']['source'])) | |
volume_id = volume_id.split('/')[1] | |
volume_id = volume_id.replace('"', '') | |
snapshot_id = str(json.dumps(event['detail']['snapshot_id'])) | |
snapshot_id = snapshot_id.split('/')[1] | |
snapshot_id = snapshot_id.replace('"', '') | |
# Get Tags from VolumeID | |
ec2 = boto3.resource('ec2') | |
volume = ec2.Volume(volume_id) | |
tags = volume.tags | |
print(snapshot_id) | |
print(tags) | |
# apply tags to snapshot | |
response = ec2.create_tags( | |
Resources=[ | |
snapshot_id, | |
], | |
Tags=tags | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment