2

working code:

import boto3
def lambda_handler(event, context):
    ec2 = boto3.client('ec2')

    # Get list of regions
    regions = ec2.describe_regions().get('Regions',[] )

    # Iterate over regions
    for region in regions:
        print("*************** Checking region  --   %s " % region['RegionName'])
        reg = region['RegionName']
        print(reg)

Output:

*************** Checking region  --   eu-north-1 
eu-north-1
*************** Checking region  --   ap-south-1 
ap-south-1
*************** Checking region  --   eu-west-3 
eu-west-3
*************** Checking region  --   eu-west-2 
eu-west-2
*************** Checking region  --   eu-west-1

its iterating and showing all the regions but my code is simply exiting with first iteration after i try to describe the resource details.

import boto3
def lambda_handler(event, context):
    ec2 = boto3.client('ec2')

    # Get list of regions
    regions = ec2.describe_regions().get('Regions',[] )

    # Iterate over regions
    for region in regions:
        print("*************** Checking region  --   %s " % region['RegionName'])
        reg = region['RegionName']
        print(reg)
        print ("+++++++++++++ Starting EC2 Instances now -----------------") 
        client = boto3.client('ec2', region_name=reg)
        response = client.describe_instances()

output error out:

Response:
{
  "errorMessage": "2019-03-14T18:08:00.104Z 5fb67a9a-3bf9-40e3-ad56 Task timed out after 3.00 seconds"
}

Request ID:
"5fb67a9a-3bf9-40e3-ad56"

Function Logs:
START RequestId: 5fb67a9a-3bf9-40e3-ad56 Version: $LATEST
*************** Checking region  --   eu-north-1 
eu-north-1
+++++++++++++ Starting EC2 Instances now -----------------
*************** Checking region  --   ap-south-1 
ap-south-1
+++++++++++++ Starting EC2 Instances now -----------------
END RequestId: 5fb67a9a-3bf9-40e3-ad56
REPORT RequestId: 5fb67a9a-3bf9-40e3-ad56-Duration: 3003.21 ms  Billed Duration: 3000 ms    Memory Size: 128 MB Max Memory Used: 79 MB  
2019-03-14T18:08:00.104Z 5fb67a9a-3bf9-40e3-ad56-Task timed out after 3.00 seconds

i have given all permissions to lambda role to access the resources. Can anyone help me what wrong i'm doing and how to know what is the error?

2
  • 1
    The Lambda seems to be timing out, can you add a try/catch around the client.describe_instances() call? And maybe increase timeout a bit, see if you're getting any error Commented Mar 14, 2019 at 18:36
  • Its working after increasing timeout ...thanks Commented Mar 15, 2019 at 5:04

1 Answer 1

1

Your code iterated eu-north-1 and ap-south-1 successfully but then timed out after the default Lambda timeout of 3 seconds. You need to either make your code run faster or extend the Lambda timeout.

  1. Go to your Lambda console
  2. Find your function and open it
  3. Scroll down and look for Timeout under Basic settings
  4. Extend your timeout (15 minutes is the current maximum)
  5. Click Save on the top
Sign up to request clarification or add additional context in comments.

1 Comment

FYI, highly recommended not to increase it to the max timeout, if you run into an issue it might cause it to run for a lot longer than necessary. Glad it worked though :)

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.