使用Boto3超时的AWS Lambda函数 [英] 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!
我在AWS上有一个无服务器项目,使用Python将记录插入运动学队列.但是,当我使用boto3.client('kinesis')或put_record函数时,它似乎一直挂起,直到超时,没有错误消息或其他信息.下面是函数:
I have a serverless project on AWS using Python to insert a record into a kinesis queue. 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. 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
serverless.yml的定义如下:
The serverless.yml definition is a follows:
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
事实证明,解决方案是lambda函数无法访问互联网.默认情况下,不在VPC中的Lambda函数可以访问Internet,但是在VPC内部的Lambda函数却不能.
It turns out that the solution was that the lambda function did not have access to the internet. By default a lambda function not in a VPC has internet access, but a lambda function inside a VPC does not.
为解决此问题,我创建了一个新的子网,路由表,弹性IP和nat网关.它们的配置如下:
To fix this I created a new subnet, route table, elastic IP and nat gateway. They were configured as follows:
- nat网关使用弹性IP并指向具有Internet网关的任何子网
- 路由"表具有用于本地通信的路由(. .0.0/16 | Local | Active)和用于所有其他IP到nat网关的路由(0.0.0.0/0 | NAT ID |有效)
- 已设置为使用新的路由表.
- The nat gateway uses the elastic IP and points to any subnet with an internet gateway
- 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)
- The is set to use the new route table.
希望这对某人有帮助!
推荐答案
事实证明,解决方案是lambda函数无法访问互联网.默认情况下,不在VPC中的Lambda函数可以访问Internet,但是在VPC内部的Lambda函数却不能.
It turns out that the solution was that the lambda function did not have access to the internet. By default a lambda function not in a VPC has internet access, but a lambda function inside a VPC does not.
为解决此问题,我创建了一个新的子网,路由表,弹性IP和nat网关.它们的配置如下:
To fix this I created a new subnet, route table, elastic IP and nat gateway. They were configured as follows:
- NAT网关使用弹性IP并通过Internet网关指向任何子网
- 路由表具有用于本地通信的路由(
..0.0/16 | Local | Active
)和用于所有其他IP到NAT网关的路由(0.0.0.0/0 | NAT ID | Active
) - 已设置为使用新的路由表.
- The NAT gateway uses the elastic IP and points to any subnet with an internet gateway
- 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
) - The is set to use the new route table.
希望这对某人有帮助!
这篇关于使用Boto3超时的AWS Lambda函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!