【问题标题】:AWS Lambda function using Boto3 timeout使用 Boto3 超时的 AWS Lambda 函数
【发布时间】:2017-03-31 21:47:04
【问题描述】:

我已经解决了我自己的问题,但我还是发布了它,希望能为其他人节省几个小时!

我在 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

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"

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

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

事实证明,解决方案是 lambda 函数无法访问互联网。默认情况下,不在 VPC 中的 lambda 函数可以访问互联网,但在 VPC 中的 lambda 函数不能。

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

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

希望这对某人有所帮助!

【问题讨论】:

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


【解决方案1】:

事实证明,解决方案是 lambda 函数无法访问互联网。默认情况下,不在 VPC 中的 lambda 函数可以访问互联网,但在 VPC 中的 lambda 函数不能。

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

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

希望这对某人有所帮助!

【讨论】:

  • 你是如何调试这个的?无声的“超时”消息不足以让我弄清楚任何事情:-(
  • 我使用日志记录来确定问题发生在哪一行,然后在 AWS 文档中阅读了我认为它需要互联网访问才能访问 kinesis 队列和 lambda 函数默认情况下不分配 IP 地址。
  • 啊,感谢您澄清这一点。所以本质上肯定需要进行一些调查:-)
  • 这个gist也很有帮助
  • 这困扰了我 2 天。静默超时,调试 boto3 只是显示打开了多个 https 请求,没有错误。非常感谢您!
猜你喜欢
  • 2020-07-04
  • 1970-01-01
  • 1970-01-01
  • 2017-07-22
  • 1970-01-01
  • 2016-08-20
  • 2018-08-21
  • 2017-10-11
相关资源
最近更新 更多