首页 > 解决方案 > 使用 boto3 将目标组添加到 ECS Fargate 任务

问题描述

我有一个从 lambda 函数调用的 Fargate 任务。我还有一个负载均衡器,它设置为特定的目标组,比如说tg-x。我使用 boto3 来运行上述任务。但是,我不使用服务,因为我只需要手动运行此任务。无论如何我可以将目标组添加到此任务中,例如在服务中?

我的代码:

def lambda_handler(event,context):
    client = boto3.client('ecs')
    response = client.run_task(
        cluster='oops-dev', # name of the cluster
        launchType = 'FARGATE',
        taskDefinition='oops-dev-oops-oops:oops'
        count = 1,
        platformVersion='LATEST',
        networkConfiguration={
            'awsvpcConfiguration': {
                'subnets': [
                    'subnet-oops',
                    'subnet-oops',
                    'subnet-oops'
                ],
                'securityGroups': [
                    'sg-oops'
                ],
                'assignPublicIp': 'ENABLED'
            }
    })

    return str(response)

我检查了boto3 文档以及AWS 文档,但无济于事。我还注意到,即使在 Web 界面中,也无法做到这一点。

提前致谢。

标签: amazon-web-servicesaws-lambdaboto3aws-fargate

解决方案


run_task是运行到完成和退出的东西。对于应该保持运行并为负载均衡器后面的 Web 应用程序提供服务的 ECS 任务,您需要使用create_service并传递适当的loadBalancers设置。


推荐阅读