Lambda function to tag all available snapshots

Following Lambda function will add a defined tag(s) to all available EBS snapshots.

Lambda function should be associated with an IAM Role with necessary permissions. E.g with EC2FullAccess permissions.


from __future__ import print_function

import json
import boto3
import logging

#setup simple logging for INFO
logger = logging.getLogger()
logger.setLevel(logging.ERROR)

#define the connection region
ec2 = boto3.resource('ec2', region_name="eu-west-2")
ec = boto3.client('ec2', 'eu-west-2')
snapshots = ec.describe_snapshots(MaxResults=1000,OwnerIds=['put account id here'])['Snapshots']

#Set this to True if you don't want the function to perform any actions
debugMode = False

def lambda_handler(event, context):
    for snapshot in snapshots:
        print('Sanpshot id '+snapshot['SnapshotId'])
        snapshotx = ec2.Snapshot(snapshot['SnapshotId'])
        snapshotx.create_tags(Tags=[{'Key': 'Tag1','Value': 'Value1'},{'Key': 'Tag2','Value': 'Value2'}])
    return 'complete'
                

Comments

Popular posts from this blog

Fixing Error "Failed to load VMMR0.r0 (VERR_SUPLIB_WORLD_WRITABLE)" in Virtualbox

Create new Java Class files at runtime

Using iReport plugin for Jasper Reports in Netbeans