Skip to content

Commit 2186393

Browse files
authored
enh: move gitlab-runner and elk-stack s3 buckets from layer1-aws into layer2-k8s (#166)
1 parent 18f106e commit 2186393

File tree

4 files changed

+92
-102
lines changed

4 files changed

+92
-102
lines changed

terraform/layer1-aws/examples/aws-s3-bucket-elastic-stack.tf

Lines changed: 0 additions & 38 deletions
This file was deleted.

terraform/layer1-aws/examples/aws-s3-bucket-gitlab-runner-cache.tf

Lines changed: 0 additions & 51 deletions
This file was deleted.

terraform/layer2-k8s/examples/eks-elk.tf

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,15 @@ locals {
44
repository = lookup(local.helm_charts[index(local.helm_charts.*.id, "elk")], "repository", null)
55
chart_version = lookup(local.helm_charts[index(local.helm_charts.*.id, "elk")], "version", null)
66
}
7-
kibana_domain_name = "kibana-${local.domain_suffix}"
8-
apm_domain_name = "apm-${local.domain_suffix}"
9-
elastic_stack_bucket_name = data.terraform_remote_state.layer1-aws.outputs.elastic_stack_bucket_name
7+
kibana_domain_name = "kibana-${local.domain_suffix}"
8+
apm_domain_name = "apm-${local.domain_suffix}"
109
}
1110

