AWS Lambda function using test trigger or schedule (Timeout)
<p>So I have created a lambda function (the purpose of which doesn't matter anymore) and have tested that it works when run from my laptop.  However the bigger problem is that I cannot get it to run off of a test event or on a schedule in AWS. </p>

<p>When I try to run it from AWS I get a 300s timeout error. </p>

<p>The following is included for your consideration:</p>

<ul>
<li>Function</li>
<li>Logs</li>
<li>Trigger Event</li>
<li>Policy</li>
<li>VPC Related Configuration</li>
</ul>

<p>If anyone can tell me what the issue might be, I would appreciate it as I have been searching for the solution for about 3 days.</p>

<p>FUNCTION:</p>

<pre><code>from __future__ import print_function

def lambda_handler(event, context):
    if event["account"] == "123456789012":
        print("Passed!!")
    return event['time']


import boto3
import datetime

def find_auto_scaling_instances():
    """
    Find Auto-scaling Instances
    """
    client=boto3.client("autoscaling")
    auto_scaling=client.describe_auto_scaling_groups()
    dict={}
    for group in auto_scaling["AutoScalingGroups"]:
        print("hello")
        auto_scaling_group_name=group["AutoScalingGroupName"]
        number_of_instances=len(group["Instances"])
        if number_of_instances == 0:
            continue
        else:
            for i in range(number_of_instances):
                if group["Instances"][i]["LifecycleState"] == "InService":
                    instance_id=group["Instances"][i]["InstanceId"]
                    dict[auto_scaling_group_name]=instance_id
                    break
    return dict

def find_staging_instances():
    """
    Find Static Instances
    """
    stg_list=[]
    tag_list=["Y-qunie-stepsrv-1a","S-StepSvr"]
    for i in range(num_of_instances):
        print("hello2")
        target=instances[i]["Instances"][0]
        number_of_tags=len(target["Tags"])
        for tag in range(number_of_tags):
            if target["Tags"][tag]["Value"] in tag_list:
                stg_list+=[target["InstanceId"]]

    return stg_list

def volumes_per_instance():
    """
    Find the EBS associated with the Instances
    """
    instance_disk={}
    for i in range(num_of_instances):
        print("hello3")
        target=instances[i]["Instances"][0]
        if target["InstanceId"] in instance_list:
            instance_disk[target["InstanceId"]]=[]
            for disk in range(len(target["BlockDeviceMappings"])):
                print("hello4")
                instance_disk[target["InstanceId"]]+=\
                target["BlockDeviceMappings"][disk]["Ebs"]["VolumeId"]]

    return instance_disk

#Group instances together and prepare to process
instance_in_asgroup_dict=find_auto_scaling_instances()

as_instance_list=[]
for group in instance_in_asgroup_dict:
    print("hello5")
    as_instance_list+=[instance_in_asgroup_dict[group]]

client=boto3.client("ec2")
instances=client.describe_instances()["Reservations"]
num_of_instances=len(instances)

staging_instances=find_staging_instances()

instance_list=[]
instance_list+=as_instance_list
instance_list+=staging_instances

#Gather Disk Information
inst_disk_set=volumes_per_instance()

date=str(datetime.datetime.now()).replace(" \
","_").replace(":","").replace(".","")

#Create Disk Images
as_image={}
image=[]
for instance in instance_list:
    print("hello6")
    if instance in as_instance_list:
        as_image[instance]=client.create_image(
            InstanceId=instance,
            Name=instance+"-"+date+"-AMI",
            Description="AMI for instance "+instance+".",
            NoReboot=True
        )
    else:
        image+=[client.create_image(
            InstanceId=instance,
            Name=instance+"-"+date+"-AMI",
            Description="AMI for instance "+instance+".",
            NoReboot=True
        )]
</code></pre>

<p>LOGS:</p>

<pre><code>18:03:30
START RequestId: 0ca9e0a3-7f11-11e6-be11-6974d9213102 Version: $LATEST

18:08:30
END RequestId: 0ca9e0a3-7f11-11e6-be11-6974d9213102

18:08:30
REPORT RequestId: 0ca9e0a3-7f11-11e6-be11-6974d9213102  Duration: 300001.99 ms  Billed Duration: 300000 ms Memory Size: 128 MB  Max Memory Used: 24 MB

18:08:30
2016-09-20T09:08:30.544Z 0ca9e0a3-7f11-11e6-be11-6974d9213102 Task timed out after 300.00 seconds
</code></pre>

<p>TRIGGER_EVENT:</p>

<pre><code>{
  "account": "123456789012",
  "region": "us-east-1",
  "detail": {},
  "detail-type": "Scheduled Event",
  "source": "aws.events",
  "time": "1970-01-01T00:00:00Z",
  "id": "cdc73f9d-aea9-11e3-9d5a-835b769c0d9c",
  "resources": [
    "arn:aws:events:us-east-1:123456789012:rule/my-schedule"
  ]
}
</code></pre>

<p><strong>EDIT-1</strong></p>

<p>IAM POLICY:</p>

<p>From my understanding all I need to allow VPC Access to my function is to add the following privilages to the lambda function's assigned policy. </p>

<ul>
<li>ec2:CreateNetworkInterface</li>
<li>ec2:DeleteNetworkInterface</li>
<li>ec2:DescribeNetworkInterfaces</li>
</ul>

<blockquote>
<pre><code>{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "logs:CreateLogGroup",
                "logs:CreateLogStream",
                "logs:PutLogEvents",
                "ec2:CreateNetworkInterface",
                "ec2:DescribeNetworkInterfaces",
                "ec2:DeleteNetworkInterface"
            ],
            "Resource": "*"
        },
        {
            "Action": [
                "ec2:DescribeInstances",
                "ec2:CreateImage",
                "autoscaling:DescribeAutoScalingGroups"
            ],
            "Effect": "Allow",
            "Resource": "*"
        },
        {
            "Effect": "Allow",
            "Action": [
                "logs:CreateLogGroup",
                "logs:CreateLogStream",
                "logs:PutLogEvents"
            ],
            "Resource": "arn:aws:logs:*:*:*"
        }
    ] }
</code></pre>
</blockquote>

<p><strong>EDIT 2</strong></p>

<p>CONFIGURATION:</p>

<p>The subnets, security groups and VPC attached to the function.</p>

<p><a href="http://i.stack.imgur.com/HwQfN.png" rel="nofollow"><img src="http://i.stack.imgur.com/HwQfN.png" alt="Configurations"></a></p>

<p><strong>EDIT 3 (CONCLUSION)</strong></p>

<p>Mark Gave an excellent answer, informing me that I had set my function up to run inside a VPC yet I was not accessing resources within the VPC.  Rather, I was accessing the Amazon API endpoint which required that I have access to the internet or the transaction would timeout.</p>

<p>As such, there were two options available to fix this situation. </p>

<ul>
<li>Remove my VPC settings</li>
<li>Create a NAT Gateway inside my VPC</li>
</ul>

<p>I chose the one that costs the least money.</p>

<p>You enabled VPC access for your Lambda function. So now it only has access to resources inside your VPC. Note that the AWS API exists outside your VPC. Any attempt to access something outside your VPC is going to result in a network timeout. That's why you are seeing the timeout issues.</p>

<p>To fix this, you can move the Lambda function outside the VPC, or you can add a NAT Gateway to your VPC. I'm not seeing anything in your code that is accessing anything inside your VPC, so it's probably cheapest and easiest to just remove the VPC settings from the Lambda function.</p>

python amazon-web-services aws-lambda boto3