Skip to content
v2.277.0

HttpCodeTarget Enum ​

Count of HTTP status originating from the targets.

Example ​

ts
import * as cloudwatch from 'aws-cdk-lib/aws-cloudwatch';

declare const service: ecs.FargateService;
declare const blueTargetGroup: elbv2.ApplicationTargetGroup;
declare const greenTargetGroup: elbv2.ApplicationTargetGroup;
declare const listener: elbv2.IApplicationListener;

// Alarm on the number of unhealthy ECS tasks in each target group
const blueUnhealthyHosts = new cloudwatch.Alarm(this, 'BlueUnhealthyHosts', {
  alarmName: Stack.of(this).stackName + '-Unhealthy-Hosts-Blue',
  metric: blueTargetGroup.metricUnhealthyHostCount(),
  threshold: 1,
  evaluationPeriods: 2,
});

const greenUnhealthyHosts = new cloudwatch.Alarm(this, 'GreenUnhealthyHosts', {
  alarmName: Stack.of(this).stackName + '-Unhealthy-Hosts-Green',
  metric: greenTargetGroup.metricUnhealthyHostCount(),
  threshold: 1,
  evaluationPeriods: 2,
});

// Alarm on the number of HTTP 5xx responses returned by each target group
const blueApiFailure = new cloudwatch.Alarm(this, 'Blue5xx', {
  alarmName: Stack.of(this).stackName + '-Http-5xx-Blue',
  metric: blueTargetGroup.metricHttpCodeTarget(
    elbv2.HttpCodeTarget.TARGET_5XX_COUNT,
    { period: Duration.minutes(1) },
  ),
  threshold: 1,
  evaluationPeriods: 1,
});

const greenApiFailure = new cloudwatch.Alarm(this, 'Green5xx', {
  alarmName: Stack.of(this).stackName + '-Http-5xx-Green',
  metric: greenTargetGroup.metricHttpCodeTarget(
    elbv2.HttpCodeTarget.TARGET_5XX_COUNT,
    { period: Duration.minutes(1) },
  ),
  threshold: 1,
  evaluationPeriods: 1,
});

new codedeploy.EcsDeploymentGroup(this, 'BlueGreenDG', {
  // CodeDeploy will monitor these alarms during a deployment and automatically roll back
  alarms: [blueUnhealthyHosts, greenUnhealthyHosts, blueApiFailure, greenApiFailure],
  autoRollback: {
    // CodeDeploy will automatically roll back if a deployment is stopped
    stoppedDeployment: true,
  },
  service,
  blueGreenDeploymentConfig: {
    blueTargetGroup,
    greenTargetGroup,
    listener,
  },
  deploymentConfig: codedeploy.EcsDeploymentConfig.CANARY_10PERCENT_5MINUTES,
});
python
import aws_cdk.aws_cloudwatch as cloudwatch

# service: ecs.FargateService
# blue_target_group: elbv2.ApplicationTargetGroup
# green_target_group: elbv2.ApplicationTargetGroup
# listener: elbv2.IApplicationListener


# Alarm on the number of unhealthy ECS tasks in each target group
blue_unhealthy_hosts = cloudwatch.Alarm(self, "BlueUnhealthyHosts",
    alarm_name=Stack.of(self).stack_name + "-Unhealthy-Hosts-Blue",
    metric=blue_target_group.metric_unhealthy_host_count(),
    threshold=1,
    evaluation_periods=2
)

green_unhealthy_hosts = cloudwatch.Alarm(self, "GreenUnhealthyHosts",
    alarm_name=Stack.of(self).stack_name + "-Unhealthy-Hosts-Green",
    metric=green_target_group.metric_unhealthy_host_count(),
    threshold=1,
    evaluation_periods=2
)

# Alarm on the number of HTTP 5xx responses returned by each target group
blue_api_failure = cloudwatch.Alarm(self, "Blue5xx",
    alarm_name=Stack.of(self).stack_name + "-Http-5xx-Blue",
    metric=blue_target_group.metric_http_code_target(elbv2.HttpCodeTarget.TARGET_5XX_COUNT, period=Duration.minutes(1)),
    threshold=1,
    evaluation_periods=1
)

green_api_failure = cloudwatch.Alarm(self, "Green5xx",
    alarm_name=Stack.of(self).stack_name + "-Http-5xx-Green",
    metric=green_target_group.metric_http_code_target(elbv2.HttpCodeTarget.TARGET_5XX_COUNT, period=Duration.minutes(1)),
    threshold=1,
    evaluation_periods=1
)

