AWS Lambda函数使用Boto3超时

Wil*_*ore 11 python amazon-web-services boto3 aws-lambda

我已经解决了我自己的问题,但无论如何都要张贴它,希望能在几个小时内拯救别人!

我在AWS上有一个无服务器项目,使用Python将记录插入到kinesis队列中.但是,当我使用boto3.client('kinesis')或put_record函数时,它似乎挂起,直到它超时,没有错误消息或其他信息.以下是功能:

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
Run Code Online (Sandbox Code Playgroud)

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"
Run Code Online (Sandbox Code Playgroud)

当我运行该功能时,我在云监视日志中看到以下内容:

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
Run Code Online (Sandbox Code Playgroud)

事实证明,解决方案是lambda函数无法访问互联网.默认情况下,不在VPC中的lambda函数具有Internet访问权限,但VPC内部的lambda函数不具有Internet访问权限.

为了解决这个问题,我创建了一个新的子网,路由表,弹性IP和nat网关.它们配置如下:

  • nat网关使用弹性IP并指向具有Internet网关的任何子网
  • 路由表对本地流量的路由(. .0.0/16 |地方|活动)和所有其他IP对NAT网关路由(0.0.0.0/0 | NAT ID |活动)
  • 设置为使用新路由表.

希望这有助于某人!

Wil*_*ore 20

事实证明,解决方案是lambda函数无法访问互联网.默认情况下,不在VPC中的lambda函数具有Internet访问权限,但VPC内部的lambda函数不具有Internet访问权限.

为了解决这个问题,我创建了一个新的子网,路由表,弹性IP和nat网关.它们配置如下:

  • NAT网关使用弹性IP并指向具有Internet网关的任何子网
  • Route表有一个本地流量路由(..0.0/16 | Local | Active)和所有其他IP到NAT网关的路由(0.0.0.0/0 | NAT ID | Active)
  • 设置为使用新路由表.

希望这有助于某人!

  • 这个[要点](https://gist.github.com/reggi/dc5f2620b7b4f515e68e46255ac042a7)也很有帮助 (2认同)
  • 这困扰了我2天。无提示超时,调试 boto3 仅显示打开多个 https 请求,没有错误。非常感谢你做的这些! (2认同)