0
votes

I have an Lambda function to upgrade my ECS. It always works ok for the latest tag.

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))

If DOCKER_IMAGE = to sdasdasdasd.dkr.ecr.us-east-1.amazonaws.com/cdtest:latest it works ok.

If DOCKER_IMAGE = to sdasdasdasd.dkr.ecr.us-east-1.amazonaws.com/cdtest:myTag it gets me the latest (tag) one.

The question is:

How can I get it to update the cluster with the cdtest:myTag and not the /cdtest:latest ? Even when DOCKER_IMAGE = .... cdtest:myTag it always updates the cluster with cdtest:latest image

1
What is your question?John Rotenstein
How can I get it to update the cluster with the cdtest:myTag and not the /cdtest:latest ? Even when DOCKER_IMAGE = .... cdtest:my it always updates the cluster with cdtest:latest imageMartim Ramos

1 Answers

0
votes

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))

Your Lambda function is successfully creating new version on TaskDefinition with the desired Tag (As you given through environment variable). The issue is with the Service update. Please add the line " taskDefinition=FAMILY_DEF," in the service updating area so that the service will be updated with the latest task definition and deploy the docker images in the ECS (with the tags you have given in the environment variable of lambda function).

Its working for me.