codedeploy.EcsDeploymentGroup(self, "BlueGreenDG",
    # CodeDeploy will monitor these alarms during a deployment and automatically roll back
    alarms=[blue_unhealthy_hosts, green_unhealthy_hosts, blue_api_failure, green_api_failure],
    auto_rollback=codedeploy.AutoRollbackConfig(
        # CodeDeploy will automatically roll back if a deployment is stopped
        stopped_deployment=True
    ),
    service=service,
    blue_green_deployment_config=codedeploy.EcsBlueGreenDeploymentConfig(
        blue_target_group=blue_target_group,
        green_target_group=green_target_group,
        listener=listener
    ),
    deployment_config=codedeploy.EcsDeploymentConfig.CANARY_10PERCENT_5MINUTES
)
java
import software.amazon.awscdk.services.cloudwatch.*;

FargateService service;
ApplicationTargetGroup blueTargetGroup;
ApplicationTargetGroup greenTargetGroup;
IApplicationListener listener;


// Alarm on the number of unhealthy ECS tasks in each target group
Alarm blueUnhealthyHosts = Alarm.Builder.create(this, "BlueUnhealthyHosts")
        .alarmName(Stack.of(this).getStackName() + "-Unhealthy-Hosts-Blue")
        .metric(blueTargetGroup.metricUnhealthyHostCount())
        .threshold(1)
        .evaluationPeriods(2)
        .build();

Alarm greenUnhealthyHosts = Alarm.Builder.create(this, "GreenUnhealthyHosts")
        .alarmName(Stack.of(this).getStackName() + "-Unhealthy-Hosts-Green")
        .metric(greenTargetGroup.metricUnhealthyHostCount())
        .threshold(1)
        .evaluationPeriods(2)
        .build();

// Alarm on the number of HTTP 5xx responses returned by each target group
Alarm blueApiFailure = Alarm.Builder.create(this, "Blue5xx")
        .alarmName(Stack.of(this).getStackName() + "-Http-5xx-Blue")
        .metric(blueTargetGroup.metricHttpCodeTarget(HttpCodeTarget.TARGET_5XX_COUNT, MetricOptions.builder().period(Duration.minutes(1)).build()))
        .threshold(1)
        .evaluationPeriods(1)
        .build();

Alarm greenApiFailure = Alarm.Builder.create(this, "Green5xx")
        .alarmName(Stack.of(this).getStackName() + "-Http-5xx-Green")
        .metric(greenTargetGroup.metricHttpCodeTarget(HttpCodeTarget.TARGET_5XX_COUNT, MetricOptions.builder().period(Duration.minutes(1)).build()))
        .threshold(1)
        .evaluationPeriods(1)
        .build();

EcsDeploymentGroup.Builder.create(this, "BlueGreenDG")
        // CodeDeploy will monitor these alarms during a deployment and automatically roll back
        .alarms(List.of(blueUnhealthyHosts, greenUnhealthyHosts, blueApiFailure, greenApiFailure))
        .autoRollback(AutoRollbackConfig.builder()
                // CodeDeploy will automatically roll back if a deployment is stopped
                .stoppedDeployment(true)
                .build())
        .service(service)
        .blueGreenDeploymentConfig(EcsBlueGreenDeploymentConfig.builder()
                .blueTargetGroup(blueTargetGroup)
                .greenTargetGroup(greenTargetGroup)
                .listener(listener)
                .build())
        .deploymentConfig(EcsDeploymentConfig.CANARY_10PERCENT_5MINUTES)
        .build();
csharp
// Install: dotnet add package Amazon.CDK.Lib
using Amazon.CDK.AWS.CloudWatch;

FargateService service;
ApplicationTargetGroup blueTargetGroup;
ApplicationTargetGroup greenTargetGroup;
IApplicationListener listener;


// Alarm on the number of unhealthy ECS tasks in each target group
var blueUnhealthyHosts = new Alarm(this, "BlueUnhealthyHosts", new AlarmProps {
    AlarmName = Stack.Of(this).StackName + "-Unhealthy-Hosts-Blue",
    Metric = blueTargetGroup.MetricUnhealthyHostCount(),
    Threshold = 1,
    EvaluationPeriods = 2
});

var greenUnhealthyHosts = new Alarm(this, "GreenUnhealthyHosts", new AlarmProps {
    AlarmName = Stack.Of(this).StackName + "-Unhealthy-Hosts-Green",
    Metric = greenTargetGroup.MetricUnhealthyHostCount(),
    Threshold = 1,
    EvaluationPeriods = 2
});

// Alarm on the number of HTTP 5xx responses returned by each target group
var blueApiFailure = new Alarm(this, "Blue5xx", new AlarmProps {
    AlarmName = Stack.Of(this).StackName + "-Http-5xx-Blue",
    Metric = blueTargetGroup.MetricHttpCodeTarget(HttpCodeTarget.TARGET_5XX_COUNT, new MetricOptions { Period = Duration.Minutes(1) }),
    Threshold = 1,
    EvaluationPeriods = 1
});

