> For the complete documentation index, see [llms.txt](https://developer.harness.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://developer.harness.io/continuous-delivery/new-experience/use-deployments/ecs-deployments/ecs-deployment-strategies/blue-green-swap.md). Agents can also search and read this documentation through its [MCP server](https://developer.harness.io/~gitbook/mcp).

# ECS BlueGreen Deploy With Swap Strategy

Deploy an ECS service using the blue-green swap target groups strategy, which deploys to a staging slot and swaps ALB target groups to shift all production traffic at once.

The ECS BlueGreen Deploy With Swap Strategy deploys the new version to a staging ECS service behind the staging ALB listener, then swaps the target groups on the production and staging listeners in a single operation. After the swap, the new service receives all production traffic.

***

## What you will learn from this topic

* What steps Harness adds to your stage when you select the ECS BlueGreen Deploy With Swap Strategy.
* How to configure the ECS BlueGreen Deploy and ECS BlueGreen Swap Target Groups steps and how rollback works.

***

## Before you begin

* **An ECS service:** Go to [ECS services](https://developer.harness.io/continuous-delivery/new-experience/use-deployments/ecs-deployments/ecs-services.md) to set up a service with a task definition and artifact source.
* **An ECS infrastructure definition:** Go to [ECS infrastructure](https://developer.harness.io/continuous-delivery/new-experience/use-deployments/ecs-deployments/ecs-infrastructure.md) to connect an AWS account and configure authentication.
* **An Application Load Balancer:** The ALB must have a production listener and a staging listener, each with a target group. Harness uses the listener rule ARNs to identify which target group is currently serving production traffic.

***

## Stage YAML

```yaml
pipeline:
  stages:
    - name: ecs-bluegreen
      id: ecs_bluegreen
      service:
        type: ecs
        items:
          - <service-id>
      environment:
        items:
          - id: <environment-id>
            deploy-to: all
      steps:
        - name: ECS BlueGreen Deploy
          id: ecsBluegreenDeployStep
          deploy:
            uses: ecsBluegreenDeployStep
            with:
              load_balancer: my-load-balancer
              traffic_shift: false
              prod_listener_arn: arn:aws:elasticloadbalancing:<region>:<account>:listener/...
              prod_listener_rule_arn: arn:aws:elasticloadbalancing:<region>:<account>:listener-rule/...
              stage_listener_arn: arn:aws:elasticloadbalancing:<region>:<account>:listener/...
              stage_listener_rule_arn: arn:aws:elasticloadbalancing:<region>:<account>:listener-rule/...
              update_green_svc: false
              remove_autoscaling_blue_svc: false
              skip_steady_state: false
              steady_state_timeout: 10m
              task_def_mode: Task Definition
              task_def: ${{serviceOutput.manifests.ecsTaskDefinition.paths}}
              service_def: ${{serviceOutput.manifests.ecsServiceDefinition.paths}}
              connector: ${{infra.connectorRef}}
              region: ${{infra.region}}
              cluster: ${{infra.cluster}}
              log_level: info
        - name: ECS BlueGreen Swap Target Groups
          id: ecsBluegreenSwapTargetGroupsStep
          deploy:
            uses: ecsBluegreenSwapTargetGroupsStep
            with:
              not_downsize_old: false
              downsize_delay: 0s
              skip_steady_state: false
              steady_state_timeout: 10m
              load_balancer: ${{stage.steps.ecsBluegreenDeployStep.output.outputVariables.loadBalancer}}
              prod_listener_arn: ${{stage.steps.ecsBluegreenDeployStep.output.outputVariables.prodListenerArn}}
              prod_listener_rule_arn: ${{stage.steps.ecsBluegreenDeployStep.output.outputVariables.prodListenerRuleArn}}
              prod_target_group_arn: ${{stage.steps.ecsBluegreenDeployStep.output.outputVariables.prodTargetGroupArn}}
              stage_listener_arn: ${{stage.steps.ecsBluegreenDeployStep.output.outputVariables.stageListenerArn}}
              stage_listener_rule_arn: ${{stage.steps.ecsBluegreenDeployStep.output.outputVariables.stageListenerRuleArn}}
              stage_target_group_arn: ${{stage.steps.ecsBluegreenDeployStep.output.outputVariables.stageTargetGroupArn}}
              blue_service: ${{stage.steps.ecsBluegreenDeployStep.output.outputVariables.serviceName}}
              green_service: ${{stage.steps.ecsBluegreenDeployStep.output.outputVariables.greenServiceName}}
              first_deployment: ${{stage.steps.ecsBluegreenDeployStep.output.outputVariables.firstDeployment}}
              connector: ${{infra.connectorRef}}
              region: ${{infra.region}}
              cluster: ${{infra.cluster}}
              log_level: info
      rollback:
        - name: rollback
          id: rollback
          deploy:
            uses: ecsBluegreenRollbackStep
      on-failure:
        errors: all
        action: stage-rollback
```

***

## How this deployment works

When you select this strategy, Harness adds the following steps to your stage by default:

1. **ECS BlueGreen Deploy**: Registers the new task definition, creates or updates the green (staging) service, waits for steady state, and emits output variables used by the rollback step.
2. **ECS BlueGreen Swap Target Groups**: Swaps the production and staging ALB listener target groups. After the swap, the new service is live.

Rollback:

* **ECS BlueGreen Rollback**: Reverts the target group swap and restores the previous service using internal rollback data captured by ECS BlueGreen Deploy and ECS BlueGreen Swap Target Groups.

***

## Configure the ECS BlueGreen Deploy step

The ECS BlueGreen Deploy step is required in this strategy. You must configure the ALB listener ARNs before the pipeline can run.

| Field                       | Required | Description                                                                                                   |
| --------------------------- | -------- | ------------------------------------------------------------------------------------------------------------- |
| **Elastic Load Balancer**   | Yes      | The name of the Application Load Balancer.                                                                    |
| **Prod Listener ARN**       | Yes      | ARN of the production ALB listener.                                                                           |
| **Prod Listener Rule ARN**  | Yes      | ARN of the production listener rule pointing to the current production target group.                          |
| **Stage Listener ARN**      | Yes      | ARN of the staging ALB listener.                                                                              |
| **Stage Listener Rule ARN** | No       | ARN of the staging listener rule. Required when using listener rules rather than the default listener action. |

Go to [ECS BlueGreen Deploy step](https://developer.harness.io/continuous-delivery/new-experience/use-deployments/ecs-deployments/step-library/ecs-bluegreen-deploy.md) for the full field reference.

***

## Configure the ECS BlueGreen Swap Target Groups step

None of the ECS BlueGreen Swap Target Groups step's fields are required. The load balancer, listener, listener rule, and target group ARNs, and the blue and green service names, are all wired from the ECS BlueGreen Deploy step's outputs and auto-resolve without any `with:` configuration.

| Field                                                                           | Required | Description                                                                                                   |
| ------------------------------------------------------------------------------- | -------- | ------------------------------------------------------------------------------------------------------------- |
| **Elastic Load Balancer**                                                       | No       | Wired from the ECS BlueGreen Deploy step's `loadBalancer` output.                                             |
| **Prod Listener ARN**, **Prod Listener Rule ARN**, **Prod Target Group ARN**    | No       | Wired from the matching `prod*` outputs. Identify the target group currently receiving production traffic.    |
| **Stage Listener ARN**, **Stage Listener Rule ARN**, **Stage Target Group ARN** | No       | Wired from the matching `stage*` outputs. Identify the target group the green service was registered against. |
| **Blue Service**, **Green Service**                                             | No       | Wired from the `serviceName` and `greenServiceName` outputs.                                                  |
| **First Deployment**                                                            | No       | Wired from the `firstDeployment` output.                                                                      |
| **Do Not Downsize Old Service**, **Downsize Delay**                             | No       | Control whether and when Harness downsizes the old service after the swap.                                    |

Go to [ECS BlueGreen Swap Target Groups step](https://developer.harness.io/continuous-delivery/new-experience/use-deployments/ecs-deployments/step-library/ecs-bluegreen-swap-target-groups.md) for the full field reference.

***

## Rollback behavior

The ECS BlueGreen Rollback step's fields are optional. When the stage failure strategy triggers rollback, Harness auto-resolves the load balancer, listener and target group ARNs, the blue and green service configurations, and the traffic-shift and new-service-created flags from internal rollback data captured during ECS BlueGreen Deploy and ECS BlueGreen Swap Target Groups, without requiring any `with:` configuration.

If you need to override the auto-resolved values, Harness exposes the captured rollback data through `${{rollback.data.*}}` expressions, for example `${{rollback.data.LOAD_BALANCER}}` and `${{rollback.data.GREEN_SERVICE_CONFIG}}`.

Go to [ECS BlueGreen Rollback step](https://developer.harness.io/continuous-delivery/new-experience/use-deployments/ecs-deployments/step-library/ecs-bluegreen-rollback.md) for the full field reference.

***

## Next steps

* Go to [ECS BlueGreen Deploy With Traffic Shift Strategy](https://developer.harness.io/continuous-delivery/new-experience/use-deployments/ecs-deployments/ecs-deployment-strategies/blue-green-traffic-shift.md) to gradually shift traffic instead of performing an atomic swap.
* Go to [ECS basic deployment](https://developer.harness.io/continuous-delivery/new-experience/use-deployments/ecs-deployments/ecs-deployment-strategies/basic.md) for a simpler upgrade-in-place strategy.
* Go to [ECS step library](https://developer.harness.io/continuous-delivery/new-experience/use-deployments/ecs-deployments/step-library.md) for the full list of available steps.
