简体   繁体   English

AWS CloudFormation 无法使用 CodeDeploy 蓝/绿部署创建堆栈

[英]AWS CloudFormation cannot create Stack using CodeDeploy blue/green deployments

I am trying to deploy a new stack using CloudFormation with an ECS service using the CodeDeploy launch type to enable blue/green deployments.我正在尝试使用 CloudFormation 和 ECS 服务部署新堆栈,使用 CodeDeploy 启动类型启用蓝/绿部署。

In the User Guide for performing blue/green deployments via CloudFormation, they state the following:在通过 CloudFormation 执行蓝/绿部署的用户指南中,他们 state 以下内容:

To enable CloudFormation to perform blue/green deployments on a Stack, include the following information in its stack template:要使 CloudFormation 能够在堆栈上执行蓝/绿部署,请在其堆栈模板中包含以下信息:

At least one of the ECS resources that will trigger a blue/green deployment if replaced during a stack update.如果在堆栈更新期间更换,至少将触发蓝/绿部署的 ECS 资源之一。 Currently, those resources are AWS::ECS::TaskDefinition and AWS::ECS::TaskSet.目前,这些资源是 AWS::ECS::TaskDefinition 和 AWS::ECS::TaskSet。

When I exclude a AWS::ECS::TaskSet , the Stack creation fails and I receive the following error:当我排除AWS::ECS::TaskSet时,堆栈创建失败并且我收到以下错误:

Transform AWS::CodeDeployBlueGreen failed with: Failed to transform template. 
ECSAttributes must include TaskSets in AWS::CodeDeploy::BlueGreen Hook

If I add a AWS::ECS::TaskSet , the stack fails to create with the following error instead:如果我添加AWS::ECS::TaskSet ,堆栈将无法创建并出现以下错误:

Resource handler returned message: 
"Invalid request provided: Amazon ECS does not support task set management on services where deployments 
are controlled by AWS CodeDeploy. 
(Service: AmazonECS; Status Code: 400; Error Code: InvalidParameterException; 
Request ID: 61b8c146-3ae9-4bc2-ac5c-08a11e194f06; Proxy: null)" 
(RequestToken: 86a8a3a5-fe89-9939-15c2-45b08b28c3f3, HandlerErrorCode: InvalidRequest)

These are the relevant parts of my stack template:这些是我的堆栈模板的相关部分:

Transform:
  - AWS::CodeDeployBlueGreen

Hooks:
  CodeDeployBlueGreenHook:
    Type: AWS::CodeDeploy::BlueGreen
    Properties:
      ServiceRole: BlueGreenDeploymentRole
      Applications:
        - Target:
            Type: AWS::ECS::Service
            LogicalID: EcsService
          ECSAttributes:
            TaskDefinitions:
              - TaskDefinitionBlue
              - TaskDefinitionGreen
            TaskSets:
              - TaskSetBlue
              - TaskSetGreen
            TrafficRouting:
              ProdTrafficRoute:
                Type: AWS::ElasticLoadBalancingV2::Listener
                LogicalID: LoadBalancerListener
              TargetGroups:
                - TargetGroupBlue
                - TargetGroupGreen
      TrafficRoutingConfig:
        Type: TimeBasedLinear
        TimeBasedLinear:
          StepPercentage: 20
          BakeTimeMins: 10
      AdditionalOptions:
        TerminationWaitTimeInMinutes: 60