1211
data "template_file" "elk" {
1312
template = file("${path.module}/templates/elk-values.yaml")
1413

1514
vars = {
16-
bucket_name = local.elastic_stack_bucket_name
15+
bucket_name = aws_s3_bucket.elastic_stack.id
1716
storage_class_name = kubernetes_storage_class.elk.id
1817
snapshot_retention_days = var.elk_snapshot_retention_days
1918
index_retention_days = var.elk_index_retention_days
@@ -136,6 +135,36 @@ resource "random_string" "kibana_password" {
136135
upper = true
137136
}
138137

138+
resource "aws_s3_bucket" "elastic_stack" {
139+
bucket = "${local.name}-elastic-stack"
140+
acl = "private"
141+
142+
server_side_encryption_configuration {
143+
rule {
144+
apply_server_side_encryption_by_default {
145+
sse_algorithm = "aws:kms"
146+
}
147+
}
148+
}
149+
150+
tags = {
151+
Name = "${local.name}-elastic-stack"
152+
Environment = local.env
153+
}
154+
}
155+
156+
resource "aws_s3_bucket_public_access_block" "elastic_stack_public_access_block" {
157+
bucket = aws_s3_bucket.elastic_stack.id
158+
# Block new public ACLs and uploading public objects
159+
block_public_acls = true
160+
# Retroactively remove public access granted through public ACLs
161+
ignore_public_acls = true
162+
# Block new public bucket policies
163+
block_public_policy = true
164+
# Retroactivley block public and cross-account access if bucket has public policies
165+
restrict_public_buckets = true
166+
}
167+
139168
module "aws_iam_elastic_stack" {
140169
source = "../modules/aws-iam-user-with-policy"
141170

@@ -152,7 +181,7 @@ module "aws_iam_elastic_stack" {
152181
"s3:ListBucketVersions"
153182
],
154183
"Resource" : [
155-
"arn:aws:s3:::${local.elastic_stack_bucket_name}"
184+
"arn:aws:s3:::${aws_s3_bucket.elastic_stack.id}"
156185
]
157186
},
158187
{
@@ -165,7 +194,7 @@ module "aws_iam_elastic_stack" {
165194
"s3:ListMultipartUploadParts"
166195
],
167196
"Resource" : [
168-
"arn:aws:s3:::${local.elastic_stack_bucket_name}/*"
197+
"arn:aws:s3:::${aws_s3_bucket.elastic_stack.id}/*"
169198
]
170199
}
171200
]
@@ -187,3 +216,8 @@ output "elasticsearch_elastic_password" {
187216
sensitive = true
188217
description = "Password of the superuser 'elastic'"
189218
}
219+
220+
output "elastic_stack_bucket_name" {
221+
value = aws_s3_bucket.elastic_stack.id
222+
description = "Name of the bucket for ELKS snapshots"
223+
}

terraform/layer2-k8s/examples/eks-gitlab-runner.tf

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,14 @@ locals {
44
repository = lookup(local.helm_charts[index(local.helm_charts.*.id, "gitlab-runner")], "repository", null)
55
chart_version = lookup(local.helm_charts[index(local.helm_charts.*.id, "gitlab-runner")], "version", null)
66
}
7-
gitlab_runner_cache_bucket_name = data.terraform_remote_state.layer1-aws.outputs.gitlab_runner_cache_bucket_name
8-
9-
gitlab_runner_template = templatefile("${path.module}/templates/gitlab-runner-values.tmpl",
7+
gitlab_runner_template = templatefile("${path.module}/templates/gitlab-runner-values.yaml",
108
{
119
registration_token = local.gitlab_registration_token
1210
namespace = module.ci_namespace.name
1311
role_arn = module.aws_iam_gitlab_runner.role_arn
14-
bucket_name = local.gitlab_runner_cache_bucket_name
12+
bucket_name = aws_s3_bucket.gitlab_runner_cache.id
1513
region = local.region
1614
})
17-
1815
}
1916

2017
module "gitlab_runner_namespace" {
@@ -36,6 +33,49 @@ resource "helm_release" "gitlab_runner" {
3633
]
3734
}
3835

36+
resource "aws_s3_bucket" "gitlab_runner_cache" {
37+
bucket = "${local.name}-gitlab-runner-cache"
38+
acl = "private"
39+
40+
server_side_encryption_configuration {
41+
rule {
42+
apply_server_side_encryption_by_default {
43+
sse_algorithm = "aws:kms"
44+
}
45+
}
46+
}
47+
48+
lifecycle_rule {
49+
id = "gitlab-runner-cache-lifecycle-rule"
50+
enabled = true
51+
52+
tags = {
53+
"rule" = "gitlab-runner-cache-lifecycle-rule"
54+
}
55+
56+
expiration {
57+
days = 120
58+
}
59+
}
60+
61+
tags = {
62+
Name = "${local.name}-gitlab-runner-cache"
63+
Environment = local.env
64+
}
65+
}
66+
67+
resource "aws_s3_bucket_public_access_block" "gitlab_runner_cache_public_access_block" {
68+
bucket = aws_s3_bucket.gitlab_runner_cache.id
69+
# Block new public ACLs and uploading public objects
70+
block_public_acls = true
71+
# Retroactively remove public access granted through public ACLs
72+
ignore_public_acls = true
73+
# Block new public bucket policies
74+
block_public_policy = true
75+
# Retroactivley block public and cross-account access if bucket has public policies
76+
restrict_public_buckets = true
77+
}
78+
3979
module "aws_iam_gitlab_runner" {
4080
source = "../modules/aws-iam-eks-trusted"
4181

@@ -58,10 +98,15 @@ module "aws_iam_gitlab_runner" {
5898
"s3:*"
5999
],
60100
"Resource" : [
61-
"arn:aws:s3:::${local.gitlab_runner_cache_bucket_name}",
62-
"arn:aws:s3:::${local.gitlab_runner_cache_bucket_name}/*"
101+
"arn:aws:s3:::${aws_s3_bucket.gitlab_runner_cache.id}",
102+
"arn:aws:s3:::${aws_s3_bucket.gitlab_runner_cache.id}/*"
63103
]
64104
}
65105
]
66106
})
67107
}
108+
109+
output "gitlab_runner_cache_bucket_name" {
110+
value = aws_s3_bucket.gitlab_runner_cache.id
111+
description = "Name of the s3 bucket for gitlab-runner cache"
112+
}

0 commit comments

Comments
 (0)