Skip to content

Commit 231f916

Browse files
authored
Merge pull request #50 from oozou/terraform-test
chore: support blue-green deployment
2 parents a4db7d2 + 40077b4 commit 231f916

File tree

5 files changed

+120
-1
lines changed

5 files changed

+120
-1
lines changed

CHANGELOG.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,21 @@
11
# Change Log
22

3+
## [v1.4.0] - 2025-08-20
4+
5+
### Added
6+
7+
- Support blue-green deployment
8+
- var:
9+
- is_enable_blue_green_deployment
10+
- green_header_value
11+
- alb_priority_green
12+
- resource:
13+
- aws_lb_target_group.green
14+
- aws_lb_listener_rule.green
15+
16+
317
## [v1.3.1] - 2025-04-09
18+
419
### Added
520

621
- lifecycle create_before_destroy for aws_lb_target_group

examples/terraform-test/main.tf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,5 +177,8 @@ module "api_service" {
177177
}
178178
}
179179

180+
# Blue-Green Deployment
181+
is_enable_blue_green_deployment = true
182+
180183
tags = var.custom_tags
181184
}

examples/terraform-test/terraform.auto.tfvars

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
prefix = "oozou"
2-
environment = "devops"
2+
environment = "dev"
33
name = "demo"
44
custom_tags = {
55
"Remark" = "terraform-aws-ecs-service"

main.tf

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,34 @@ resource "aws_lb_target_group" "this" {
149149
create_before_destroy = true
150150
}
151151

152+
}
153+
154+
resource "aws_lb_target_group" "green" {
155+
count = local.is_create_target_group && var.is_enable_blue_green_deployment ? 1 : 0
156+
157+
name = format("%s-gn-tg", substr(local.container_target_group_object.name, 0, min(32, length(local.container_target_group_object.name))))
158+
159+
port = lookup(local.container_target_group_object, "port_mappings", null)[0].container_port
160+
protocol = lookup(local.container_target_group_object, "port_mappings", null)[0].container_port == 443 ? "HTTPS" : "HTTP"
161+
vpc_id = var.vpc_id
162+
target_type = "ip"
163+
deregistration_delay = var.target_group_deregistration_delay
164+
165+
health_check {
166+
interval = lookup(var.health_check, "interval", null)
167+
path = lookup(var.health_check, "path", null)
168+
timeout = lookup(var.health_check, "timeout", null)
169+
healthy_threshold = lookup(var.health_check, "healthy_threshold", null)
170+
unhealthy_threshold = lookup(var.health_check, "unhealthy_threshold", null)
171+
matcher = lookup(var.health_check, "matcher", null)
172+
}
173+
174+
tags = merge(local.tags, { "Name" = format("%s-tg", substr(local.container_target_group_object.name, 0, min(29, length(local.container_target_group_object.name)))) })
175+
176+
lifecycle {
177+
create_before_destroy = true
178+
}
179+
152180
}
153181
/* ------------------------------ Listener Rule ----------------------------- */
154182
resource "aws_lb_listener_rule" "this" {
@@ -189,6 +217,57 @@ resource "aws_lb_listener_rule" "this" {
189217

190218
tags = local.tags
191219
}
220+
221+
/* ------------------------------ Green Listener Rule ----------------------------- */
222+
resource "aws_lb_listener_rule" "green" {
223+
count = local.is_create_target_group && var.is_enable_blue_green_deployment ? 1 : 0
224+
225+
listener_arn = var.alb_listener_arn
226+
priority = var.alb_priority_green
227+
228+
action {
229+
type = "forward"
230+
target_group_arn = aws_lb_target_group.green[0].arn
231+
}
232+
233+
condition {
234+
path_pattern {
235+
values = var.alb_paths == [] ? ["*"] : var.alb_paths
236+
}
237+
}
238+
239+
dynamic "condition" {
240+
for_each = var.alb_host_header == null ? [] : [true]
241+
content {
242+
host_header {
243+
values = [var.alb_host_header]
244+
}
245+
}
246+
}
247+
248+
dynamic "condition" {
249+
for_each = var.custom_header_token == "" ? [] : [true]
250+
content {
251+
http_header {
252+
http_header_name = "custom-header-token" # Match value within cloudfront module
253+
values = [var.custom_header_token]
254+
}
255+
}
256+
}
257+
258+
dynamic "condition" {
259+
for_each = var.green_header_value == null ? [] : [true]
260+
content {
261+
http_header {
262+
http_header_name = "green-header-name"
263+
values = [var.green_header_value]
264+
}
265+
}
266+
}
267+
268+
tags = local.tags
269+
}
270+
192271
/* -------------------------------------------------------------------------- */
193272
/* Secret */
194273
/* -------------------------------------------------------------------------- */

variables.tf

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,3 +305,25 @@ variable "container" {
305305
type = any
306306
default = {}
307307
}
308+
309+
/* -------------------------------------------------------------------------- */
310+
/* Blue-Green Deployment */
311+
/* -------------------------------------------------------------------------- */
312+
variable "is_enable_blue_green_deployment" {
313+
description = "Whether the service enable blue-green deployment"
314+
type = bool
315+
default = false
316+
}
317+
318+
variable "green_header_value" {
319+
description = "green header value in alb listener rule for blue-green deployment"
320+
type = string
321+
default = "green"
322+
}
323+
324+
variable "alb_priority_green" {
325+
description = "Priority of ALB rule https://docs.aws.amazon.com/elasticloadbalancing/latest/application/load-balancer-listeners.html#listener-rules"
326+
type = string
327+
default = "110"
328+
}
329+

0 commit comments

Comments
 (0)