Skip to content

Commit 5849425

Browse files
authored
Merge pull request #40 from hazelops/tcp-tls-updates
TCP mode Updates
2 parents 1964f63 + df8977a commit 5849425

File tree

3 files changed

+72
-62
lines changed

3 files changed

+72
-62
lines changed

locals.tf

Lines changed: 65 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
locals {
2-
name = var.app_type == "tcp-app" ? "${var.env}-${var.name}-tcp" : "${var.env}-${var.name}"
2+
name = "${var.env}-${var.name}"
33
ecs_service_name = var.ecs_service_name != "" ? var.ecs_service_name : "${var.env}-${var.name}"
44
ecs_cluster_name = var.ecs_cluster_name != "" ? var.ecs_cluster_name : "${var.env}-${var.namespace}"
55
ecs_cluster_arn = length(var.ecs_cluster_arn) != "" ? var.ecs_cluster_arn : "arn:aws:ecs:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:cluster/${local.ecs_cluster_name}"
66
ecr_repo_name = var.ecr_repo_name != "" ? var.ecr_repo_name : "${var.namespace}-${var.name}"
77
name_prefix = "${substr(var.name, 0, 5)}-"
8-
domain_names = var.root_domain_name != "example.com" ? concat(["${var.name}.${var.env}.${var.root_domain_name}"], var.domain_names) : []
8+
domain_names = var.root_domain_name != "example.com" ? concat([
9+
"${var.name}.${var.env}.${var.root_domain_name}"
10+
], var.domain_names) : []
911

1012
# Datadog Environment Variables: https://docs.datadoghq.com/agent/guide/environment-variables/
1113
# https://docs.datadoghq.com/agent/docker/apm/?tab=linux#docker-apm-agent-environment-variables
@@ -31,12 +33,12 @@ locals {
3133

3234
fluentbit_container_definition = [
3335
{
34-
essential = true
35-
image = "public.ecr.aws/aws-observability/aws-for-fluent-bit:latest"
36-
name = "log_router"
37-
memoryReservation = 75
36+
essential = true
37+
image = "public.ecr.aws/aws-observability/aws-for-fluent-bit:latest"
38+
name = "log_router"
39+
memoryReservation = 75
3840
firelensConfiguration = {
39-
"type" = "fluentbit"
41+
"type" = "fluentbit"
4042
"options" = {
4143
"enable-ecs-log-metadata" = "true"
4244
}
@@ -46,43 +48,45 @@ locals {
4648

4749
volumes = concat(var.web_proxy_enabled ? [
4850
{
49-
name = "nginx-templates",
51+
name = "nginx-templates",
5052
mount_point = {
5153
"sourceVolume" = "nginx-templates"
5254
"containerPath" = "/etc/nginx/templates/"
5355
"readOnly" = null
5456
}
5557

56-
docker_volume_configuration = [{
57-
"scope" : "task",
58-
"driver" : "local",
59-
"labels" : {
60-
"scratch" : "space"
58+
docker_volume_configuration = [
59+
{
60+
"scope" : "task",
61+
"driver" : "local",
62+
"labels" : {
63+
"scratch" : "space"
64+
}
6165
}
62-
}]
66+
]
6367
},
6468
{
65-
name = "nginx-app",
69+
name = "nginx-app",
6670
mount_point = {
6771
"sourceVolume" = "nginx-app"
6872
"containerPath" = "/etc/nginx/app/"
6973
"readOnly" = null
7074
}
7175

72-
docker_volume_configuration = [{
73-
"scope" : "task",
74-
"driver" : "local",
75-
"labels" : {
76-
"scratch" : "space"
76+
docker_volume_configuration = [
77+
{
78+
"scope" : "task",
79+
"driver" : "local",
80+
"labels" : {
81+
"scratch" : "space"
82+
}
7783
}
78-
}]
84+
]
7985
},
80-
]
81-
: [],
82-
86+
] : [],
8387
var.efs_enabled ? [
8488
{
85-
name = "efs",
89+
name = "efs",
8690
mount_point = {
8791
"sourceVolume" = "efs"
8892
"containerPath" = var.efs_mount_point,
@@ -103,45 +107,48 @@ locals {
103107
(var.datadog_enabled && var.ecs_launch_type == "EC2") ? module.datadog.volumes : []
104108
)
105109

106-
http_tcp_listeners = var.app_type == "tcp-app" ? [
110+
alb_http_tcp_listeners = var.app_type == "tcp-app" ? [
107111
for index, port_mapping in var.port_mappings :
108-
{
109-
port = port_mapping.host_port
110-
protocol = "TCP"
111-
target_group_index = index
112-
} if port_mapping.https_listener == false
112+
{
113+
port = port_mapping["host_port"]
114+
protocol = "TCP"
115+
target_group_index = index
116+
} if ! lookup(port_mapping, "tls", false)
113117
] : [
114118
{
115119
port = var.http_port
116120
protocol = "HTTP"
117121
target_group_index = 0
118-
},]
122+
}
123+
]
119124

120-
https_tls_listeners = var.app_type == "tcp-app" ? [
125+
# In case app type is "tcp-app" and port_mapping has "tls" config and is true we use tcp over tls.
126+
alb_https_listeners = var.app_type == "tcp-app" ? [
121127
for index, port_mapping in var.port_mappings :
122-
{
123-
port = port_mapping.host_port
124-
protocol = "TLS"
125-
certificate_arn = var.tls_cert_arn
126-
target_group_index = index
127-
} if port_mapping.https_listener == true
128-
] : [
129-
{
130-
port = 443
131-
protocol = "HTTPS"
132-
certificate_arn = var.tls_cert_arn
133-
target_group_index = 0
134-
},]
128+
{
129+
port = port_mapping["host_port"]
130+
protocol = "TLS"
131+
certificate_arn = var.tls_cert_arn
132+
target_group_index = index
133+
} if lookup(port_mapping, "tls", false)
134+
] : [
135+
{
136+
port = 443
137+
protocol = "HTTPS"
138+
certificate_arn = var.tls_cert_arn
139+
target_group_index = 0
140+
}
141+
]
135142

136143
ecs_service_tcp_port_mappings = [
137144
for index, port_mapping in var.port_mappings :
138-
{
139-
container_name = var.name
140-
container_port = port_mapping.container_port
141-
host_port = port_mapping.host_port
142-
target_group_arn = length(module.alb[*].target_group_arns) >= 1 ? module.alb[0].target_group_arns[index] : ""
143-
}
144-
]
145+
{
146+
container_name = var.name
147+
container_port = port_mapping["container_port"]
148+
host_port = port_mapping["host_port"]
149+
target_group_arn = length(module.alb[*].target_group_arns) >= 1 ? module.alb[0].target_group_arns[index] : ""
150+
}
151+
]
145152

146153
target_groups_web = [
147154
{
@@ -150,6 +157,8 @@ locals {
150157
backend_port = var.web_proxy_enabled ? var.web_proxy_docker_container_port : var.docker_container_port
151158
target_type = var.ecs_launch_type == "EC2" ? "instance" : "ip"
152159
deregistration_delay = var.alb_deregistration_delay
160+
preserve_client_ip = true
161+
# This is specified for compatibility with the tcp target groups. It's not actually used in a lookup.
153162

154163
health_check = {
155164
enabled = true
@@ -170,9 +179,10 @@ locals {
170179
{
171180
name_prefix = local.name_prefix
172181
backend_protocol = "TCP"
173-
backend_port = port_mapping.container_port
182+
backend_port = port_mapping["container_port"]
174183
target_type = var.ecs_launch_type == "EC2" ? "instance" : "ip"
175184
deregistration_delay = var.alb_deregistration_delay
185+
preserve_client_ip = true
176186

177187
health_check = {
178188
enabled = true
@@ -182,11 +192,9 @@ locals {
182192
unhealthy_threshold = var.alb_health_check_unhealthy_threshold
183193
timeout = null
184194
matcher = null
185-
port = port_mapping.host_port
195+
port = port_mapping["host_port"]
186196
protocol = "TCP"
187197
}
188-
189198
}
190199
]
191-
192200
}

main.tf

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,19 @@ module "alb" {
55
version = "~> 7.0"
66

77
name = var.public ? local.name : "${local.name}-private"
8-
load_balancer_type = var.app_type == "web" ? "application" : "network"
8+
load_balancer_type = var.app_type == "web" ? "application" : "network"
99
internal = var.public ? false : true
1010
vpc_id = var.vpc_id
1111
security_groups = var.alb_security_groups
1212
subnets = var.public ? var.public_subnets : var.private_subnets
1313
idle_timeout = var.alb_idle_timeout
1414

15-
http_tcp_listeners = local.http_tcp_listeners
16-
https_listeners = var.https_enabled ? concat(local.https_tls_listeners) : []
1715

18-
target_groups = concat(var.app_type == "web" ? local.target_groups_web : local.target_groups_tcp)
16+
17+
http_tcp_listeners = local.alb_http_tcp_listeners
18+
https_listeners = var.https_enabled ? concat(local.alb_https_listeners) : []
19+
20+
target_groups = concat(var.app_type == "web" ? local.target_groups_web : local.target_groups_tcp)
1921

2022
access_logs = var.alb_access_logs_enabled && var.alb_access_logs_s3bucket_name != "" ? {
2123
bucket = var.alb_access_logs_s3bucket_name

variables.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ variable "docker_host_port" {
200200

201201
variable "port_mappings" {
202202
description = "List of ports to open from a service"
203-
type = list(any)
203+
type = any
204204
default = []
205205
}
206206

0 commit comments

Comments
 (0)