I created a repo with all the files here, here'shere are the relevant the lb and, ecs configand CodeDeploy configs:
resource "aws_lb" "lb" {
name = "${var.name}-lb"
internal = false
load_balancer_type = "application"
subnets = [aws_default_subnet.default_az1.id, aws_default_subnet.default_az2.id]
security_groups = [aws_security_group.blue_green.id]
}
resource "aws_lb_target_group" "blue" {
name = "${var.name}-blue"
port = 80
target_type = "ip"
protocol = "HTTP"
vpc_id = aws_default_vpc.default.id
depends_on = [
aws_lb.lb
]
health_check {
path = "/"
protocol = "HTTP"
matcher = "200"
interval = "10"
timeout = "5"
unhealthy_threshold = "3"
healthy_threshold = "3"
}
tags = {
Name = var.name
}
}
resource "aws_lb_target_group" "green" {
name = "${var.name}-green"
port = 80
target_type = "ip"
protocol = "HTTP"
vpc_id = aws_default_vpc.default.id
depends_on = [
aws_lb.lb
]
health_check {
path = "/"
protocol = "HTTP"
matcher = "200"
interval = "10"
timeout = "5"
unhealthy_threshold = "3"
healthy_threshold = "3"
}
tags = {
Name = var.name
}
}
resource "aws_lb_listener" "green" {
load_balancer_arn = aws_lb.lb.arn
port = "80"
protocol = "HTTP"
default_action {
type = "forward"
target_group_arn = aws_lb_target_group.green.arn
}
depends_on = [aws_lb_target_group.green]
}
resource "aws_lb_listener" "blue" {
load_balancer_arn = aws_lb.lb.arn
port = "8080"
protocol = "HTTP"
default_action {
type = "forward"
target_group_arn = aws_lb_target_group.blue.arn
}
depends_on = [aws_lb_target_group.blue]
}
codedeploy
resource "aws_codedeploy_app" "app" {
compute_platform = "ECS"
name = var.name
}
resource "aws_codedeploy_deployment_group" "group" {
app_name = var.name
deployment_config_name = "CodeDeployDefault.ECSAllAtOnce"
deployment_group_name = "${var.name}-group-1"
service_role_arn = aws_iam_role.codedeploy-service.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"
}
}
deployment_style {
deployment_option = "WITH_TRAFFIC_CONTROL"
deployment_type = "BLUE_GREEN"
}
ecs_service {
cluster_name = aws_ecs_cluster.cluster.name
service_name = aws_ecs_service.api.name
}
load_balancer_info {
target_group_pair_info {
prod_traffic_route {
listener_arns = [aws_lb_listener.green.arn]
}
test_traffic_route {
listener_arns = [aws_lb_listener.blue.arn]
}
target_group {
name = aws_lb_target_group.blue.name
}
target_group {
name = aws_lb_target_group.green.name
}
}
}
}