This repository has been archived by the owner on Jul 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 417
/
main.tf
199 lines (162 loc) · 3.94 KB
/
main.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
/**
* The service module creates an ecs service, task definition
* elb and a route53 record under the local service zone (see the dns module).
*
* 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 ELB module"
}
variable "security_groups" {
description = "Comma separated list of security group IDs that will be passed to the ELB module"
}
variable "port" {
description = "The container host port"
}
variable "cluster" {
description = "The cluster name or ARN"
}
variable "dns_name" {
description = "The DNS name to use, e.g nginx"
}
variable "log_bucket" {
description = "The S3 bucket ID to use for the ELB"
}
/**
* 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 "protocol" {
description = "The ELB protocol, HTTP or TCP"
default = "HTTP"
}
variable "iam_role" {
description = "IAM Role ARN to use"
}
variable "zone_id" {
description = "The zone ID to create the record in"
}
/**
* 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}"
load_balancer {
elb_name = "${module.elb.id}"
container_name = "${module.task.name}"
container_port = "${var.container_port}"
}
lifecycle {
create_before_destroy = true
}
}
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}"
ports = <<EOF
[
{
"containerPort": ${var.container_port},
"hostPort": ${var.port}
}
]
EOF
}
module "elb" {
source = "../elb"
name = "${module.task.name}"
port = "${var.port}"
environment = "${var.environment}"
subnet_ids = "${var.subnet_ids}"
security_groups = "${var.security_groups}"
dns_name = "${coalesce(var.dns_name, module.task.name)}"
healthcheck = "${var.healthcheck}"
protocol = "${var.protocol}"
zone_id = "${var.zone_id}"
log_bucket = "${var.log_bucket}"
}
/**
* Outputs.
*/
// The name of the ELB
output "name" {
value = "${module.elb.name}"
}
// The DNS name of the ELB
output "dns" {
value = "${module.elb.dns}"
}
// The id of the ELB
output "elb" {
value = "${module.elb.id}"
}
// The zone id of the ELB
output "zone_id" {
value = "${module.elb.zone_id}"
}
// FQDN built using the zone domain and name
output "fqdn" {
value = "${module.elb.fqdn}"
}