AWS Lambda函数使用测试触发器或计划(超时)

时间:2016-09-20 09:19:53

标签: python amazon-web-services aws-lambda boto3

所以我创建了一个lambda函数(其目的不再重要),并测试了它从我的笔记本电脑运行时的效果。然而,更大的问题是我无法让它在AWS中运行测试事件或计划。

当我尝试从AWS运行它时,我得到300秒的超时错误。

以下内容供您考虑:

  • 功能
  • 日志
  • 触发事件
  • 政策
  • VPC相关配置

如果有人能告诉我这个问题是什么,我会很感激,因为我一直在寻找解决方案大约3天。

功能:

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
        )]

日志:

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

TRIGGER_EVENT:

{
  "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"
  ]
}

修改-1

IAM政策:

根据我的理解,我需要允许VPC访问我的功能是将以下特权添加到lambda函数的已分配策略中。

  • EC2:CreateNetworkInterface
  • EC2:DeleteNetworkInterface
  • EC2:DescribeNetworkInterfaces
{
    "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:*:*:*"
        }
    ] }

编辑2

配置:

附加到该功能的子网,安全组和VPC。

Configurations

编辑3(结论)

Mark给出了一个很好的答案,通知我,我已将我的功能设置为在VPC内部运行,但我没有访问VPC内的资源。相反,我正在访问Amazon API端点,该端点要求我可以访问互联网,否则事务将超时。

因此,有两种方法可以解决这种情况。

  • 删除我的VPC设置
  • 在我的VPC中创建NAT网关

我选择了花费最少的钱。

1 个答案:

答案 0 :(得分:2)

您为Lambda函数启用了VPC访问权限。所以现在它只能访问您的VPC内的资源。请注意,AWS API存在于您的VPC之外。任何尝试访问VPC外部的内容都会导致网络超时。这就是您看到超时问题的原因。

要解决此问题,您可以将Lambda功能移到VPC外部,或者可以将NAT网关添加到VPC。我的代码中没有看到任何内容正在访问您的VPC中的任何内容,因此从Lambda函数中删除VPC设置可能是最便宜和最简单的。