简体   繁体   English

AWS Lambda函数使用Boto3超时

[英]AWS Lambda function using Boto3 timeout

I have solved my own question, but am posting it anyway in the hope of saving someone else a few hours! 我已经解决了我自己的问题,但无论如何都要张贴它,希望能在几个小时内拯救别人!

I have a serverless project on AWS using Python to insert a record into a kinesis queue. 我在AWS上有一个无服务器项目,使用Python将记录插入到kinesis队列中。 However when I use boto3.client('kinesis') or the put_record function it seems to hang until it times out, with no error messages or other information. 但是,当我使用boto3.client('kinesis')或put_record函数时,它似乎挂起,直到它超时,没有错误消息或其他信息。 Below is the function: 以下是功能:

import boto3

def put_record_kinesis(data, stream_name, partition_key):
    print "create kinesis begin"
    kinesis = boto3.client("kinesis")

    print "put record begin"
    response = kinesis.put_record(StreamName=stream_name, Data=data, PartitionKey=partition_key)
    print "put record complete"
    print response

The serverless.yml definition is a follows: serverless.yml定义如下:

provider:
  name: aws
  runtime: python2.7
  iamRoleStatements:
    - Effect: "Allow"
      Action:
        - "ec2:CreateNetworkInterface"
        - "ec2:DescribeNetworkInterfaces"
        - "ec2:DeleteNetworkInterface"
        - "kinesis:*"
      Resource: "*"

  vpc:
    securityGroupIds:
      - sg-...
    subnetIds:
      - subnet-...
      - subnet-...
      - subnet-...

  stage: dev
  region: eu-west-1
  memorySize: 128

functions:
  LambdaQueueFunction:
    handler: python_file.queue
    memorySize: 1024
    timeout: 100

  LambdaDequeueFunction:
    handler: python_file.dequeue

resources:
  Resources:
    KinesisQueue:
      Type: AWS::Kinesis::Stream
      Properties:
        Name: kinesis-queue
        ShardCount: 1
    ChronosQueueMap:
      Type: AWS::Lambda::EventSourceMapping
      DependsOn:
        - "LambdaDequeueFunctionLambdaFunction"
        - "IamPolicyLambdaExecution"
      Properties:
        BatchSize: 1
        EventSourceArn:
          Fn::GetAtt:
            - "KinesisQueue"
            - "Arn"
        FunctionName:
          Fn::GetAtt:
            - "LambdaDequeueFunctionLambdaFunction"
            - "Arn"
        StartingPosition: "TRIM_HORIZON"

When I run the function I see the following in cloud watch logs: 当我运行该功能时,我在云监视日志中看到以下内容:

10:53:02 | START RequestId: 027bb0cb-acb4-11e6-b20c-1b587b734943 Version: $LATEST
10:53:02 | put records begin
10:54:42 | END RequestId: 027bb0cb-acb4-11e6-b20c-1b587b734943
10:54:42 | REPORT RequestId: 027bb0cb-acb4-11e6-b20c-1b587b734943   Duration: 100002.99 ms  Billed Duration: 100000 ms Memory Size: 1024 MB Max Memory Used: 22 MB
10:54:42 | 2016-11-17T10:54:42.155Z 027bb0cb-acb4-11e6-b20c-1b587b734943 Task timed out after 100.00 seconds

It turns out that the solution was that the lambda function did not have access to the internet. 事实证明,解决方案是lambda函数无法访问互联网。 By default a lambda function not in a VPC has internet access, but a lambda function inside a VPC does not. 默认情况下,不在VPC中的lambda函数具有Internet访问权限,但VPC内部的lambda函数不具有Internet访问权限。

To fix this I created a new subnet, route table, elastic IP and nat gateway. 为了解决这个问题,我创建了一个新的子网,路由表,弹性IP和nat网关。 They were configured as follows: 它们配置如下:

  • The nat gateway uses the elastic IP and points to any subnet with an internet gateway nat网关使用弹性IP并指向具有Internet网关的任何子网
  • The Route table has a route for local traffic ( . .0.0/16 | Local | Active) and a route for all other IP's to the nat gateway (0.0.0.0/0 | NAT ID | Active) 路由表对本地流量的路由(.0.0 / 16 |。本地|活动)和所有其他IP对NAT网关路由(0.0.0.0/0 | NAT ID |活动)
  • The is set to use the new route table. 设置为使用新路由表。

Hope this helps someone! 希望这有助于某人!

It turns out that the solution was that the lambda function did not have access to the internet. 事实证明,解决方案是lambda函数无法访问互联网。 By default a lambda function not in a VPC has internet access, but a lambda function inside a VPC does not. 默认情况下,不在VPC中的lambda函数具有Internet访问权限,但VPC内部的lambda函数不具有Internet访问权限。

To fix this I created a new subnet, route table, elastic IP and nat gateway. 为了解决这个问题,我创建了一个新的子网,路由表,弹性IP和nat网关。 They were configured as follows: 它们配置如下:

  • The NAT gateway uses the elastic IP and points to any subnet with an internet gateway NAT网关使用弹性IP并指向具有Internet网关的任何子网
  • The Route table has a route for local traffic ( ..0.0/16 | Local | Active ) and a route for all other IP's to the NAT gateway ( 0.0.0.0/0 | NAT ID | Active ) Route表有本地流量的路由( ..0.0/16 | Local | Active )和NAT网关的所有其他IP的路由( 0.0.0.0/0 | NAT ID | Active
  • The is set to use the new route table. 设置为使用新路由表。

Hope this helps someone! 希望这有助于某人!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM