6
我已經解決了我自己的問題,但無論如何發佈它希望保存別人幾個小時!AWS Lambda函數使用Boto3超時
我在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"
當我跑我看到雲watch日誌以下功能:
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,並指出任何子網與Internet網關
- 路由表對本地流量的路由( .0.0/16 |本地。 | Active)以及所有其他IP到nat網關的路由(0.0.0.0/0 | NAT ID | Active)
- 設置爲使用新的路由表。
希望這可以幫助別人!
Thanks @ Farhan.K –
你怎麼能夠調試呢?無聲的「超時」消息不足以讓我弄清楚任何事情:-( –
我使用日誌記錄來確定發生問題的線路,然後閱讀AWS文檔,在那裏我需要互聯網訪問能夠訪問kinesis隊列,並且lambda函數不是默認分配的IP地址。 –