How do I deploy updated Docker images to Amazon ECS tasks?

前端 未结 12 1084
灰色年华
灰色年华 2021-01-29 22:00

What is the right approach to make my Amazon ECS tasks update their Docker images, once said images have been updated in the corresponding registry?

12条回答
  •  清酒与你
    2021-01-29 22:55

    since there has not been any progress at AWS side. I will give you the simple python script that exactly performs the steps described in the high rated answers of Dima and Samuel Karp.

    First push your image into your AWS registry ECR then run the script:

    import boto3, time
    
    client = boto3.client('ecs')
    cluster_name = "Example_Cluster"
    service_name = "Example-service"
    reason_to_stop = "obsolete deployment"
    
    # Create new deployment; ECS Service forces to pull from docker registry, creates new task in service
    response = client.update_service(cluster=cluster_name, service=service_name, forceNewDeployment=True)
    
    # Wait for ecs agent to start new task
    time.sleep(10)
    
    # Get all Service Tasks
    service_tasks = client.list_tasks(cluster=cluster_name, serviceName=service_name)
    
    # Get meta data for all Service Tasks
    task_meta_data = client.describe_tasks(cluster=cluster_name, tasks=service_tasks["taskArns"])
    
    # Extract creation date
    service_tasks = [(task_data['taskArn'], task_data['createdAt']) for task_data in task_meta_data["tasks"]]
    
    # Sort according to creation date
    service_tasks = sorted(service_tasks, key= lambda task: task[1])
    
    # Get obsolete task arn
    obsolete_task_arn = service_tasks[0][0]
    print("stop ", obsolete_task_arn)
    
    # Stop obsolete task
    stop_response = client.stop_task(cluster=cluster_name, task=obsolete_task_arn, reason=reason_to_stop)
    

    This code does:

    1. create a new task with the new image in the service
    2. stop the obsolete old task with the old image in the service

提交回复
热议问题