-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdocdb-example.tf
273 lines (261 loc) · 9.66 KB
/
docdb-example.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
locals {
docdb_example_fqdn = "docdb-example.${var.ingress_domain}"
# see Connecting Programmatically to Amazon DocumentDB at https://docs.aws.amazon.com/documentdb/latest/developerguide/
docdb_example_master_connection_string = format(
"mongodb://%s:%s@%s:%d/?tls=true&tlsCAFile=/etc/ssl/certs/ca-certificates.crt&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false",
urlencode("master"),
urlencode("Ex0mple!"),
data.external.docdb_example.result.endpoint,
27017
)
}
# see https://registry.terraform.io/providers/hashicorp/external/latest/docs/data-sources/external
data "external" "docdb_example" {
program = ["bash", "${path.module}/docdb-data.sh"]
query = {
name = var.cluster_name
}
}
# TODO re-evaluate replacing aws_acm_certificate/aws_acm_certificate_validation/aws_route53_record
# with acm-controller et al to be alike the cert-manager/external-dns CRDs
# when the following issues are addressed.
# see https://github.com/aws-controllers-k8s/community/issues/1904
# see https://github.com/aws-controllers-k8s/community/issues/482#issuecomment-755922462
# see https://github.com/kubernetes-sigs/aws-load-balancer-controller/issues/2509
# see https://github.com/aws-controllers-k8s/acm-controller/blob/v0.0.14/apis/v1alpha1/certificate.go#L23-L24
# see https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/acm_certificate
resource "aws_acm_certificate" "docdb_example" {
domain_name = local.docdb_example_fqdn
validation_method = "DNS"
key_algorithm = "EC_prime256v1"
lifecycle {
create_before_destroy = true
}
}
# see https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/acm_certificate_validation
resource "aws_acm_certificate_validation" "docdb_example" {
certificate_arn = aws_acm_certificate.docdb_example.arn
validation_record_fqdns = [for record in aws_route53_record.docdb_example_certificate_validation : record.fqdn]
}
# see https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route53_record
resource "aws_route53_record" "docdb_example_certificate_validation" {
for_each = {
for dvo in aws_acm_certificate.docdb_example.domain_validation_options : dvo.domain_name => {
name = dvo.resource_record_name
record = dvo.resource_record_value
type = dvo.resource_record_type
}
}
allow_overwrite = true
type = each.value.type
name = each.value.name
records = [each.value.record]
ttl = 60
zone_id = data.aws_route53_zone.ingress.zone_id
}
# see https://kubernetes.io/docs/concepts/configuration/
# see https://kubernetes.io/docs/concepts/configuration/secret/
# see https://kubernetes.io/docs/concepts/security/secrets-good-practices/
# see https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs/resources/secret_v1
resource "kubernetes_secret_v1" "docdb_example_master" {
metadata {
name = "docdb-example-master"
}
data = {
connection_string = local.docdb_example_master_connection_string
}
}
# see https://kubernetes.io/docs/concepts/services-networking/ingress/
# see https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.29/#ingress-v1-networking-k8s-io
# see https://kubernetes-sigs.github.io/aws-load-balancer-controller/v2.7/guide/ingress/annotations/
# see https://docs.aws.amazon.com/elasticloadbalancing/latest/application/create-https-listener.html#describe-ssl-policies
# see https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs/resources/ingress_v1
resource "kubernetes_ingress_v1" "docdb_example" {
metadata {
name = "docdb-example"
annotations = {
"alb.ingress.kubernetes.io/scheme" = "internet-facing"
"alb.ingress.kubernetes.io/target-type" = "ip"
"alb.ingress.kubernetes.io/group.name" = var.ingress_domain
"alb.ingress.kubernetes.io/listen-ports" = "[{\"HTTP\":80},{\"HTTPS\":443}]"
"alb.ingress.kubernetes.io/ssl-redirect" = "443"
"alb.ingress.kubernetes.io/ssl-policy" = "ELBSecurityPolicy-TLS13-1-2-2021-06"
"alb.ingress.kubernetes.io/healthcheck-path" = "/health/ready"
}
}
spec {
rule {
host = local.docdb_example_fqdn
http {
path {
path = "/"
path_type = "Prefix"
backend {
service {
name = "docdb-example"
port {
name = "web"
}
}
}
}
}
}
}
}
# see https://kubernetes.io/docs/concepts/services-networking/service/
# see https://kubernetes.io/docs/concepts/services-networking/service/#clusterip
# see https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.29/#service-v1-core
# see https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.29/#serviceport-v1-core
# see https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs/resources/service_v1
resource "kubernetes_service_v1" "docdb_example" {
metadata {
name = "docdb-example"
}
spec {
type = "ClusterIP"
selector = {
app = "docdb-example"
}
port {
name = "web"
port = 80
protocol = "TCP"
target_port = "web"
}
}
}
# see https://kubernetes.io/docs/concepts/workloads/controllers/deployment/
# see https://kubernetes.io/docs/concepts/storage/projected-volumes/#serviceaccounttoken
# see https://kubernetes.io/docs/tasks/configure-pod-container/configure-service-account/
# see https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.29/#deployment-v1-apps
# see https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.29/#podtemplatespec-v1-core
# see https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.29/#container-v1-core
# see https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.29/#serviceaccounttokenprojection-v1-core
# see https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs/resources/deployment_v1
resource "kubernetes_deployment_v1" "docdb_example" {
metadata {
name = "docdb-example"
labels = {
app = "docdb-example"
}
}
spec {
replicas = 1
selector {
match_labels = {
app = "docdb-example"
}
}
template {
metadata {
labels = {
app = "docdb-example"
}
}
spec {
enable_service_links = false
container {
name = "docdb-example"
image = local.docdb_example_image
env {
name = "DOCDB_EXAMPLE_CONNECTION_STRING"
value_from {
secret_key_ref {
name = kubernetes_secret_v1.docdb_example_master.metadata[0].name
key = "connection_string"
}
}
}
# see https://github.com/golang/go/blob/go1.22.3/src/crypto/x509/root_linux.go
volume_mount {
name = "ca-certificates"
mount_path = "/etc/ssl/certs"
read_only = true
}
port {
name = "web"
container_port = 8000
}
readiness_probe {
http_get {
path = "/health/ready"
port = "web"
}
}
resources {
requests = {
cpu = "0.1"
memory = "24Mi"
}
limits = {
cpu = "0.1"
memory = "24Mi"
}
}
}
volume {
name = "ca-certificates"
config_map {
name = kubernetes_manifest.docdb_example_ca_certificates.manifest.metadata.name
default_mode = "0444"
}
}
}
}
}
}
# see https://docs.aws.amazon.com/documentdb/latest/developerguide/connect_programmatically.html#connect_programmatically-tls_enabled
# see https://registry.terraform.io/providers/hashicorp/http/latest/docs/data-sources/http
data "http" "aws_rds_ca_certificates" {
url = "https://truststore.pki.rds.amazonaws.com/global/global-bundle.pem"
}
# see https://docs.aws.amazon.com/documentdb/latest/developerguide/connect_programmatically.html#connect_programmatically-tls_enabled
# see https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs/resources/config_map_v1
resource "kubernetes_config_map_v1" "aws_rds_ca_certificates" {
metadata {
namespace = "cert-manager"
name = "aws-rds-ca-certificates"
}
data = {
"ca-certificates.crt" = data.http.aws_rds_ca_certificates.response_body
}
}
# NB the bundle object will create the docdb-example-ca-certificates configmap.
# NB this is a kubernetes cluster level object.
# see https://cert-manager.io/docs/trust/trust-manager/api-reference/
# see https://cert-manager.io/docs/tutorials/getting-started-with-trust-manager/
# see https://github.com/golang/go/blob/go1.22.3/src/crypto/x509/root_linux.go
# see https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs/resources/manifest
resource "kubernetes_manifest" "docdb_example_ca_certificates" {
manifest = {
apiVersion = "trust.cert-manager.io/v1alpha1"
kind = "Bundle"
metadata = {
name = "docdb-example-ca-certificates"
}
spec = {
sources = [
{
useDefaultCAs = true
},
{
configMap = {
name = kubernetes_config_map_v1.aws_rds_ca_certificates.metadata[0].name
key = "ca-certificates.crt"
}
},
]
target = {
namespaceSelector = {
matchLabels = {
"kubernetes.io/metadata.name" = "default"
}
}
configMap = {
key = "ca-certificates.crt"
}
}
}
}
}