0

我正在使用 bitbucket 进行版本控制。

CI部分

我正在通过 circleci 构建图像并将其推送到 dockerhub。

光盘

为了托管我的应用程序,我将 codedeploy 与 ecs 一起使用。

当我将更新的代码推送到 bitbucket、circleci 构建并将图像推送到 dockerhub 但 codedeploy 未更新新版本的应用程序时,我遇到了一个问题。

我尝试更新 ecs 集群时抛出错误:“调用 UpdateService 操作时:无法使用 CODE_DEPLOY 部署控制器强制对服务进行新部署。使用 AWS CodeDeploy 触发新部署。”

在这种情况下,我应该如何配置我的 codedeploy 以正常工作。

resource "aws_ecs_task_definition" "ecs-task-definition" {
  family                   = "ecs-task-definition" 
  container_definitions = <<DEFINITION
  [
    {
      "image": "${var.image}",
      "cpu": 1024,
      "memory": 2048,
      "name": "hello-world-app",
      "essential": true,
      "networkMode": "awsvpc",
      "portMappings": [
        {
          "containerPort": 3000,
          "hostPort": 3000
        }
      ]
    }
  ]
  DEFINITION
  requires_compatibilities = ["FARGATE"] 
  network_mode             = "awsvpc"  
  execution_role_arn       = data.aws_iam_role.ecs_task_execution_role.arn  
  memory                   = 2048        
  cpu                      = 1024
}

resource "aws_ecs_service" "ecs-service" {
  name            = "ecs-service"                         
  cluster         = aws_ecs_cluster.appscrip-devops-intern-ecs-cluster.id             
  task_definition = aws_ecs_task_definition.ecs-task-definition.arn 
  launch_type     = "FARGATE"
  desired_count   = 3 

  load_balancer {
    target_group_arn = aws_lb_target_group.target_group.arn 
    container_name   = "hello-world-app"
    container_port   = 3000 
  }

  network_configuration {
    subnets = [ "subnet-a1f706dc", "subnet-d0b6ef9c", "subnet-2ada6141"]
    assign_public_ip = true                                                # Providing our containers with public IPs
    security_groups  = [aws_security_group.service_security_group.id] # Setting the security group
  }

  deployment_controller {
      type = "CODE_DEPLOY"
  }

  depends_on = [ aws_lb_target_group.target_group ]
}


resource "aws_iam_role" "example" {
  name = "example-role"

  assume_role_policy = <<EOF
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "",
      "Effect": "Allow",
      "Principal": {
        "Service": [
          "codedeploy.amazonaws.com",
          "ec2.amazonaws.com"
        ]
      },
      "Action": "sts:AssumeRole"
    }
  ]
}
EOF
}
resource "aws_iam_role_policy" "AWSCodeDeployRole" {
  role       = aws_iam_role.example.name
  policy = <<POLICY
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Action": [
        "cloudwatch:DescribeAlarms",
        "ecs:CreateTaskSet",
        "ecs:DeleteTaskSet",
        "ecs:DescribeServices",
        "ecs:UpdateServicePrimaryTaskSet",
        "elasticloadbalancing:DescribeListeners",
        "elasticloadbalancing:DescribeRules",
        "elasticloadbalancing:DescribeTargetGroups",
        "elasticloadbalancing:ModifyListener",
        "elasticloadbalancing:ModifyRule",
        "lambda:InvokeFunction",
        "s3:GetObject",
        "s3:GetObjectMetadata",
        "s3:GetObjectVersion",
        "sns:Publish"
      ],
      "Effect": "Allow",
      "Resource": "*"
    }
  ]
}
POLICY
}

resource "aws_codedeploy_app" "example" {
  compute_platform = "ECS"
  name = "example-app"
}

resource "aws_codedeploy_deployment_group" "example" {
  app_name              = aws_codedeploy_app.example.name
  deployment_config_name = "CodeDeployDefault.ECSAllAtOnce"
  deployment_group_name = "example"
  service_role_arn      = aws_iam_role.example.arn

  blue_green_deployment_config {
    deployment_ready_option {
      action_on_timeout = "CONTINUE_DEPLOYMENT"
    }

    terminate_blue_instances_on_deployment_success {
      action                           = "TERMINATE"
      termination_wait_time_in_minutes = 5
    }
  }

  deployment_style {
    deployment_option = "WITH_TRAFFIC_CONTROL"
    deployment_type   = "BLUE_GREEN"
  }

  ecs_service {
    cluster_name = aws_ecs_cluster.appscrip-devops-intern-ecs-cluster.name
    service_name = aws_ecs_service.ecs-service.name
  }

  load_balancer_info {
    target_group_pair_info {
      prod_traffic_route {
        listener_arns = [aws_lb_listener.listener.arn]
      }

      target_group {
        name = aws_lb_target_group.target_group.name
      }

      target_group {
        name = aws_lb_target_group.target_group1.name
      }
    }
  }

  auto_rollback_configuration {
    enabled = true
    events  = ["DEPLOYMENT_FAILURE"]
  }

}
4

0 回答 0