0
votes

I am using boto3, for list all snapshots. But when I use function ec2.snapshots.all(), and then try iterate with a for, it's not working.

from __future__ import print_function
from boto3 import resource

REGION = "us-east-1"

def lambda_handler(event, context):
    ec2 = resource("ec2", region_name=REGION)
    all_snapshots = ec2.snapshots.all()
    for snapshot in all_snapshots:
        print("SnapshotIteration")

The result is the next:

{
  "errorMessage": "2017-03-07T00:08:56.583Z 3faed105-02ca-11e7-b637-cfb921e4e3cc Task timed out after 3.00 seconds"
}
1
How many snapshots do you have? I guess it is way too many. What are you trying to do?helloV
I agree with @helloV - how long does it take to run "aws ec2 describe-snapshots" on the command line for you? We have quite a few and it took about 15 seconds for that to come back. Why is your timeout only 3 seconds?stdunbar
I did not know that the function returned all public snapshots. I had tested by increasing the timeout to 30 seconds but obviously not enough. I applied a filter indicating my owner-id as indicated by John Rotenstein and now it works.Jonathan Cordero Duarte

1 Answers

0
votes

I suspect it is listing all publicly-available snapshots, which can be quite a lot. It is better to limit it to snapshots for which you are the owner.

The describe-snapshots() function takes an OwnerIds parameter that can scope-down the return value.

Here's an example from the boto3 snapshot documentation that shows how to use a filter on the snapshots iterator:

snapshot_iterator = ec2.snapshots.filter(
    DryRun=True|False,
    SnapshotIds=[
        'string',
    ],
    OwnerIds=[
        'string',
    ],
    RestorableByUserIds=[
        'string',
    ],
    Filters=[
        {
            'Name': 'string',
            'Values': [
                'string',
            ]
        },
    ],
    NextToken='string',
    MaxResults=123
)

That might help.