var greenApiFailure = new Alarm(this, "Green5xx", new AlarmProps {
    AlarmName = Stack.Of(this).StackName + "-Http-5xx-Green",
    Metric = greenTargetGroup.MetricHttpCodeTarget(HttpCodeTarget.TARGET_5XX_COUNT, new MetricOptions { Period = Duration.Minutes(1) }),
    Threshold = 1,
    EvaluationPeriods = 1
});

new EcsDeploymentGroup(this, "BlueGreenDG", new EcsDeploymentGroupProps {
    // CodeDeploy will monitor these alarms during a deployment and automatically roll back
    Alarms = new [] { blueUnhealthyHosts, greenUnhealthyHosts, blueApiFailure, greenApiFailure },
    AutoRollback = new AutoRollbackConfig {
        // CodeDeploy will automatically roll back if a deployment is stopped
        StoppedDeployment = true
    },
    Service = service,
    BlueGreenDeploymentConfig = new EcsBlueGreenDeploymentConfig {
        BlueTargetGroup = blueTargetGroup,
        GreenTargetGroup = greenTargetGroup,
        Listener = listener
    },
    DeploymentConfig = EcsDeploymentConfig.CANARY_10PERCENT_5MINUTES
});
go
// Install: go get github.com/aws/aws-cdk-go/awscdk/v2
import "github.com/aws/aws-cdk-go/awscdk"

var service FargateService
var blueTargetGroup ApplicationTargetGroup
var greenTargetGroup ApplicationTargetGroup
var listener IApplicationListener


// Alarm on the number of unhealthy ECS tasks in each target group
blueUnhealthyHosts := cloudwatch.NewAlarm(this, jsii.String("BlueUnhealthyHosts"), &AlarmProps{
	AlarmName: jsii.String(awscdk.stack_Of(this).stackName + "-Unhealthy-Hosts-Blue"),
	Metric: blueTargetGroup.MetricUnhealthyHostCount(),
	Threshold: jsii.Number(1),
	EvaluationPeriods: jsii.Number(2),
})

greenUnhealthyHosts := cloudwatch.NewAlarm(this, jsii.String("GreenUnhealthyHosts"), &AlarmProps{
	AlarmName: jsii.String(awscdk.stack_Of(this).stackName + "-Unhealthy-Hosts-Green"),
	Metric: greenTargetGroup.*MetricUnhealthyHostCount(),
	Threshold: jsii.Number(1),
	EvaluationPeriods: jsii.Number(2),
})

// Alarm on the number of HTTP 5xx responses returned by each target group
blueApiFailure := cloudwatch.NewAlarm(this, jsii.String("Blue5xx"), &AlarmProps{
	AlarmName: jsii.String(awscdk.stack_Of(this).stackName + "-Http-5xx-Blue"),
	Metric: blueTargetGroup.MetricHttpCodeTarget(elbv2.HttpCodeTarget_TARGET_5XX_COUNT, &MetricOptions{
		Period: awscdk.Duration_Minutes(jsii.Number(1)),
	}),
	Threshold: jsii.Number(1),
	EvaluationPeriods: jsii.Number(1),
})

greenApiFailure := cloudwatch.NewAlarm(this, jsii.String("Green5xx"), &AlarmProps{
	AlarmName: jsii.String(awscdk.stack_Of(this).stackName + "-Http-5xx-Green"),
	Metric: greenTargetGroup.*MetricHttpCodeTarget(elbv2.HttpCodeTarget_TARGET_5XX_COUNT, &MetricOptions{
		Period: awscdk.Duration_*Minutes(jsii.Number(1)),
	}),
	Threshold: jsii.Number(1),
	EvaluationPeriods: jsii.Number(1),
})

codedeploy.NewEcsDeploymentGroup(this, jsii.String("BlueGreenDG"), &EcsDeploymentGroupProps{
	// CodeDeploy will monitor these alarms during a deployment and automatically roll back
	Alarms: []IAlarmRef{
		blueUnhealthyHosts,
		greenUnhealthyHosts,
		blueApiFailure,
		greenApiFailure,
	},
	AutoRollback: &AutoRollbackConfig{
		// CodeDeploy will automatically roll back if a deployment is stopped
		StoppedDeployment: jsii.Boolean(true),
	},
	Service: Service,
	BlueGreenDeploymentConfig: &EcsBlueGreenDeploymentConfig{
		BlueTargetGroup: *BlueTargetGroup,
		GreenTargetGroup: *GreenTargetGroup,
		Listener: *Listener,
	},
	DeploymentConfig: codedeploy.EcsDeploymentConfig_CANARY_10PERCENT_5MINUTES(),
})

Members ​

MemberDescription
TARGET_2XX_COUNTThe number of 2xx response codes from targets.
TARGET_3XX_COUNTThe number of 3xx response codes from targets.
TARGET_4XX_COUNTThe number of 4xx response codes from targets.
TARGET_5XX_COUNTThe number of 5xx response codes from targets.