在ECS上进行蓝/绿部署所需的Cloudformation脚本

时间:2019-01-22 09:07:39

标签: amazon-web-services amazon-cloudformation amazon-ecs aws-code-deploy blue-green-deployment

我正在尝试为具有蓝绿色部署支持的AWS ECS编写云形成模板。此蓝绿色功能是AWS最近在ECS中添加的,在云形成模板中找不到用于更新它的任何参考。他们提供了有关如何通过UI而不是通过云形成的文档。我想,AWS可能不会更新其云形成文档,因为它是一项新功能。查找文档的任何帮助将不胜感激。预先感谢您。

2 个答案:

答案 0 :(得分:4)

已添加对CloudFormation中的蓝色/绿色部署的支持。可以在这里的文档中找到: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ecs-service-deploymentcontroller.html

在“类型”属性中,可以选择“ CODE_DEPLOY”作为部署类型。希望这可以帮助!

答案 1 :(得分:0)

当前cloudformation不支持 DeploymentController 参数,您可以在其中指定 CODE_DEPLOY 。 通过访问此页面以获取文档更新来保持更新: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ecs-service.html

现在-使用自定义cloudformation资源。使用Boto3库通过CODE_DEPLOY设置创建服务。在这里阅读更多: https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ecs.html#ECS.Client.create_service

这是可以创建/删除/更新ecs的python类:

import boto3

from botocore.exceptions import ClientError
from typing import Any, Dict

client = boto3.client('ecs')


class Service:
    @staticmethod
    def create(**kwargs) -> Dict[str, Any]:
        kwargs = dict(
            cluster=kwargs.get('cluster'),
            serviceName=kwargs.get('serviceName'),
            taskDefinition=kwargs.get('taskDefinition'),
            loadBalancers=kwargs.get('loadBalancers'),
            serviceRegistries=kwargs.get('serviceRegistries'),
            desiredCount=kwargs.get('desiredCount'),
            clientToken=kwargs.get('clientToken'),
            launchType=kwargs.get('launchType'),
            platformVersion=kwargs.get('platformVersion'),
            role=kwargs.get('role'),
            deploymentConfiguration=kwargs.get('deploymentConfiguration'),
            placementConstraints=kwargs.get('placementConstraints'),
            placementStrategy=kwargs.get('placementStrategy'),
            networkConfiguration=kwargs.get('networkConfiguration'),
            healthCheckGracePeriodSeconds=kwargs.get('healthCheckGracePeriodSeconds'),
            schedulingStrategy=kwargs.get('schedulingStrategy'),
            deploymentController=kwargs.get('deploymentController'),
            tags=kwargs.get('tags'),
            enableECSManagedTags=kwargs.get('enableECSManagedTags'),
            propagateTags=kwargs.get('propagateTags'),
        )

        kwargs = {key: value for key, value in kwargs.items() if key and value}
        return client.create_service(**kwargs)

    @staticmethod
    def update(**kwargs: Dict[str, Any]) -> Dict[str, Any]:
        filtered_kwargs = dict(
            cluster=kwargs.get('cluster'),
            service=kwargs.get('serviceName'),
            desiredCount=kwargs.get('desiredCount'),
            taskDefinition=kwargs.get('taskDefinition'),
            deploymentConfiguration=kwargs.get('deploymentConfiguration'),
            networkConfiguration=kwargs.get('networkConfiguration'),
            platformVersion=kwargs.get('platformVersion'),
            forceNewDeployment=kwargs.get('forceNewDeployment'),
            healthCheckGracePeriodSeconds=kwargs.get('healthCheckGracePeriodSeconds')
        )

        try:
            filtered_kwargs = {key: value for key, value in filtered_kwargs.items() if key and value}
            return client.update_service(**filtered_kwargs)
        except ClientError as ex:
            if ex.response['Error']['Code'] == 'InvalidParameterException':
                if 'use aws codedeploy' in ex.response['Error']['Message'].lower():
                    # For services using the blue/green (CODE_DEPLOY ) deployment controller,
                    # only the desired count, deployment configuration, and health check grace period
                    # can be updated using this API. If the network configuration, platform version, or task definition
                    # need to be updated, a new AWS CodeDeploy deployment should be created.
                    filtered_kwargs = dict(
                        cluster=kwargs.get('cluster'),
                        service=kwargs.get('serviceName'),
                        desiredCount=kwargs.get('desiredCount'),
                        deploymentConfiguration=kwargs.get('deploymentConfiguration'),
                        healthCheckGracePeriodSeconds=kwargs.get('healthCheckGracePeriodSeconds'),
                    )

                    filtered_kwargs = {key: value for key, value in filtered_kwargs.items() if key and value}
                    return client.update_service(**filtered_kwargs)
            elif ex.response['Error']['Code'] == 'ServiceNotActiveException':
                # We can not update ecs service if it is inactive.
                return {'Code': 'ServiceNotActiveException'}
            elif ex.response['Error']['Code'] == 'ServiceNotFoundException':
                # If for some reason service was not found - don't update and return.
                return {'Code': 'ServiceNotFoundException'}
            raise

    @staticmethod
    def delete(**kwargs: Dict[str, Any]) -> Dict[str, Any]:
        kwargs = dict(
            cluster=kwargs.get('cluster'),
            service=kwargs.get('serviceName'),
            force=True
        )

        kwargs = {key: value for key, value in kwargs.items() if key and value}
        return client.delete_service(**kwargs)