Resources:
  # IAM Role for blue/green deployments
  BlueGreenDeploymentRole:
    Type: AWS::IAM::Role
    Properties:
      RoleName: blue-green-deployment-role
      AssumeRolePolicyDocument:
        Statement:
          - Effect: Allow
            Principal:
              Service: codedeploy.amazonaws.com
            Action: sts:AssumeRole
      Policies:
        - PolicyName: BlueGreenDeploymentPolicy
          PolicyDocument:
            Statement:
              - Effect: Allow
                Action:
                  - codedeploy:Get*
                  - codedeploy:CreateCloudFormationDeployment
                Resource: '*'
      ManagedPolicyArns:
        - arn:aws:iam::aws:policy/AWSCodeDeployRoleForECS

  #########################
  # Load Balancer
  #########################

  # Application Load Balancer
  LoadBalancer:
    Type: AWS::ElasticLoadBalancingV2::LoadBalancer
    Properties:
      IpAddressType: ipv4
      Name: service-alb
      Scheme: internal
      Subnets:
        - !Ref SubnetOne
        - !Ref SubnetTwo
      SecurityGroups:
        - !Ref SecurityGroup

  # Load Balancer Listener
  LoadBalancerListener:
    Type: AWS::ElasticLoadBalancingV2::Listener
    Properties:
      DefaultActions:
        - Type: forward
          ForwardConfig:
            TargetGroups:
              - TargetGroupArn: !Ref TargetGroupBlue
                Weight: 1
              - TargetGroupArn: !Ref TargetGroupGreen
                Weight: 1
      LoadBalancerArn: !Ref LoadBalancer
      Port: 8080
      Protocol: HTTP

  # Load Balancer Target Groups
  TargetGroupBlue:
    Type: AWS::ElasticLoadBalancingV2::TargetGroup
    Properties:
      Name: !Sub ${ServiceName}-blue
      TargetType: ip
      VpcId: !Ref Vpc
      Port: 8080
      Protocol: HTTP
      HealthCheckPort: 8080
      HealthCheckPath: /actuator/health

  TargetGroupGreen:
    Type: AWS::ElasticLoadBalancingV2::TargetGroup
    Properties:
      Name: !Sub ${ServiceName}-green
      TargetType: ip
      VpcId: !Ref Vpc
      Port: 8080
      Protocol: HTTP
      HealthCheckPort: 8080
      HealthCheckPath: /actuator/health

  #########################
  # ECS
  #########################

  # ECS Cluster
  Cluster:
    Type: AWS::ECS::Cluster
    Properties:
      ClusterName: !Ref ServiceName

  # ECS Service
  EcsService:
    Type: AWS::ECS::Service
    DependsOn: LoadBalancerListener
    Properties:
      ServiceName: !Ref ServiceName
      Cluster: !Ref Cluster
      TaskDefinition: !Ref TaskDefinitionBlue
      DeploymentController:
        Type: CODE_DEPLOY
      DesiredCount: 0
      LaunchType: FARGATE
      LoadBalancers:
        - ContainerName: !Sub ${ServiceName}-container
          ContainerPort: 8080
          TargetGroupArn: !Ref TargetGroupBlue
        - ContainerName: !Sub ${ServiceName}-container
          ContainerPort: 8080
          TargetGroupArn: !Ref TargetGroupGreen
      NetworkConfiguration:
        AwsvpcConfiguration:
          Subnets:
            - !Ref SubnetOne
            - !Ref SubnetTwo
          SecurityGroups:
            - !Ref SecurityGroup
      SchedulingStrategy: REPLICA

  # Task Definitions
  TaskDefinitionBlue:
    Type: AWS::ECS::TaskDefinition
    Properties:
      ContainerDefinitions:
        - Name: !Sub ${ServiceName}-container
          Image: !Sub ${ImageRepository.RepositoryUri}:latest
          LogConfiguration:
            LogDriver: awslogs
            Options:
              awslogs-group: !Ref LogGroup
              awslogs-region: !Ref AWS::Region
              awslogs-stream-prefix: payments
          PortMappings:
            - ContainerPort: 8080
      Cpu: 256
      Memory: 512
      NetworkMode: awsvpc
      Family: !Sub ${ServiceName}
      ExecutionRoleArn: !Ref TaskExecutionRole
      RequiresCompatibilities:
        - FARGATE

  TaskDefinitionGreen:
    Type: AWS::ECS::TaskDefinition
    Properties:
      ContainerDefinitions:
        - Name: !Sub ${ServiceName}-container
          Image: !Sub ${ImageRepository.RepositoryUri}:latest
          LogConfiguration:
            LogDriver: awslogs
            Options:
              awslogs-group: !Ref LogGroup
              awslogs-region: !Ref AWS::Region
              awslogs-stream-prefix: payments
          PortMappings:
            - ContainerPort: 8080
      Cpu: 256
      Memory: 512
      NetworkMode: awsvpc
      Family: !Sub ${ServiceName}
      ExecutionRoleArn: !Ref TaskExecutionRole
      RequiresCompatibilities:
        - FARGATE

  # Image Repository
  ImageRepository:
    Type: AWS::ECR::Repository
    Properties:
      RepositoryName: !Sub ${ServiceName}
      LifecyclePolicy:
        LifecyclePolicyText: |
          {
            "rules": [
              {
                "rulePriority": 1,
                "description": "Maintain at most 25 images",
                "selection": {
                  "tagStatus": "untagged",
                  "countType": "imageCountMoreThan",
                  "countNumber": 25
                },
                "action": {
                  "type": "expire"
                }
              }
            ]
          }

  TaskSetBlue:
    Type: AWS::ECS::TaskSet
    Properties:
      Cluster: !Ref Cluster
      LaunchType: FARGATE
      Service: !Ref EcsService
      TaskDefinition: !Ref TaskDefinitionBlue
      NetworkConfiguration:
        AwsVpcConfiguration:
          Subnets:
            - !Ref SubnetOne
            - !Ref SubnetTwo
          SecurityGroups:
            - !Ref SecurityGroup
      LoadBalancers:
        - ContainerName: !Sub ${ServiceName}-container
          ContainerPort: 8080
          TargetGroupArn: !Ref TargetGroupBlue

