0

I am new to python using boto3 for AWS. I am creating a lambda function that will return orphaned snapshots list. Code is -

def lambda_handler(event, context):
    ec2_resource = boto3.resource('ec2')
    
    # Make a list of existing volumes
    all_volumes = ec2_resource.volumes.all()
    volumes = [volume.volume_id for volume in all_volumes]
    
    # Find snapshots without existing volume
    snapshots = ec2_resource.snapshots.filter(OwnerIds=['self'])
    
    # Create list of all snapshots
    osl =[]
    
    for snapshot in snapshots:
        if snapshot.volume_id not in volumes:
            osl.append(snapshot)
            print('\n Snapshot ID is :-    '+str(snapshot))
            #snapshot.delete()
            continue
        for tag in snapshot.tags:
          if tag['Key'] == 'Name':
              value=tag['Value']
              print('\n Snapshot Tags are:- '+str(tag))
              break
    print('Total orphaned snapshots are:-    '+str(len(osl)))

This returns list of snapshots & tags too in incorrect format.

enter image description here

Surprisingly, when I run same code in another account, it gives lambda function error -

enter image description here

I have created same permissions IAM role. But different results in different accounts is something i am not gettting.

1 Answer 1

1

You are printing snapshot object but you want to print id. Second problem is all snapshot do not need to have tags, you need to check if snapshot have tags or not.

def lambda_handler(event, context):
    ec2_resource = boto3.resource('ec2')
    
    # Make a list of existing volumes
    all_volumes = ec2_resource.volumes.all()
    volumes = [volume.volume_id for volume in all_volumes]
    
    # Find snapshots without existing volume
    snapshots = ec2_resource.snapshots.filter(OwnerIds=['self'])
    
    # Create list of all snapshots
    osl =[]
    
    for snapshot in snapshots:
        if snapshot.volume_id not in volumes:
            osl.append(snapshot.id)
            print('\n Snapshot ID is :-    ' + snapshot.id)
    for snapshot in snapshots:
        if snapshot.volume_id not in volumes:
            osl.append(snapshot.id)
            print('\n Snapshot ID is :-    ' + snapshot.id)
            if snapshot.tags:
                for tag in snapshot.tags:
                    if tag['Key'] == 'Name':
                        value = tag['Value']
                        print('\n Snapshot Tags are '+ tag['Key'] + ", value = " + value)
                        break
    print('Total orphaned snapshots are:-    '+str(len(osl)))
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! I believe that id thing is what I missed. Also for -- Second problem is all snapshot do not need to have tags, you need to check if snapshot have tags or not -> added else to print no tags. Next step to check how to print tags below the ID itself
You need to move tags condition under volume check, check my answer with updated version.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.