[英]Endpoint request timed out when starting AWS ECS task from Lambda
I am trying to run an ECS task from Lambda Api gateway. 我正在尝试从Lambda Api网关运行ECS任务。 But frequently getting Endpoint request timed out.
但是频繁获取Endpoint请求超时。 I have changed the default Labmda timeout to 5 min.
我已将默认Labmda超时更改为5分钟。 But still getting timeout exception sometimes.
但是有时仍然会出现超时异常。 Is there any way to run the ECS task through Lambda without getting timeout ?
有没有办法通过Lambda运行ECS任务而不会超时?
Here is the core python code from Lambda to run an ECS task 这是Lambda运行ECS任务的核心python代码
ecs = boto3.client(
'ecs',
region_name=config.AWS_REGION,
aws_access_key_id=config.AWS_ACCESS_KEY_ID,
aws_secret_access_key=config.AWS_SECRET_ACCESS_KEY
)
request_id = str(uuid.uuid1())
ecs.run_task(
cluster='test-cluster',
taskDefinition='test-task',
startedBy=request_id,
launchType='FARGATE',
overrides={
'containerOverrides': [
{
'name': 'test-container',
'environment': [
{
'name': 'request_id',
'value': request_id
}
]
}
]
},
networkConfiguration={
'awsvpcConfiguration': {
'securityGroups': [
'sg-XXXXXXXX',
],
'subnets': [
'subnet-XXXXXXXX',
'subnet-XXXXXXXX',
'subnet-XXXXXXXX'
],
'assignPublicIp': 'ENABLED'
}
}
)
AWS API Gateway has a hard timeout limit of 29 seconds. AWS API Gateway的硬超时限制为29秒。 So regardless of the timeout limit of your lambda, the API Gateway will return a timeout if it doesn't get a response after 29 seconds.
因此,无论您的lambda超时时间是多少,API网关都会在29秒后未收到响应的情况下返回超时信息。
Your lambda should carry on regardless though, this should be simple enough to verify in cloudwatch logs. 无论如何,您的lambda应该继续进行,这应该足够简单以在cloudwatch日志中进行验证。
I'm not sure what you want API Gateway to return in this scenario, but it may be enough to have lambda begin the job and return a response to API gateway that the job has begun. 我不确定在这种情况下您希望API Gateway返回什么,但是让lambda开始工作并向该工作已开始的API网关返回响应可能就足够了。
To do this I would have API Gateway call a Lambda that invoked the Lambda running the ECS task. 为此,我将让API Gateway调用Lambda,该Lambda调用运行ECS任务的Lambda。 To make this asynch, the invocation from Lambda to Lambda would need to be an 'Event' invocation type.
为了使此请求更安全,从Lambda到Lambda的调用必须是“事件”调用类型。
Example: 例:
import boto3
import json
def lambda_handler(event, context):
response = client.invoke(
FunctionName='<ecs_lambda>',
InvocationType='Event',
Payload=json.dumps(event)
)
return { "result": "OK" }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.