使用ECR中针对特定标签的最新版本更新ECR

时间:2019-11-19 16:52:19

标签: amazon-web-services aws-lambda boto3

我具有Lambda函数来升级我的ECS。对于最新的标记,它始终可以正常工作。

import boto3
import os

## Setting variables and importing libraries.
region = "us-east-1"
client = boto3.client('ecs', region_name=region)

CONTAINER_NAME = os.environ['NAME']
DOCKER_IMAGE = os.environ['IMAGE']
FAMILY_DEF = os.environ['TASK_DEF']
CLUSTER_NAME = os.environ['CLUSTER']
SERVICE_NAME = os.environ['SERVICE']

print ("IMAGE -> ", DOCKER_IMAGE)

def lambda_handler(event, context):
    print("----- START -----")

    response = client.register_task_definition(
        family=FAMILY_DEF,
        networkMode='awsvpc',
        containerDefinitions=[
            {
                'name': CONTAINER_NAME,
                'image': DOCKER_IMAGE,
                'memory': 300,
                'portMappings': [
                    {
                        'containerPort': 80,
                        'hostPort': 80,
                        'protocol': 'tcp'
                    },
                ],
                'essential': True,
            },
        ],
    )

    ## TaskDef for updating our service.
    response = client.update_service(
        cluster=CLUSTER_NAME,
        service=SERVICE_NAME,
        desiredCount=1,
        forceNewDeployment=True,
    ##   how many containers > n * 2 >
        deploymentConfiguration={
            'maximumPercent': 200,
            'minimumHealthyPercent': 100
        }
    )
    print("Updated service named {} cluster named {} with an updated task definition".format(SERVICE_NAME, CLUSTER_NAME))

如果DOCKER_IMAGE =到sdasdasdasd.dkr.ecr.us-east-1.amazonaws.com/cdtest:latest,它可以正常工作。

如果DOCKER_IMAGE =到sdasdasdasd.dkr.ecr.us-east-1.amazonaws.com/cdtest:myTag,它将获得最新的(标签)标签。

问题是:

如何使用cdtest: myTag 而不是/ cdtest:latest更新群集?即使当DOCKER_IMAGE = .... cdtest: myTag 时,它也始终使用cdtest:最新图片

更新集群。

1 个答案:

答案 0 :(得分:0)

import boto3
import os

## Setting variables and importing libraries.
region = "us-east-1"
client = boto3.client('ecs', region_name=region)

CONTAINER_NAME = os.environ['NAME']
DOCKER_IMAGE = os.environ['IMAGE']
FAMILY_DEF = os.environ['TASK_DEF']
CLUSTER_NAME = os.environ['CLUSTER']
SERVICE_NAME = os.environ['SERVICE']

print ("IMAGE -> ", DOCKER_IMAGE)

def lambda_handler(event, context):
    print("----- START -----")

    response = client.register_task_definition(
        family=FAMILY_DEF,
        networkMode='awsvpc',
        containerDefinitions=[
            {
                'name': CONTAINER_NAME,
                'image': DOCKER_IMAGE,
                'memory': 300,
                'portMappings': [
                    {
                        'containerPort': 80,
                        'hostPort': 80,
                        'protocol': 'tcp'
                    },
                ],
                'essential': True,
            },
        ],
    )

    ## TaskDef for updating our service.
    response = client.update_service(
        cluster=CLUSTER_NAME,
        service=SERVICE_NAME,
        desiredCount=1,
        taskDefinition=FAMILY_DEF,
        forceNewDeployment=True,
    ##   how many containers > n * 2 >
        deploymentConfiguration={
            'maximumPercent': 200,
            'minimumHealthyPercent': 100
        }
    )
    print("Updated service named {} cluster named {} with an updated task definition".format(SERVICE_NAME, CLUSTER_NAME))

您的Lambda函数已在TaskDefinition上成功创建具有所需标签的新版本(如您通过环境变量给出的那样)。问题在于服务更新。请在服务更新区域中添加“ taskDefinition = FAMILY_DEF ”行,以便使用最新的任务定义更新服务,并在ECS中部署docker映像(使用您在其中提供的标签) lambda函数的环境变量)。

它为我工作。