使用 Boto3 超时的 AWS Lambda 函数
AWS Lambda function using Boto3 timeout
我已经解决了我自己的问题,但我还是将其发布,希望能为其他人节省几个小时!
我在 AWS 上有一个无服务器项目,使用 Python 将记录插入运动队列。但是,当我使用 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 函数没有。
为了解决这个问题,我创建了一个新的子网、路由 table、弹性 IP 和 nat 网关。它们的配置如下:
- nat 网关使用弹性 IP 并指向任何具有互联网网关的子网
- 路由 table 有一个本地流量路由(..0.0/16 | Local | Active)和一个所有其他 IP 到 nat 网关的路由(0.0.0.0/0 | NAT ID | 活跃)
- 设置为使用新路线 table。
希望这对某人有所帮助!
事实证明,解决方案是 lambda 函数无法访问 Internet。默认情况下,不在 VPC 中的 lambda 函数可以访问互联网,但在 VPC 中的 lambda 函数没有。
为了解决这个问题,我创建了一个新的子网、路由 table、弹性 IP 和 nat 网关。它们的配置如下:
- NAT 网关使用弹性 IP 并指向任何具有互联网网关的子网
- 路由 table 具有本地流量路由 (
..0.0/16 | Local | Active
) 和所有其他 IP 到 NAT 网关的路由 (0.0.0.0/0 | NAT ID | Active
)
- 设置为使用新路线 table。
希望这对某人有所帮助!
我已经解决了我自己的问题,但我还是将其发布,希望能为其他人节省几个小时!
我在 AWS 上有一个无服务器项目,使用 Python 将记录插入运动队列。但是,当我使用 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 函数没有。
为了解决这个问题,我创建了一个新的子网、路由 table、弹性 IP 和 nat 网关。它们的配置如下:
- nat 网关使用弹性 IP 并指向任何具有互联网网关的子网
- 路由 table 有一个本地流量路由(..0.0/16 | Local | Active)和一个所有其他 IP 到 nat 网关的路由(0.0.0.0/0 | NAT ID | 活跃)
- 设置为使用新路线 table。
希望这对某人有所帮助!
事实证明,解决方案是 lambda 函数无法访问 Internet。默认情况下,不在 VPC 中的 lambda 函数可以访问互联网,但在 VPC 中的 lambda 函数没有。
为了解决这个问题,我创建了一个新的子网、路由 table、弹性 IP 和 nat 网关。它们的配置如下:
- NAT 网关使用弹性 IP 并指向任何具有互联网网关的子网
- 路由 table 具有本地流量路由 (
..0.0/16 | Local | Active
) 和所有其他 IP 到 NAT 网关的路由 (0.0.0.0/0 | NAT ID | Active
) - 设置为使用新路线 table。
希望这对某人有所帮助!