Amazon web services 使用lambda';用于ECS自动部署的s

Amazon web services 使用lambda';用于ECS自动部署的s,amazon-web-services,aws-lambda,amazon-ecs,Amazon Web Services,Aws Lambda,Amazon Ecs,我对这件事几乎束手无策。我目前正在为aws应用程序设置自动部署。应用程序在没有负载平衡器的ECS内运行 这里的想法是,当一个新的docker映像被推送到ECR时,cloudTrail会拾取日志,cloudWatch会触发并发出警报,所述警报会发送到SNS,SNS会触发lambda进行部署。到目前为止,除了lambda之外,我所有的东西都在工作 我使用的代码是python,如下所示: import boto3 import pprint import os region = "us-east-2

我对这件事几乎束手无策。我目前正在为aws应用程序设置自动部署。应用程序在没有负载平衡器的ECS内运行

这里的想法是,当一个新的docker映像被推送到ECR时,cloudTrail会拾取日志,cloudWatch会触发并发出警报,所述警报会发送到SNS,SNS会触发lambda进行部署。到目前为止,除了lambda之外,我所有的东西都在工作

我使用的代码是python,如下所示:

import boto3
import pprint
import os

region = "us-east-2"
client = boto3.client('ecs', region_name=region)


response = client.list_task_definitions(familyPrefix= 'stage-dcs-task-000', status='ACTIVE')

def lambda_handler(event, context):
    response = client.register_task_definition(
        family='stage-dcs-task-000',
        networkMode='bridge',
        containerDefinitions=[
            {
                'name': 'dcs-stage-container-000',
                'image': 'ecrUrlHere:latest',
                'cpu': 10,
                'memory': 500,
                'portMappings': [
                    {
                        'containerPort': 8080,
                        'hostPort': 80,
                        'protocol': 'tcp'
                    },
                ],
                'essential': True,
            },
        ],
    )
    taskDefinitionRev = response['taskDefinition']['family'] + ':' + str(response['taskDefinition']['revision'])


    response = client.update_service(
        cluster='stage-secretName-conversation-service',
        service='stage-dcs-service-000',
        desiredCount=1,
        taskDefinition=taskDefinitionRev,
        deploymentConfiguration={
            'maximumPercent': 200,
            'minimumHealthyPercent': 100
        },
        forceNewDeployment=True
    )

现在,这个lambda由于错误而失败

  "errorMessage": "An error occurred (InvalidParameterException) when calling the UpdateService operation: The container stage-dcs-container-000is not valid. The value for 'containerName' must already be specified in the task definition. Registry: ARN-LINK-HERE"
查看AWS中创建的内容,任务定义创建正确,容器命名和配置正确。根据我从文档中了解到的情况,我对更新服务的调用是正确的,我只是不知道为什么容器名称会无效

值得注意的是,我认为容器名称可能必须是唯一的。因此,我开始在容器名称的末尾添加任务修订号来代替000,但是,我总是将stage-dcs-container-000作为无效名称返回,并且不知道它可能来自何处。有什么想法吗

在进行了更多的调试之后,我发现引发错误的是当前任务定义中的容器名称。根据容器名称规则,我不能使用破折号,所以我将它们改为下划线,但同样的问题仍然存在


参考资料:这是我一直在学习的教程。我花了一点时间才弄明白这一点,但最终还是解决了。说实话,我不是100%确定修复是什么,但我知道至少有两个错误

第一个事实是,我不知何故陷入了一种状态,在堆栈的不同部分的名称中有非法字符。为了给出一个明确的示例,我在容器的名称中使用了破折号(-),这是一个非法字符。为了解决这个问题,我们使用驼峰式外壳重新创建了整个技术堆栈

第二个是lambda函数中有一些错误,特别是在创建任务修订版时。我最终注意到,当我通过cli创建东西时,我不知何故忽略了docker的要求。我是如何解决这个问题的,我在AWS领事中创建了任务修订版,然后将json复制到lambda中的to register_task_定义方法中。这是我开始时没有用的东西

response = client.register_task_definition(
        family='stage-dcs-task-000',
        networkMode='bridge',
        containerDefinitions=[
            {
                'name': 'dcs-stage-container-000',
                'image': 'ecrUrlHere:latest',
                'cpu': 10,
                'memory': 500,
                'portMappings': [
                    {
                        'containerPort': 8080,
                        'hostPort': 80,
                        'protocol': 'tcp'
                    },
                ],
                'essential': True,
            },
        ],
    )
这就是我完成的工作

response = client.register_task_definition(
        containerDefinitions=[
            {
                "portMappings": [
                    {
                        "hostPort": 80,
                        "protocol": "tcp",
                        "containerPort": 8080
                    }
                ],
                "cpu": 10,
                "environment": [],
                "mountPoints": [],
                "memoryReservation": 500,
                "volumesFrom": [],
                "image": "image uri here",
                "essential": True,
                "name": "productionDCSContainer"
            }
        ],
        family="productionDCSTask",
        requiresCompatibilities=[
            "EC2"
        ],
        networkMode="bridge"
    )
应该注意的是,很容易陷入修订更新死锁的状态,也就是说,您有minPercentage=100、maxPercentage=100和desired=1。在这种情况下,AWS永远不会停止运行实例来启动更新。解决这个问题的一种方法是让minInstance=0、maxInstance=200%和forceNewDeployment=True