Skip to content

Instantly share code, notes, and snippets.

@jmturwy
Created September 12, 2017 16:30
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jmturwy/8ecda3eb010a92af460f4e7a887d9e3d to your computer and use it in GitHub Desktop.
Save jmturwy/8ecda3eb010a92af460f4e7a887d9e3d to your computer and use it in GitHub Desktop.
AWS - AutoTag Snapshot's from Cloudwatch
{
"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"
}
]
}
{
"source": [
"aws.ec2"
],
"detail-type": [
"EBS Snapshot Notification"
],
"detail": {
"event": [
"createSnapshot"
],
"result": [
"succeeded"
]
}
}
# 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