Resource: aws_codedeploy_deployment_group

Provides a CodeDeploy Deployment Group for a CodeDeploy Application

Example Usage

data "aws_iam_policy_document" "assume_role" {
  statement {
    effect = "Allow"

    principals {
      type        = "Service"
      identifiers = ["codedeploy.amazonaws.com"]
    }

    actions = ["sts:AssumeRole"]
  }
}

resource "aws_iam_role" "example" {
  name               = "example-role"
  assume_role_policy = data.aws_iam_policy_document.assume_role.json
}

resource "aws_iam_role_policy_attachment" "AWSCodeDeployRole" {
  policy_arn = "arn:aws:iam::aws:policy/service-role/AWSCodeDeployRole"
  role       = aws_iam_role.example.name
}

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

resource "aws_sns_topic" "example" {
  name = "example-topic"
}

resource "aws_codedeploy_deployment_group" "example" {
  app_name              = aws_codedeploy_app.example.name
  deployment_group_name = "example-group"
  service_role_arn      = aws_iam_role.example.arn

  ec2_tag_set {
    ec2_tag_filter {
      key   = "filterkey1"
      type  = "KEY_AND_VALUE"
      value = "filtervalue"
    }

    ec2_tag_filter {
      key   = "filterkey2"
      type  = "KEY_AND_VALUE"
      value = "filtervalue"
    }
  }

  trigger_configuration {
    trigger_events     = ["DeploymentFailure"]
    trigger_name       = "example-trigger"
    trigger_target_arn = aws_sns_topic.example.arn
  }

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

  alarm_configuration {
    alarms  = ["my-alarm-name"]
    enabled = true
  }

  outdated_instances_strategy = "UPDATE"

}

Blue Green Deployments with ECS

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

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

  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_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
      }
    }
  }
}

Blue Green Deployments with Servers and Classic ELB

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

resource "aws_codedeploy_deployment_group" "example" {
  app_name              = aws_codedeploy_app.example.name
  deployment_group_name = "example-group"
  service_role_arn      = aws_iam_role.example.arn

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

  load_balancer_info {
    elb_info {
      name = aws_elb.example.name
    }
  }

  blue_green_deployment_config {
    deployment_ready_option {
      action_on_timeout    = "STOP_DEPLOYMENT"
      wait_time_in_minutes = 60
    }

    green_fleet_provisioning_option {
      action = "DISCOVER_EXISTING"
    }

    terminate_blue_instances_on_deployment_success {
      action = "KEEP_ALIVE"
    }
  }
}

Argument Reference

This resource supports the following arguments:

alarm_configuration Argument Reference

You can configure a deployment to stop when a CloudWatch alarm detects that a metric has fallen below or exceeded a defined threshold. alarm_configuration supports the following:

_Only one alarm_configuration is allowed_.

auto_rollback_configuration Argument Reference

You can configure a deployment group to automatically rollback when a deployment fails or when a monitoring threshold you specify is met. In this case, the last known good version of an application revision is deployed. auto_rollback_configuration supports the following:

_Only one auto_rollback_configuration is allowed_.

blue_green_deployment_config Argument Reference

You can configure options for a blue/green deployment. blue_green_deployment_config supports the following:

_Only one blue_green_deployment_config is allowed_.

You can configure how traffic is rerouted to instances in a replacement environment in a blue/green deployment. deployment_ready_option supports the following:

You can configure how instances will be added to the replacement environment in a blue/green deployment. green_fleet_provisioning_option supports the following:

You can configure how instances in the original environment are terminated when a blue/green deployment is successful. terminate_blue_instances_on_deployment_success supports the following:

deployment_style Argument Reference

You can configure the type of deployment, either in-place or blue/green, you want to run and whether to route deployment traffic behind a load balancer. deployment_style supports the following:

_Only one deployment_style is allowed_.

ec2_tag_filter Argument Reference

The ec2_tag_filter configuration block supports the following:

Multiple occurrences of ec2_tag_filter are allowed, where any instance that matches to at least one of the tag filters is selected.

ec2_tag_set Argument Reference

You can form a tag group by putting a set of tag filters into ec2_tag_set. If multiple tag groups are specified, any instance that matches to at least one tag filter of every tag group is selected.

ecs_service Argument Reference

Each ecs_service configuration block supports the following:

load_balancer_info Argument Reference

You can configure the Load Balancer to use in a deployment. load_balancer_info supports the following:

load_balancer_info elb_info Argument Reference

The elb_info configuration block supports the following:

load_balancer_info target_group_info Argument Reference

The target_group_info configuration block supports the following:

load_balancer_info target_group_pair_info Argument Reference

The target_group_pair_info configuration block supports the following:

load_balancer_info target_group_pair_info prod_traffic_route Argument Reference

The prod_traffic_route configuration block supports the following:

load_balancer_info target_group_pair_info target_group Argument Reference

The target_group configuration block supports the following:

load_balancer_info target_group_pair_info test_traffic_route Argument Reference

The test_traffic_route configuration block supports the following:

on_premises_instance_tag_filter Argument Reference

The on_premises_instance_tag_filter configuration block supports the following:

trigger_configuration Argument Reference

Add triggers to a Deployment Group to receive notifications about events related to deployments or instances in the group. Notifications are sent to subscribers of the SNS topic associated with the trigger. _CodeDeploy must have permission to publish to the topic from this deployment group_. trigger_configuration supports the following:

Attribute Reference

This resource exports the following attributes in addition to the arguments above:

Import

In Terraform v1.5.0 and later, use an import block to import CodeDeploy Deployment Groups using app_name, a colon, and deployment_group_name. For example:

import {
  to = aws_codedeploy_deployment_group.example
  id = "my-application:my-deployment-group"
}

Using terraform import, import CodeDeploy Deployment Groups using app_name, a colon, and deployment_group_name. For example:

% terraform import aws_codedeploy_deployment_group.example my-application:my-deployment-group