Configures a blue-green deployment for ECS services using AWS CodeDeploy, including setup for load balancers and traffic routing.

Terraform Template

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

resource "aws_codedeploy_deployment_group" "example" {
  app_name = aws_codedeploy_app.example.name

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

  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_config_name = "CodeDeployDefault.ECSAllAtOnce"
  deployment_group_name = "example"

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

  ecs_service {
    cluster_name = "aws_ecs_cluster.example.name"
    service_name = "aws_ecs_service.example.name"
  }

  load_balancer_info {

    target_group_pair_info {

      prod_traffic_route {
        listener_arns = ["aws_lb_listener.example.arn"]
      }

      target_group {
        name = "aws_lb_target_group.blue.name"
      }

      target_group {
        name = "aws_lb_target_group.green.name"
      }
    }
  }
  service_role_arn = "aws_iam_role.example.arn"
}