

# Create a deployment group with CodeDeploy
Create a deployment group

You can use the CodeDeploy console, the AWS CLI, the CodeDeploy APIs, or an CloudFormation template to create deployment groups. For information about using an CloudFormation template to create a deployment group, see [CloudFormation templates for CodeDeploy reference](reference-cloudformation-templates.md).

When you use the CodeDeploy console to create an application, you configure its first deployment group at the same time. When you use the AWS CLI to create an application, you create its first deployment group in a separate step.

As part of creating a deployment group, you must specify a service role. For more information, see [Step 2: Create a service role for CodeDeploy](getting-started-create-service-role.md).

**Topics**
+ [

# Create a deployment group for an in-place deployment (console)
](deployment-groups-create-in-place.md)
+ [

# Create a deployment group for an EC2/On-Premises blue/green deployment (console)
](deployment-groups-create-blue-green.md)
+ [

# Create a deployment group for an Amazon ECS deployment (console)
](deployment-groups-create-ecs.md)
+ [

# Set up a load balancer in Elastic Load Balancing for CodeDeploy Amazon EC2 deployments
](deployment-groups-create-load-balancer.md)
+ [

# Set up a load balancer, target groups, and listeners for CodeDeploy Amazon ECS deployments
](deployment-groups-create-load-balancer-for-ecs.md)
+ [

# Create a deployment group (CLI)
](deployment-groups-create-cli.md)

# Create a deployment group for an in-place deployment (console)


To use the CodeDeploy console to create a deployment group for an in-place deployment:

**Warning**  
Do not follow these steps if:  
You have not prepared your instances to be used in the first CodeDeploy deployment of an application. To set up your instances, follow the instructions in [Working with instances for CodeDeploy](instances.md), and then follow the steps in this topic.
You want to create a deployment group that uses a custom deployment configuration, but you have not yet created the deployment configuration. Follow the instructions in [Create a deployment configuration with CodeDeploy](deployment-configurations-create.md), and then follow the steps in this topic. 
You do not have a service role that trusts CodeDeploy with, at minimum, the trust and permissions described in [Step 2: Create a service role for CodeDeploy](getting-started-create-service-role.md). To create and configure a service role, follow the instructions in [Step 2: Create a service role for CodeDeploy](getting-started-create-service-role.md), and then follow the steps in this topic.
You want to select a Classic Load Balancer, Application Load Balancer, or Network Load Balancer in Elastic Load Balancing for the in-place deployment, but have not yet created it.

1. Sign in to the AWS Management Console and open the CodeDeploy console at [https://console.aws.amazon.com/codedeploy](https://console.aws.amazon.com/codedeploy).
**Note**  
Sign in with the same user that you set up in [Getting started with CodeDeploy](getting-started-codedeploy.md).

1. In the navigation pane, expand **Deploy**, then choose **Applications**.

1. On the **Applications** page, choose the name of the application for which you want to create a deployment group.

1. On your application page, from the **Deployment groups** tab, choose **Create deployment group**.

1. In **Deployment group name**, enter a name that describes the deployment group.
**Note**  
If you want to use the same settings used in another deployment group (including the deployment group name; tags, Amazon EC2 Auto Scaling group names, or both; and the deployment configuration), specify those settings on this page. Although this new deployment group and the existing deployment group have the same name, CodeDeploy treats them as separate deployment groups, because they are each associated with separate applications.

1. In **Service role**, choose a service role that grants CodeDeploy access to your target instance.

1. In **Deployment type**, choose **In-place**.

1. In **Environment configuration**, do the following: 

   1. If you want to deploy your application to an Amazon EC2 Auto Scaling group, select **Amazon EC2 Auto Scaling groups**, and then choose the name of an Amazon EC2 Auto Scaling group to deploy your application revision to. When new Amazon EC2 instances are launched as part of an Amazon EC2 Auto Scaling group, CodeDeploy can deploy your revisions to the new instances automatically. You can add up to 10 Amazon EC2 Auto Scaling groups to a deployment group. For more information, see [Integrating CodeDeploy with Amazon EC2 Auto Scaling](integrations-aws-auto-scaling.md).

   1. 

      If you selected **Amazon EC2 Auto Scaling groups**, optionally select **Add a termination hook to Auto Scaling groups** to have CodeDeploy install a termination hook into your Auto Scaling group when you create or update the deployment group. When this hook is installed, CodeDeploy will perform termination deployments. For more information, see [Enabling termination deployments during Auto Scaling scale-in events](integrations-aws-auto-scaling.md#integrations-aws-auto-scaling-behaviors-hook-enable).

   1. If you want to tag your instances, select **Amazon EC2 instances** or **On-premises instances**. In the **Key** and **Value** fields, enter the values of the key-value pair you used to tag the instances. You can tag up to 10 key-value pairs in a single tag group.

      1. You can use wildcards in the **Value** field to identify all instances tagged in certain patterns, such as similar Amazon EC2 instance, cost center, and group names, and so on. For example, if you choose **Name** in the **Key** field and enter **GRP-\$1a** in the **Value** field, CodeDeploy identifies all instances that fit that pattern, such as **GRP-1a**, **GRP-2a**, and **GRP-XYZ-a**.

      1. The **Value** field is case sensitive. 

      1. To remove a key-value pair from the list, choose the remove icon.

      As CodeDeploy finds instances that match each specified key-value pair or Amazon EC2 Auto Scaling group name, it displays the number of matching instances. To see more information about the instances, click the number.

      If you want to refine the criteria for the deployed-to instances, choose **Add tag group** to create an tag group. You can create up to three tag groups with up to 10 key-value pairs each. When you use multiple tag groups in a deployment group, only instances that are identified by all the tag groups are included in the deployment group. That means an instance must match at least one of the tags in each of the groups to be included in the deployment group.

      For information about using tag groups to refine your deployment group, see [Tagging instances for deployment groups in CodeDeploy](instances-tagging.md).

1. In **Agent configuration with Systems Manager**, specify how you would like to install and update the CodeDeploy agent on the instances in your deployment group. For more information on the CodeDeploy agent, see [Working with the CodeDeploy agent](https://docs.aws.amazon.com/en_us/codedeploy/latest/userguide/codedeploy-agent.html). For more information about Systems Manager, see [What is Systems Manager?](https://docs.aws.amazon.com/systems-manager/latest/userguide/what-is-systems-manager.html)

   1. **Never**: Skip configuring the CodeDeploy installation with Systems Manager. Instances must have the agent installed to be used in deployments, so only choose this option if you will install the CodeDeploy agent another way.

   1. **Only once**: Systems Manager will install the CodeDeploy agent once on every instance in your deployment group.

   1. **Now and schedule updates**: Systems Manager will create an association with State Manager that installs the CodeDeploy agent on the schedule you configure. For more information about State Manager and associations, see [About State Manager](https://docs.aws.amazon.com/systems-manager/latest/userguide/sysman-state-about.html).

1. In **Deployment configuration**, choose a deployment configuration to control the rate at which instances are deployed to, such as one at a time or all at once. For more information about deployment configurations, see [Working with deployment configurations in CodeDeploy](deployment-configurations.md).

1. (Optional) In **Load balancer**, select **Enable load balancing**, and then from the lists, select the Classic Load Balancers, Application Load Balancer target groups, and Network Load Balancer target groups to manage traffic to the instances during the CodeDeploy deployment. You can select up to 10 Classic Load Balancers and 10 target groups, for a total of 20 items. Make sure that the Amazon EC2 instances that you want to deploy to are registered with the selected load balancers (Classic Load Balancers) or target groups (Application Load Balancers and Network Load Balancers).

   During a deployment, the original instances are deregistered from the selected load balancers and target groups to prevent traffic from being routed to these instances during the deployment. When the deployment is complete, each instance is re-registered with *all* the selected Classic Load Balancers and target groups.

   For more information about load balancers for CodeDeploy deployments, see [Integrating CodeDeploy with Elastic Load Balancing](integrations-aws-elastic-load-balancing.md).
**Warning**  
If you are configuring both Auto Scaling groups and an Elastic Load Balancing load balancers in this deployment group, and you want to [attach load balancers to the Auto Scaling groups](https://docs.aws.amazon.com/autoscaling/ec2/userguide/attach-load-balancer-asg.html), we recommend completing this attachment *before* creating the CodeDeploy deployment from this deployment group. Attempting to complete the attachment after creating the deployment may cause all the instances to become deregistered from the load balancers unexpectedly.

1. (Optional) Expand **Advanced** and configure any options you want to include in the deployment, such as Amazon SNS notification triggers, Amazon CloudWatch alarms, Auto Scaling options, or automatic rollbacks.

   For more information, see [Configure advanced options for a deployment group](deployment-groups-configure-advanced-options.md). 

1. Choose **Create deployment group**. 

# Create a deployment group for an EC2/On-Premises blue/green deployment (console)


To use the CodeDeploy console to create a deployment group for a blue/green deployment:

**Warning**  
Do not follow these steps if:  
You do not have instances with the CodeDeploy agent installed that you want to replace during the blue/green deployment process. To set up your instances, follow the instructions in [Working with instances for CodeDeploy](instances.md), and then follow the steps in this topic.
You want to create an application that uses a custom deployment configuration, but you have not yet created the deployment configuration. Follow the instructions in [Create a deployment configuration with CodeDeploy](deployment-configurations-create.md), and then follow the steps in this topic. 
You do not have a service role that trusts CodeDeploy with, at minimum, the trust and permissions described in [Step 2: Create a service role for CodeDeploy](getting-started-create-service-role.md). To create and configure a service role, follow the instructions in [Step 2: Create a service role for CodeDeploy](getting-started-create-service-role.md), and then follow the steps in this topic.
You have not created a Classic Load Balancer or an Application Load Balancer in Elastic Load Balancing for the registration of the instances in your replacement environment. For more information, see [Set up a load balancer in Elastic Load Balancing for CodeDeploy Amazon EC2 deployments](deployment-groups-create-load-balancer.md).

1. Sign in to the AWS Management Console and open the CodeDeploy console at [https://console.aws.amazon.com/codedeploy](https://console.aws.amazon.com/codedeploy).
**Note**  
Sign in with the same user that you set up in [Getting started with CodeDeploy](getting-started-codedeploy.md).

1. In the navigation pane, expand **Deploy**, then choose **Applications**.

1. On the **Applications** page, choose the name of the application for which you want to create a deployment group.

1. On your application page, from the **Deployment groups** tab, choose **Create deployment group**.

1. In **Deployment group name**, enter a name that describes the deployment group.
**Note**  
If you want to use the same settings used in another deployment group (including the deployment group name, tags, Amazon EC2 Auto Scaling group names, and the deployment configuration), choose those settings on this page. Although this new deployment group and the existing deployment group have the same name, CodeDeploy treats them as separate deployment groups, because they are associated with separate applications.

1. In **Service role**, choose a service role that grants CodeDeploy access to your target instance.

1. In **Deployment type** choose **Blue/green**.

1. In **Environment configuration**, do the following:
   + Select the method to use to provide instances for your replacement environment. You have the following options:
     + **Automatically copy Amazon EC2 Auto Scaling group**: CodeDeploy creates an Amazon EC2 Auto Scaling group by copying one you specify.
     + **Manually provision instances**: You won't specify the instances for your replacement environment until you create a deployment. You must create the instances before you start the deployment. Instead, here you specify the instances you want to replace.
   + If you selected **Automatically copy Amazon EC2 Auto Scaling group**, optionally select **Add a termination hook to Auto Scaling groups** to have CodeDeploy install a termination hook into your Auto Scaling group when you create or update the deployment group. When this hook is installed, CodeDeploy will perform termination deployments. For more information, see [Enabling termination deployments during Auto Scaling scale-in events](integrations-aws-auto-scaling.md#integrations-aws-auto-scaling-behaviors-hook-enable).

1. In **Agent configuration with Systems Manager**, specify how you would like to install and update the CodeDeploy agent on the instances in your deployment group. For more information on the CodeDeploy agent, see [Working with the CodeDeploy agent](https://docs.aws.amazon.com/en_us/codedeploy/latest/userguide/codedeploy-agent.html). For more information about Systems Manager, see [What is Systems Manager?](https://docs.aws.amazon.com/systems-manager/latest/userguide/what-is-systems-manager.html)

   1. **Never**: Skip configuring the CodeDeploy installation with Systems Manager. Instances must have the agent installed to be used in deployments, so only choose this option if you will install the CodeDeploy agent another way.

   1. **Only once**: Systems Manager will install the CodeDeploy agent once on every instance in your deployment group.

   1. **Now and schedule updates**: Systems Manager will create an association with State Manager that installs the CodeDeploy agent on the schedule you configure. For more information about State Manager and associations, see [About State Manager](https://docs.aws.amazon.com/systems-manager/latest/userguide/sysman-state-about.html).

1. Depending on your choice in step 8, do one of the following:
   + If you chose **Automatically copy Amazon EC2 Auto Scaling group**: In **Amazon EC2 Auto Scaling group**, choose or enter the name of the Amazon EC2 Auto Scaling group you want to use as a template for the Amazon EC2 Auto Scaling group that is created for the instances in your replacement environment. The number of currently healthy instances in the Amazon EC2 Auto Scaling group you select is created in your replacement environment.
   + If you chose **Manually provision instances**: Select **Amazon EC2 Auto Scaling groups**, **Amazon EC2 Auto Scaling intances**, or both to specify instances to add to this deployment group. Enter Amazon EC2 Auto Scaling tag values or Amazon EC2 Auto Scaling group names to identify the instances in your original environment (that is, the instances you want to replace or that are running the current application revision). 

1. In **Load balancer**, select **Enable load balancing**, and then from the lists, select the Classic Load Balancers, Application Load Balancer target groups, and Network Load Balancer target groups that you want to register your replacement Amazon EC2 instances with. Each replacement instance will be registered with *all* the selected Classic Load Balancers and target groups. You can select up to 10 Classic Load Balancers and 10 target groups, for a total of 20 items.

   Traffic will be rerouted from the original to the replacement instances according to your chosen **Traffic rerouting** and **Deployment configuration** settings.

   For more information about load balancers for CodeDeploy deployments, see [Integrating CodeDeploy with Elastic Load Balancing](integrations-aws-elastic-load-balancing.md).
**Warning**  
If you are configuring both Auto Scaling groups and Elastic Load Balancing load balancers in this deployment group, and you want to [attach the load balancers to Auto Scaling groups](https://docs.aws.amazon.com/autoscaling/ec2/userguide/attach-load-balancer-asg.html), we recommend completing this attachment *before* creating the CodeDeploy deployment from this deployment group. Attempting to complete the attachment after creating the deployment may cause all the instances to become deregistered from the load balancers unexpectedly.

1. In **Deployment settings**, review the default options for rerouting traffic to the replacement environment, which deployment configuration to use for the deployment, and how instances in the original environment are handled after the deployment.

   If you want to change the settings, continue to the next step. Otherwise, skip to step 14.

1. To change the deployment settings for the blue/green deployment, choose any of the following settings.    
[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/codedeploy/latest/userguide/deployment-groups-create-blue-green.html)

1. (Optional) In **Advanced**, configure options you want to include in the deployment, such as Amazon SNS notification triggers, Amazon CloudWatch alarms, Auto Scaling options, or automatic rollbacks.

   For information about specifying advanced options in deployment groups, see [Configure advanced options for a deployment group](deployment-groups-configure-advanced-options.md). 

1. Choose **Create deployment group**. 

# Create a deployment group for an Amazon ECS deployment (console)


1. Sign in to the AWS Management Console and open the CodeDeploy console at [https://console.aws.amazon.com/codedeploy](https://console.aws.amazon.com/codedeploy).
**Note**  
Sign in with the same user that you set up in [Getting started with CodeDeploy](getting-started-codedeploy.md).

1. In the navigation pane, expand **Deploy**, then choose **Applications**.

1.  From the **Applications table**, choose the name of the application associated with the deployment group you want to edit. 

1.  On your application page, from **Deployment groups**, choose the name of the deployment group you want to edit. 

1. On your application page, from the **Deployment groups** tab, choose **Create deployment group**. For more information about what you need to create a deployment group for an Amazon ECS deployment, see [Before you begin an Amazon ECS deployment](deployment-steps-ecs.md#deployment-steps-prerequisites-ecs). 

1. In **Deployment group name**, enter a name that describes the deployment group.
**Note**  
If you want to use the same settings used in another deployment group (including the deployment group name and the deployment configuration), choose those settings on this page. Although this new group and the existing group might have the same name, CodeDeploy treats them as separate deployment groups, because each is associated with a separate application.

1. In **Service role**, choose a service role that grants CodeDeploy access to Amazon ECS. For more information, see [Step 2: Create a service role for CodeDeploy](getting-started-create-service-role.md).

1.  From **Load balancer name**, choose the name of the load balancer that serves traffic to your Amazon ECS service. 

1.  From **Production listener port**, choose the port and protocol for the listener that serves production traffic to your Amazon ECS service. 

1.  (Optional) From **Test listener port**, choose the port and protocol of a test listener that serves traffic to the replacement task set in your Amazon ECS service during deployment. You can specify one or more Lambda funtions in the AppSpec file that run during the `AfterAllowTestTraffic` hook. The functions can run validation tests. If a validation test fails, a deployment rollback is triggered. If the validation tests succeed, the next hook in the deployment lifecycle, `BeforeAllowTraffic`, is triggered. If a test listener port is not specified, nothing happens during the `AfterAllowTestTraffic` hook. For more information, see [AppSpec 'hooks' section for an Amazon ECS deployment](reference-appspec-file-structure-hooks.md#appspec-hooks-ecs). 

1. From **Target group 1 name** and **Target group 2 name**, choose the target groups used to route traffic during your deployment. CodeDeploy binds one target group to your Amazon ECS service's original task set and the other to its replacement task set. For more information, see [Target Groups for Your Application Load Balancers](https://docs.aws.amazon.com/elasticloadbalancing/latest/application/load-balancer-target-groups.html). 

1.  Choose **Reroute traffic immediately** or **Specify when to reroute traffic** to determine when to reroute traffic to your updated Amazon ECS service. 

    If you choose **Reroute traffic immediately**, then the deployment automatically reroutes traffic after the replacement task set is provisioned. 

    If you choose **Specify when to reroute traffic**, then choose the number of days, hours, and minutes to wait after the replacement task set is successfully provisioned. During this wait time, validation tests in Lambda functions specified in the AppSpec file are executed. If the wait time expires before traffic is rerouted, then the deployment status changes to `Stopped`. 

1.  For **Original revision termination**, choose the number of days, hours, and minutes to wait after a successful deployment before the original task set in your Amazon ECS service is terminated. 

1. (Optional) In **Advanced**, configure any options you want to include in the deployment, such as Amazon SNS notification triggers, Amazon CloudWatch alarms, or automatic rollbacks.

   For more information, see [Configure advanced options for a deployment group](deployment-groups-configure-advanced-options.md). 

# Set up a load balancer in Elastic Load Balancing for CodeDeploy Amazon EC2 deployments


Before you run any blue/green deployment, or an in-place deployment for which you want to specify an optional load balancer in the deployment group, you must have created at least one Classic Load Balancer, Application Load Balancer, or Network Load Balancer in Elastic Load Balancing. For blue/green deployments, you use that load balancer to register the instances that make up your replacement environment. Instances in your original environment can optionally be registered with this same load balancer. For in-place deployments, the load balancer is used to deregister instances that are being worked on by CodeDeploy, and reregister them when the work is complete.

CodeDeploy supports blue/green and in-place deployment to Amazon EC2 instances behind muliple load balancers. For example, assume you have 200 Amazon EC2 instances, where 100 of them are registered with 2 Classic Load Balancers, and another 100 of them are registered with 4 target groups in 2 Application Load Balancers. In this scenario, CodeDeploy will allow you to do blue/green and in-place deployments to all 200 instances, even though they're spread across 2 Classic Load Balancers, 2 Application Load Balancers, and 4 target groups.

CodeDeploy supports up to 10 Classic Load Balancers and 10 target groups, for a total of 20 items.

To configure one or more Classic Load Balancers, follow the instructions in [Tutorial: Create a Classic Load Balancer](https://docs.aws.amazon.com/elasticloadbalancing/latest/classic/elb-getting-started.html) in *User Guide for Classic Load Balancers*. Note the following:
+ In **Step 2: Define Load Balancer**, in **Create LB Inside**, choose the same VPC you selected when you created your instances.
+ In **Step 5: Register EC2 Instances with Your Load Balancer**, select the instances currently in your deployment group (in-place deployments) or that you have designated to be in your original environment (blue/green deployments).
+ In **Step 7: Create and Verify Your Load Balancer**, make a note of the DNS address of your load balancer.

  For example, if you named your load balancer `my-load-balancer`, your DNS address appears in a format such as `my-load-balancer-1234567890.us-east-2.elb.amazonaws.com`.

To configure one or more Application Load Balancers, follow the instructions in one of the following topics:
+ [Create an Application Load Balancer](https://docs.aws.amazon.com/elasticloadbalancing/latest/application/create-application-load-balancer.html)
+ [Tutorial: Create an Application Load Balancer using the AWS CLI](https://docs.aws.amazon.com/elasticloadbalancing/latest/application/tutorial-application-load-balancer-cli.html)

To configure one or more Network Load Balancers, follow the instructions in one of the following topics:
+  [Create a Network Load Balancer](https://docs.aws.amazon.com/elasticloadbalancing/latest/network/create-network-load-balancer.html) 
+  [Tutorial: Create a Network Load Balancer using the AWS CLI](https://docs.aws.amazon.com/elasticloadbalancing/latest/network/network-load-balancer-cli.html) 

# Set up a load balancer, target groups, and listeners for CodeDeploy Amazon ECS deployments


 Before you run a deployment using the Amazon ECS compute platform, you must create an Application Load Balancer or a Network Load Balancer, two target groups, and one or two listeners. This topic shows you how to create an Application Load Balancer. For more information, see [Before you begin an Amazon ECS deployment](deployment-steps-ecs.md#deployment-steps-prerequisites-ecs). 

 One of the target groups directs traffic to your Amazon ECS application's original task set. The other target group directs traffic to its replacement task set. During deployment, CodeDeploy creates a replacement task set and reroutes traffic from the original task set to the new one. CodeDeploy determines which target group is used for each task set. 

 A listener is used by your load balancer to direct traffic to your target groups. One production listener is required. You can specify an optional test listener that directs traffic to your replacement task set while you run validation tests. 

 The load balancer must use a VPC with two public subnets in different Availability Zones. The following steps show you how to confirm your default VPC, create an Amazon EC2 Application Load Balancer, and then create two target groups for your load balancer. For more information, see [Target groups for your network load balancers](https://docs.aws.amazon.com/elasticloadbalancing/latest/network/load-balancer-target-groups.html). 

## Verify your default VPC, public subnets, and security group


 This topic shows how to create an Amazon EC2 Application Load Balancer, two target groups, and two ports that can be used during an Amazon ECS deloyment. One of the ports is optional and needed only if you direct traffic to a test port for validation tests during your deployment. 

1. Sign in to the AWS Management Console and open the Amazon VPC console at [https://console.aws.amazon.com/vpc/](https://console.aws.amazon.com/vpc/).

1. Verify the default VPC to use. In the navigation pane, choose **Your VPCs**. Note which VPC shows **Yes** in the **Default VPC** column. This is your default VPC. It contains default subnets that you use.

1. Choose **Subnets**. Make a note of the subnet IDs of two subnets that show **Yes** in the **Default subnet** column. You use these IDs when you create your load balancer.

1. Choose each subnet, and then choose the **Description** tab. Verify that the subnets you want to use are in different Availability Zones.

1. Choose the subnets, and then choose the **Route Table** tab. To verify that each subnet you want to use is a public subnet, confirm that a row with a link to an internet gateway is included in the route table.

1. Sign in to the AWS Management Console and open the Amazon EC2 console at [https://console.aws.amazon.com/ec2/](https://console.aws.amazon.com/ec2/).

1. From the navigation pane, choose **Security Groups**.

1. Verify the security group you want to use is available and make a note of its group ID (for example, **sg-abcd1234**). You use this when you create your load balancer.

## Create an Amazon EC2 Application Load Balancer, two target groups, and listeners (console)


To use the Amazon EC2 console to create an Amazon EC2 Application Load Balancer:

1. Sign in to the AWS Management Console and open the Amazon EC2 console at [https://console.aws.amazon.com/ec2/](https://console.aws.amazon.com/ec2/).

1. In the navigation pane, choose **Load Balancers**. 

1. Choose **Create Load Balancer**.

1. Choose **Application Load Balancer**, and then choose **Create**.

1. In **Name**, enter the name of your load balancer.

1. In **Scheme**, choose **internet-facing**.

1. In **IP address type**, choose **ipv4**.

1. (Optional) Configure a second listener port for your load balancer. You can run deployment validation tests using test traffic that is served to this port.

   1. Under **Load Balancer Protocol**, choose **Add listener**.

   1. Under **Load Balancer Protocol** for the second listener, choose **HTTP**. 

   1. Under **Load Balancer Port**, enter **8080**.

1. Under **Availability Zones**, in **VPC**, choose the default VPC, and then select the two default subnets you want to use.

1. Choose **Next: Configure Security Settings**.

1. Choose **Next: Configure Security Groups**.

1. Choose **Select an existing security group**, choose the default security group, and then make a note of its ID.

1. Choose **Next: Configure Routing**.

1. In **Target group**, choose **New target group**, and configure your first target group: 

   1. In **Name**, enter a target group name (for example, **target-group-1**).

   1. In **Target type**, choose **IP**.

   1. In **Protocol** choose **HTTP**. In **Port**, enter **80**.

   1. Choose **Next: Register Targets**.

1. Choose **Next: Review**, and then choose **Create**.

**To create a second target group for your load balancer**

1. After your load balancer is provisioned, open the Amazon EC2 console. In the navigation pane, choose **Target Groups**.

1. Choose **Create target group**.

1. In **Name**, enter a target group name (for example, **target-group-2**).

1. In **Target type**, choose **IP**.

1. In **Protocol** choose **HTTP**. In **Port**, enter **80**.

1. In **VPC**, choose the default VPC.

1. Choose **Create**.
**Note**  
You must have two target groups created for your load balancer in order for your Amazon ECS deployment to run. You use the ARN of one of your target groups when you create your Amazon ECS service. For more information, see [Step 4: Create an Amazon ECS service](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/create-blue-green.html#create-blue-green-taskdef) in the *Amazon ECS User Guide*.

## Create an Amazon EC2 Application Load Balancer, two target groups, and listeners (CLI)


To create an Application Load Balancer using the AWS CLI:

1. Use the [create-load-balancer](https://docs.aws.amazon.com/cli/latest/reference/elbv2/create-load-balancer.html) command to create an Application Load Balancer. Specify two subnets that aren't in the same Availability Zone and a security group.

   ```
   aws elbv2 create-load-balancer --name bluegreen-alb \
   --subnets subnet-abcd1234 subnet-abcd5678 --security-groups sg-abcd1234 --region us-east-1
   ```

   The output includes the Amazon Resource Name (ARN) of the load balancer, in the following format:

   ```
   arn:aws:elasticloadbalancing:region:aws_account_id:loadbalancer/app/bluegreen-alb/e5ba62739c16e642
   ```

1. Use the [create-target-group](https://docs.aws.amazon.com/cli/latest/reference/elbv2/create-target-group.html) command to create your first target group. CodeDeploy routes this target group's traffic to the original or the replacement task set in your service.

   ```
   aws elbv2 create-target-group --name bluegreentarget1 --protocol HTTP --port 80 \
   --target-type ip --vpc-id vpc-abcd1234 --region us-east-1
   ```

   The output includes the ARN of the first target group, in the following format:

   ```
   arn:aws:elasticloadbalancing:region:aws_account_id:targetgroup/bluegreentarget1/209a844cd01825a4
   ```

1. Use the [create-target-group](https://docs.aws.amazon.com/cli/latest/reference/elbv2/create-target-group.html) command to create your second target group. CodeDeploy routes target group's traffic to the task set that is not served by your first target group. For example, if your first target group routes traffic to the original task set, this target group routes traffic to the replacement task set.

   ```
   aws elbv2 create-target-group --name bluegreentarget2 --protocol HTTP --port 80 \
   --target-type ip --vpc-id vpc-abcd1234 --region us-east-1
   ```

   The output includes the ARN of the second target group, in the following format:

   ```
   arn:aws:elasticloadbalancing:region:aws_account_id:targetgroup/bluegreentarget2/209a844cd01825a4
   ```

1. Use the [create-listener](https://docs.aws.amazon.com/cli/latest/reference/elbv2/create-listener.html) command to create a listener with a default rule that forwards production traffic to port 80.

   ```
   aws elbv2 create-listener --load-balancer-arn arn:aws:elasticloadbalancing:region:aws_account_id:loadbalancer/app/bluegreen-alb/e5ba62739c16e642 \
   --protocol HTTP --port 80 \
   --default-actions Type=forward,TargetGroupArn=arn:aws:elasticloadbalancing:region:aws_account_id:targetgroup/bluegreentarget1/209a844cd01825a4 --region us-east-1
   ```

   The output includes the ARN of the listener, in the following format:

   ```
   arn:aws:elasticloadbalancing:region:aws_account_id:listener/app/bluegreen-alb/e5ba62739c16e642/665750bec1b03bd4
   ```

1. (Optional) Use the [create-listener](https://docs.aws.amazon.com/cli/latest/reference/elbv2/create-listener.html) command to create a second listener with a default rule that forwards test traffic to port 8080. You can run deployment validation tests using test traffic that is served this port.

   ```
   aws elbv2 create-listener --load-balancer-arn arn:aws:elasticloadbalancing:region:aws_account_id:loadbalancer/app/bluegreen-alb/e5ba62739c16e642 \
   --protocol HTTP --port 8080 \
   --default-actions Type=forward,TargetGroupArn=arn:aws:elasticloadbalancing:region:aws_account_id:targetgroup/bluegreentarget2/209a844cd01825a4 --region us-east-1
   ```

   The output includes the ARN of the listener, in the following format:

   ```
   arn:aws:elasticloadbalancing:region:aws_account_id:listener/app/bluegreen-alb/e5ba62739c16e642/665750bec1b03bd4
   ```

# Create a deployment group (CLI)


To use the AWS CLI to create a deployment group, call the [create-deployment-group](https://docs.aws.amazon.com/cli/latest/reference/deploy/create-deployment-group.html) command, specifying:
+ The application name. To view a list of application names, call the [list-applications](https://docs.aws.amazon.com/cli/latest/reference/deploy/list-applications.html) command.
+ A name for the deployment group. A deployment group with this name is created for the specified application. A deployment group can only be associated with one application.
+ Information about the tags, tag groups, or Amazon EC2 Auto Scaling group names that identify the instances to be included in the deployment group.
+ The Amazon Resource Name (ARN) identifier of the service role that allows CodeDeploy to act on behalf of your AWS account when interacting with other AWS services. To get the service role ARN, see [Get the service role ARN (CLI)](getting-started-create-service-role.md#getting-started-get-service-role-cli). For more information about service roles, see [Roles terms and concepts](https://docs.aws.amazon.com/IAM/latest/UserGuide/roles-toplevel.html#roles-about-termsandconcepts) in *IAM User Guide*.
+ Information about the type of deployment, either in-place or blue/green, to associate with the deployment group.
+ (Optional) The name of an existing deployment configuration. To view a list of deployment configurations, see [View deployment configuration details with CodeDeploy](deployment-configurations-view-details.md). If not specified, CodeDeploy uses a default deployment configuration.
+ (Optional) Commands to create a trigger that pushes notifications about deployment and instance events to those who are subscribed to an Amazon Simple Notification Service topic. For more information, see [Monitoring deployments with Amazon SNS event notifications](monitoring-sns-event-notifications.md).
+ (Optional) Commands to add existing CloudWatch alarms to the deployment group that are activated if a metric specified in an alarm falls below or exceeds a defined threshold.
+ (Optional) Commands for a deployment to roll back to the last known good revision when a deployment fails or a CloudWatch alarm is activated.
+ (Optional) Commands for a deployment to generate lifecycle event hooks during an Auto Scaling scale-in event. For more information, see [How Amazon EC2 Auto Scaling works with CodeDeploy](integrations-aws-auto-scaling.md#integrations-aws-auto-scaling-behaviors).
+ For in-place deployments:
  + (Optional) The names of the Classic Load Balancers, Application Load Balancers, or Network Load Balancers in Elastic Load Balancing that manage traffic to the instances during the deployment processes.
+ For blue/green deployments:
  + Configuration of the blue/green deployment process:
    + How new instances in the replacement environment are provisioned.
    + Whether to reroute traffic to the replacement environment immediately or wait a specified period for traffic to be rerouted manually.
    + Whether instances in the original environment should be terminated. 
  + The names of the Classic Load Balancers, Application Load Balancers, or Network Load Balancers in Elastic Load Balancing to be used for instances registered in the replacement environment.
**Warning**  
If you are configuring both an Auto Scaling group and an Elastic Load Balancing load balancer in your deployment group, and you want to [attach the load balancer to the Auto Scaling group](https://docs.aws.amazon.com/autoscaling/ec2/userguide/attach-load-balancer-asg.html), we recommend completing this attachment *before* creating the CodeDeploy deployment from this deployment group. Attempting to complete the attachment after creating the deployment may cause all the instances to become deregistered from the load balancer unexpectedly.