简体   繁体   English

从Lambda启动AWS ECS任务时端点请求超时

[英]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.

相关问题 使用Python从AWS Lambda安排ECS任务 - Schedule ECS Task from AWS Lambda in Python AWS Lambda 函数 Python 脚本任务超时错误 - AWS Lambda function Python script Task timed out error 使用 Python 在 Lambda 中获取请求任务超时 - get request Task timed out in Lambda using Python 请求返回“端点请求超时” - request returning "Endpoint request timed out" aws lambda 在将 rds 与 boto3 连接时未显示“任务超时”错误 - Aws lambda not showing "Task timed out" error while connecting rds with boto3 AWS Lambda 日志记录我想使用向量(ECS 任务)将 AWS Lambda 的日志记录从 CloudWatch 移动到 Elasticsearch 和 Kibana - AWS Lambda logging I want to move my logging for AWS Lambda from CloudWatch to Elasticsearch and Kibana, using vector(ECS Task) AWS Lambda 检查端口是否在 DNS 名称上打开时出现超时错误 - AWS Lambda timed out error for checking if a port is open on a DNS name AWS Lambda function 读取红移详细信息超时 - AWS Lambda function reading redshift details getting timed out Python,无服务器...向 lambda AWS 端点发送请求时出现“无法导入 app.app” - Python, Serverless...Getting "Unable to import app.app" when sending a request to lambda AWS Endpoint 当我使用 twilio API 时,如何在 AWS Lambda 函数中使用 python 修复超时错误 - How to fix timed out error using python in AWS Lambda functions when i am using twilio API's
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM