-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplate.tf
244 lines (227 loc) · 7.03 KB
/
template.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
locals {
ssm_path_envs = [
{
"name" : "SSM_PATH",
"value" : local.ssm_path
}
]
env_vars = local.ssm_path == null ? var.env_vars : var.env_vars == null ? local.ssm_path_envs : setunion(
local.ssm_path_envs,
var.env_vars
)
app_log_configuration = merge(
local.use_newrelic_firelens_sidecar ? {
"logDriver" : "awsfirelens",
"options" : {
"Name" : "newrelic"
},
"secretOptions" : [{
"name" : "apiKey",
"valueFrom" : local.newrelic_secret_arn
}]
} : null,
local.use_newrelic_firelens_sidecar ? null : {
"logDriver" : "awslogs",
"options" : {
"awslogs-group" : local.log_group_name,
"awslogs-region" : data.aws_region.current.name,
"awslogs-stream-prefix" : local.container_name
"mode" : var.awslogs_driver_mode
}
}
)
use_envoy_sidecar = var.mesh_name != null && var.virtual_node != null
specify_command = var.command != null
specify_entrypoint = var.entrypoint != null
app_container_definition = merge(
{
"name" : local.container_name,
"image" : "${var.image_repo}:${var.image_tag}",
"environment" : local.env_vars,
"portMappings" : [{
"containerPort" : var.container_port
}],
"essential" : true,
"logConfiguration" : local.app_log_configuration,
},
length(var.secrets) == 0 ? {} :
{
"secrets" : [
for secret in var.secrets : {
name = secret.name
valueFrom = secret.valueFrom
}
]
},
length(var.efs_mounts) == 0 ? {} : {
"mountPoints" : [
for mount in var.efs_mounts : {
sourceVolume = mount.file_system_id
containerPath = mount.container_path
}
]
},
!local.use_envoy_sidecar ? {} : {
"dependsOn" : [
{
"containerName" : "envoy",
"condition" : "HEALTHY"
}
]
},
!local.specify_command ? {} : {
"command" : var.command,
},
!local.specify_entrypoint ? {} : {
"entrypoint" : var.entrypoint,
}
)
xray_container_definition = {
"name" : "xray-daemon",
"image" : "amazon/aws-xray-daemon",
"user" : "1337",
"essential" : true,
"cpu" : 32,
"memoryReservation" : 256,
"portMappings" : [
{
"containerPort" : 2000,
"protocol" : "udp"
}
],
"logConfiguration" : {
"logDriver" : "awslogs",
"options" : {
"awslogs-group" : local.log_group_name,
"awslogs-region" : data.aws_region.current.name,
"awslogs-stream-prefix" : "xray"
}
}
}
newrelic_firelens_container_definition = {
"name" : "firelens",
"essential" : true,
"image" : var.newrelic_firelens_image,
"firelensConfiguration" : {
"type" : "fluentbit",
"options" : {
"enable-ecs-log-metadata" : "true"
}
}
"logConfiguration" : {
"logDriver" : "awslogs",
"options" : {
"awslogs-group" : local.log_group_name,
"awslogs-region" : data.aws_region.current.name,
"awslogs-stream-prefix" : "firelens"
}
}
}
null_safe_mesh_name = var.mesh_name != null ? var.mesh_name : ""
null_safe_virtual_gateway = var.virtual_gateway != null ? var.virtual_gateway : ""
null_safe_virtual_node = var.virtual_node != null ? var.virtual_node : ""
envoy_standalone_container_definition = {
"name" : local.container_name,
"image" : "public.ecr.aws/appmesh/aws-appmesh-envoy:${var.envoy_tag}",
"essential" : true,
"environment" : [
{
"name" : "APPMESH_RESOURCE_ARN",
"value" : "arn:aws:appmesh:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:mesh/${local.null_safe_mesh_name}/virtualGateway/${local.null_safe_virtual_gateway}"
},
{
"name" : "ENABLE_ENVOY_XRAY_TRACING",
"value" : "1"
}
],
"healthCheck" : {
"command" : [
"CMD-SHELL",
"curl -s http://localhost:9901/server_info | grep state | grep -q LIVE"
],
"interval" : 5,
"retries" : 3,
"startPeriod" : 10,
"timeout" : 2
},
"user" : "1337",
"portMappings" : [
{
"containerPort" : var.container_port,
"protocol" : "tcp"
}
],
"logConfiguration" : local.app_log_configuration
}
envoy_proxy_container_definition = {
"name" : "envoy",
"image" : "public.ecr.aws/appmesh/aws-appmesh-envoy:${var.envoy_tag}",
"essential" : true,
"environment" : [
{
"name" : "APPMESH_RESOURCE_ARN",
"value" : "arn:aws:appmesh:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:mesh/${local.null_safe_mesh_name}/virtualNode/${local.null_safe_virtual_node}"
},
{
"name" : "ENABLE_ENVOY_XRAY_TRACING",
"value" : "1"
}
],
"healthCheck" : {
"command" : [
"CMD-SHELL",
"curl -s http://localhost:9901/server_info | grep state | grep -q LIVE"
],
"interval" : 5,
"retries" : 3,
"startPeriod" : 10,
"timeout" : 2
},
"memory" : 500,
"user" : "1337",
"logConfiguration" : {
"logDriver" : "awslogs",
"options" : {
"awslogs-group" : local.log_group_name,
"awslogs-region" : data.aws_region.current.name,
"awslogs-stream-prefix" : "proxy"
}
}
}
cwagent_container_definition = {
"name" : "ecs-cwagent",
"image" : "public.ecr.aws/cloudwatch-agent/cloudwatch-agent:latest",
"essential" : true,
"secrets" : [
{
"name" : "CW_CONFIG_CONTENT",
"valueFrom" : "ecs-cwagent"
}
],
"logConfiguration" : {
"logDriver" : "awslogs",
"options" : {
"awslogs-create-group" : "true",
"awslogs-group" : "/ecs/ecs-cwagent",
"awslogs-region" : data.aws_region.current.name,
"awslogs-stream-prefix" : "ecs"
}
}
}
use_virtual_gateway_def = var.mesh_name != null && var.virtual_gateway != null
use_virtual_node_def = var.mesh_name != null && var.virtual_node != null
use_app_container = !local.use_virtual_gateway_def
use_envoy_standalone_container = local.use_virtual_gateway_def
use_xray_sidecar = var.use_xray_sidecar != null ? var.use_xray_sidecar : local.use_virtual_node_def || local.use_virtual_gateway_def
use_newrelic_firelens_sidecar = var.newrelic_secret_arn != null || var.newrelic_secret_name != null
container_definitions = var.container_definitions != null ? var.container_definitions : jsonencode(
concat(
local.use_envoy_standalone_container ? [local.envoy_standalone_container_definition] : [],
local.use_app_container ? [local.app_container_definition] : [],
local.use_newrelic_firelens_sidecar ? [local.newrelic_firelens_container_definition] : [],
local.use_envoy_sidecar ? [local.envoy_proxy_container_definition] : [],
local.use_xray_sidecar ? [local.xray_container_definition] : [],
var.use_cwagent_sidecar ? [local.cwagent_container_definition] : [],
)
)
}