How can I update my template to allow for the blue/green deployment strategy via CodeDeploy?如何更新我的模板以允许通过 CodeDeploy 进行蓝/绿部署策略?

G/B deployment using CFN is EXTERNAL , not CODE_DEPLOY .使用 CFN 的 G/B 部署是EXTERNAL而不是CODE_DEPLOY There could be many other issues with your template, but your current error relates to using wrong DeploymentController .您的模板可能还有许多其他问题,但您当前的错误与使用错误的DeploymentController有关。 Please study AWS docs and example:请研究 AWS 文档和示例:

Exactly, AWS doesn't support Blue Green deployment on EC2/ASG...!确切地说,AWS 不支持在 EC2/ASG 上进行蓝绿部署……! The easiest way that I just figure out is to create the deployment group with In-Place method && Then change the deployment group configuration manually from In-place to Blue Green我刚刚发现的最简单的方法是使用就地方法创建部署组 && 然后手动将部署组配置从就地更改为蓝绿色

Or you can use the lambda function, but is a litte bit hard to customize it或者你可以使用 lambda function,但是有点难定制

Wish this will help you希望这对你有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AWS Blue/Green CodeDeploy 到 ECS 安装生命周期事件超时 - AWS Blue/Green CodeDeploy to ECS install lifecycle event timesout 如何让 CodeDeploy Blue/Green 为自定义指标创建 CloudWatch 警报? - How to make CodeDeploy Blue/Green create CloudWatch alarms for custom metrics? 无法创建 AWS CloudFormation 堆栈 - Unable to create AWS CloudFormation stack Cloudformation 蓝/绿部署 HealthCheckGracePeriodSeconds - Cloudformation Blue/Green Deployment HealthCheckGracePeriodSeconds AWS CloudFormation create-stack 命令导致未创建堆栈 - AWS CloudFormation create-stack command results in no stack created 如何使用 AWS CloudFormation 创建 Amazon VPC? - How to create an Amazon VPC using AWS CloudFormation? 无法通过单个 AWS CloudFormation 堆栈创建 AWS EKS 集群和工作节点组 - Unable to create AWS EKS cluster and worker nodes group by a single AWS CloudFormation stack CloudFormation 堆栈类型:“AWS::IAM::Role” - CloudFormation Stack Type: 'AWS::IAM::Role' 我们如何使用 AWS CloudFormation 创建 SageMaker 管道? - How do we create a SageMaker pipeline using AWS CloudFormation? 如何使用 AWS CloudFormation 创建具有公共访问权限的 Postgres 数据库 - How to create Postgres DB with Public access using AWS CloudFormation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM