-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.tf
349 lines (294 loc) · 9.13 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
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
terraform {
required_providers {
kubernetes = "~>2.20.0"
aws = "~>4.47.0"
}
}
// locals provides some aliases for commonly-used names
locals {
hostname = "${var.subdomain}.${var.domain}"
}
resource "kubernetes_deployment" "deployment" {
metadata {
name = var.app_name
labels = {
appName = var.app_name
}
}
spec {
replicas = 1
strategy {
type = "RollingUpdate"
rolling_update {
max_surge = 1
max_unavailable = 1
}
}
selector {
match_labels = {
appName = var.app_name
}
}
template {
metadata {
labels = {
appName = var.app_name
}
}
spec {
container {
name = var.app_name
image = var.container_image
dynamic "env" {
for_each = var.env_vars
content {
name = env.value["name"]
value = env.value["value"]
}
}
resources {
limits = {
cpu = var.resource_limits.cpu
memory = var.resource_limits.memory
}
requests = {
cpu = var.resource_requests.cpu
memory = var.resource_requests.memory
}
}
liveness_probe {
http_get {
path = var.healthcheck_path
port = var.internal_port
dynamic "http_header" {
for_each = var.healthcheck_headers
iterator = header
content {
name = header.value["name"]
value = header.value["value"]
}
}
}
period_seconds = 10
initial_delay_seconds = 15
timeout_seconds = 3
failure_threshold = 3
}
}
// We might need at least one volume to make automounting the service
// token work
volume {
name = "${var.app_name}-data"
empty_dir {}
}
service_account_name = var.app_name
}
}
}
}
resource "aws_acm_certificate" "cert" {
domain_name = local.hostname
validation_method = "DNS"
subject_alternative_names = keys(var.cert_alternative_names)
tags = merge(var.standard_tags, {
Name = "Certificate for ${var.app_name}"
})
lifecycle {
create_before_destroy = true
}
}
resource "aws_route53_record" "cert_validation" {
for_each = {
for dv in aws_acm_certificate.cert.domain_validation_options : dv.domain_name => {
name = dv.resource_record_name
record = dv.resource_record_value
type = dv.resource_record_type
// For each name, we look up the corresponding zone in cert_alternative_names,
// or use the overall zone ID as the default if we do not find an entry.
// Some string operations are needed to turn the validation record name back into
// the original SAN.
zone = lookup(var.cert_alternative_names,
trimsuffix(replace(dv.resource_record_name, "/^[^.]*[.]/", ""),"."),
var.route53_zone_id)
}
}
allow_overwrite = true
name = each.value.name
records = [each.value.record]
ttl = 60
type = each.value.type
zone_id = each.value.zone
}
resource "aws_acm_certificate_validation" "validation" {
certificate_arn = aws_acm_certificate.cert.arn
validation_record_fqdns = [for record in aws_route53_record.cert_validation : record.fqdn]
}
// Load balancer which can use ACM certificates
resource "kubernetes_service" "load_balancer" {
metadata {
name = var.app_name
annotations = {
"service.beta.kubernetes.io/aws-load-balancer-backend-protocol" = "http",
"service.beta.kubernetes.io/aws-load-balancer-ssl-cert" = aws_acm_certificate.cert.arn
"service.beta.kubernetes.io/aws-load-balancer-ssl-ports" = "https"
}
}
spec {
type = "LoadBalancer"
selector = {
appName = var.app_name
}
session_affinity = "None"
port {
name = "http"
port = 80
target_port = var.internal_port
}
port {
name = "https"
port = 443
target_port = var.internal_port
}
}
}
resource "kubernetes_ingress_v1" "ingress" {
metadata {
name = "${var.app_name}-ingress"
annotations = {
"ingress.kubernetes.io/rewrite-target" = "/",
"kubernetes.io/ingress.class" = "alb"
"alb.ingress.kubernetes.io/scheme" = "internet-facing"
"alb.ingress.kubernetes.io/listen-ports" = "[{\"HTTP\": 80}, {\"HTTPS\":443}]"
"alb.ingress.kubernetes.io/actions.ssl-redirect" = "{\"Type\": \"redirect\", \"RedirectConfig\": { \"Protocol\": \"HTTPS\", \"Port\": \"443\", \"StatusCode\": \"HTTP_301\"}}"
}
}
spec {
default_backend {
service {
name = var.app_name
port {
number = var.internal_port
}
}
}
tls {
hosts = [local.hostname]
}
rule {
http {
path {
path = "/*"
backend {
service {
name = "ssl-redirect"
port {
name = "use-annotation"
}
}
}
}
}
}
rule {
host = local.hostname
http {
path {
path = "/"
backend {
service {
name = var.app_name
port {
number = var.internal_port
}
}
}
}
}
}
}
}
resource "aws_route53_record" "external_dns" {
zone_id = var.route53_zone_id
name = local.hostname
type = "CNAME"
ttl = "5"
records = [kubernetes_service.load_balancer.status[0].load_balancer[0].ingress[0].hostname]
}
resource "aws_route53_record" "internal_dns" {
count = var.route53_internal_zone_id!="" ? 1 : 0
zone_id = var.route53_internal_zone_id
name = local.hostname
type = "CNAME"
ttl = "5"
records = [kubernetes_service.load_balancer.status[0].load_balancer[0].ingress[0].hostname]
}
/* IAM:
See https://docs.aws.amazon.com/eks/latest/userguide/iam-roles-for-service-accounts.html
for an explanation of what's going on here, but here's a summary:
We want the application to be able to access AWS resources like SecretsManager,
S3, and so on, which means it needs credentials. The application running on
Kubernetes has an identity on the Kubernetes cluster; our job is to grant that
Kubernetes identity the permission to assume an AWS IAM role.
This requires a few things:
0. We need to register the Kubernetes cluster as an identity provider for IAM.
This lets IAM know that our cluster exists and is reasonably trusted. Note
that this step is _not_ done in this module, for two reasons: first, it's an
admin action which the SCIMMA devops team doesn't have permission to do, and
second, it's something that should be done once for the whole cluster, not
for every application on the cluster.
1. We need to make a Kubernetes Service Account. This will be the identity that
the application uses on the cluster.
2. We need to make an AWS IAM Role which we'll use to hold the permissions that
target just the few APIs we'll permit.
3. We need to tell AWS that we give our Kubernetes cluster permission to assume
this role, under the condition that the assumer has the right Service
Account.
4. We need to attach any desired AWS IAM policies to the role we created.
*/
// Step 1: Make a service account
resource "kubernetes_service_account" "account" {
metadata {
name = var.app_name
annotations = {
"eks.amazonaws.com/role-arn" = aws_iam_role.app.arn
}
}
automount_service_account_token = true
}
// Step 2: Make a role
resource "aws_iam_role" "app" {
name = "hopDev-k8s-${var.app_name}"
assume_role_policy = data.aws_iam_policy_document.permit_kubernetes_assume_role.json
permissions_boundary = "arn:aws:iam::585193511743:policy/NoIAM"
tags = var.standard_tags
}
// Step 3: Permit the cluster (which should already exist) to assume the role
data "aws_eks_cluster" "cluster" {
name = var.eks_cluster_name
}
data "aws_caller_identity" "current" {}
locals {
# Trim the https:// prefix from the OIDC issuer value to get an issuer
# identifier. This is just the format that AWS expects.
oidc_issuer_id = replace(data.aws_eks_cluster.cluster.identity.0.oidc.0.issuer, "https://", "")
oidc_arn = "arn:aws:iam::${data.aws_caller_identity.current.account_id}:oidc-provider/${local.oidc_issuer_id}"
}
data "aws_iam_policy_document" "permit_kubernetes_assume_role" {
statement {
effect = "Allow"
actions = ["sts:AssumeRoleWithWebIdentity"]
principals {
type = "Federated"
identifiers = [local.oidc_arn]
}
}
}
// Step 4: Attach IAM policies to the role we created.
resource "aws_iam_policy" "policy" {
name = "hopDev-k8s-${var.app_name}"
policy = var.iam_policy_json
}
resource "aws_iam_role_policy_attachment" "attachment" {
policy_arn = aws_iam_policy.policy.arn
role = aws_iam_role.app.name
}