forked from segmentio/stack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.tf
More file actions
251 lines (204 loc) · 5.8 KB
/
main.tf
File metadata and controls
251 lines (204 loc) · 5.8 KB
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
245
246
247
248
249
250
251
/**
* The web-service is similar to the `service` module, but the
* it provides a __public__ ALB instead.
*
* Usage:
*
* module "auth_service" {
* source = "github.com/segmentio/stack/service"
* name = "auth-service"
* image = "auth-service"
* cluster = "default"
* }
*
*/
/**
* Required Variables.
*/
variable "environment" {
description = "Environment tag, e.g prod"
}
variable "image" {
description = "The docker image name, e.g nginx"
}
variable "name" {
description = "The service name, if empty the service name is defaulted to the image name"
default = ""
}
variable "version" {
description = "The docker image version"
default = "latest"
}
variable "subnet_ids" {
description = "Comma separated list of subnet IDs that will be passed to the ALB module"
}
variable "security_groups" {
description = "Comma separated list of security group IDs that will be passed to the ALB module"
}
variable "port" {
description = "The container host port"
}
variable "cluster" {
description = "The cluster name or ARN"
}
variable "log_bucket" {
description = "The S3 bucket ID to use for the ALB"
}
variable "ssl_certificate_id" {
description = "SSL Certificate ID to use"
}
variable "iam_role" {
description = "IAM Role ARN to use"
}
variable "external_dns_name" {
description = "The subdomain under which the ALB is exposed externally, defaults to the task name"
default = ""
}
variable "internal_dns_name" {
description = "The subdomain under which the ALB is exposed internally, defaults to the task name"
default = ""
}
variable "external_zone_id" {
description = "The zone ID to create the record in"
}
variable "internal_zone_id" {
description = "The zone ID to create the record in"
}
/**
* Options.
*/
variable "healthcheck" {
description = "Path to a healthcheck endpoint"
default = "/"
}
variable "container_port" {
description = "The container port"
default = 3000
}
variable "command" {
description = "The raw json of the task command"
default = "[]"
}
variable "env_vars" {
description = "The raw json of the task env vars"
default = "[]"
}
variable "desired_count" {
description = "The desired count"
default = 2
}
variable "memory" {
description = "The number of MiB of memory to reserve for the container"
default = 512
}
variable "cpu" {
description = "The number of cpu units to reserve for the container"
default = 512
}
variable "working_directory" {
description = "The working directory of the container process."
default = "/"
}
variable "deployment_minimum_healthy_percent" {
description = "lower limit (% of desired_count) of # of running tasks during a deployment"
default = 100
}
variable "deployment_maximum_percent" {
description = "upper limit (% of desired_count) of # of running tasks during a deployment"
default = 200
}
variable vpc_id {
description = "The id of the VPC."
}
variable "log_driver" {
description = "The log driver to use use for the container"
default = "journald"
}
/**
* Resources.
*/
resource "aws_ecs_service" "main" {
name = "${module.task.name}"
cluster = "${var.cluster}"
task_definition = "${module.task.arn}"
desired_count = "${var.desired_count}"
iam_role = "${var.iam_role}"
deployment_minimum_healthy_percent = "${var.deployment_minimum_healthy_percent}"
deployment_maximum_percent = "${var.deployment_maximum_percent}"
# Track the latest ACTIVE revision
task_definition = "${module.task.name}:${max("${module.task.revision}", "${data.aws_ecs_task_definition.task.revision}")}"
load_balancer {
target_group_arn = "${module.alb.target_group}"
container_name = "${module.task.name}"
container_port = "${var.container_port}"
}
lifecycle {
create_before_destroy = true
}
}
data "aws_ecs_task_definition" "task" {
task_definition = "${module.task.name}"
}
module "task" {
source = "../task"
name = "${coalesce(var.name, replace(var.image, "/", "-"))}"
image = "${var.image}"
image_version = "${var.version}"
command = "${var.command}"
env_vars = "${var.env_vars}"
memory = "${var.memory}"
cpu = "${var.cpu}"
working_directory = "${var.working_directory}"
log_driver = "${var.log_driver}"
ports = <<EOF
[
{
"containerPort": ${var.container_port},
"hostPort": ${var.port}
}
]
EOF
}
module "alb" {
source = "./alb"
name = "${module.task.name}"
port = "${var.port}"
environment = "${var.environment}"
subnet_ids = "${var.subnet_ids}"
external_dns_name = "${coalesce(var.external_dns_name, module.task.name)}"
internal_dns_name = "${coalesce(var.internal_dns_name, module.task.name)}"
healthcheck = "${var.healthcheck}"
external_zone_id = "${var.external_zone_id}"
internal_zone_id = "${var.internal_zone_id}"
security_groups = "${var.security_groups}"
log_bucket = "${var.log_bucket}"
ssl_certificate_id = "${var.ssl_certificate_id}"
vpc_id = "${var.vpc_id}"
}
/**
* Outputs.
*/
// The name of the ALB
output "name" {
value = "${module.alb.name}"
}
// The DNS name of the ALB
output "dns" {
value = "${module.alb.dns}"
}
// The id of the ALB
output "alb" {
value = "${module.alb.id}"
}
// The zone id of the ALB
output "zone_id" {
value = "${module.alb.zone_id}"
}
// FQDN built using the zone domain and name (external)
output "external_fqdn" {
value = "${module.alb.external_fqdn}"
}
// FQDN built using the zone domain and name (internal)
output "internal_fqdn" {
value = "${module.alb.internal_fqdn}"
}