forked from cloudposse/terraform-aws-tfstate-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
269 lines (215 loc) · 6.58 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
locals {
prevent_unencrypted_uploads = var.prevent_unencrypted_uploads && var.enable_server_side_encryption ? true : false
policy = local.prevent_unencrypted_uploads ? join(
"",
data.aws_iam_policy_document.prevent_unencrypted_uploads.*.json
) : ""
terraform_backend_config_file = format(
"%s/%s",
var.terraform_backend_config_file_path,
var.terraform_backend_config_file_name
)
terraform_backend_config_template_file = var.terraform_backend_config_template_file != "" ? var.terraform_backend_config_template_file : "${path.module}/templates/terraform.tf.tpl"
bucket_name = var.s3_bucket_name != "" ? var.s3_bucket_name : module.this.id
}
data "aws_iam_policy_document" "prevent_unencrypted_uploads" {
count = local.prevent_unencrypted_uploads ? 1 : 0
statement {
sid = "DenyIncorrectEncryptionHeader"
effect = "Deny"
principals {
identifiers = ["*"]
type = "AWS"
}
actions = [
"s3:PutObject"
]
resources = [
"${var.arn_format}:s3:::${local.bucket_name}/*",
]
condition {
test = "StringNotEquals"
variable = "s3:x-amz-server-side-encryption"
values = [
"AES256",
"aws:kms"
]
}
}
statement {
sid = "DenyUnEncryptedObjectUploads"
effect = "Deny"
principals {
identifiers = ["*"]
type = "AWS"
}
actions = [
"s3:PutObject"
]
resources = [
"${var.arn_format}:s3:::${local.bucket_name}/*",
]
condition {
test = "Null"
variable = "s3:x-amz-server-side-encryption"
values = [
"true"
]
}
}
statement {
sid = "EnforceTlsRequestsOnly"
effect = "Deny"
principals {
type = "AWS"
identifiers = ["*"]
}
actions = ["s3:*"]
resources = [
"${var.arn_format}:s3:::${local.bucket_name}",
"${var.arn_format}:s3:::${local.bucket_name}/*",
]
condition {
test = "Bool"
variable = "aws:SecureTransport"
values = ["false"]
}
}
}
resource "aws_s3_bucket" "default" {
#bridgecrew:skip=BC_AWS_S3_13:Skipping `Enable S3 Bucket Logging` check until Bridgecrew will support dynamic blocks (https://github.com/bridgecrewio/checkov/issues/776).
#bridgecrew:skip=CKV_AWS_52:Skipping `Ensure S3 bucket has MFA delete enabled` check due to issues operating with `mfa_delete` in terraform
bucket = substr(local.bucket_name, 0, 63)
acl = var.acl
force_destroy = var.force_destroy
policy = local.policy
versioning {
enabled = true
mfa_delete = var.mfa_delete
}
server_side_encryption_configuration {
rule {
apply_server_side_encryption_by_default {
sse_algorithm = "AES256"
}
}
}
dynamic "replication_configuration" {
for_each = var.s3_replication_enabled ? toset([var.s3_replica_bucket_arn]) : []
content {
role = aws_iam_role.replication[0].arn
rules {
id = module.this.id
prefix = ""
status = "Enabled"
destination {
bucket = var.s3_replica_bucket_arn
storage_class = "STANDARD"
}
}
}
}
dynamic "logging" {
for_each = var.logging == null ? [] : [1]
content {
target_bucket = var.logging["bucket_name"]
target_prefix = var.logging["prefix"]
}
}
tags = module.this.tags
}
resource "aws_s3_bucket_public_access_block" "default" {
count = var.enable_public_access_block ? 1 : 0
bucket = aws_s3_bucket.default.id
block_public_acls = var.block_public_acls
ignore_public_acls = var.ignore_public_acls
block_public_policy = var.block_public_policy
restrict_public_buckets = var.restrict_public_buckets
}
module "dynamodb_table_label" {
source = "cloudposse/label/null"
version = "0.22.0"
attributes = compact(concat(var.attributes, ["lock"]))
context = module.this.context
}
resource "aws_dynamodb_table" "with_server_side_encryption" {
count = var.enable_server_side_encryption ? 1 : 0
name = module.dynamodb_table_label.id
billing_mode = var.billing_mode
read_capacity = var.billing_mode == "PROVISIONED" ? var.read_capacity : null
write_capacity = var.billing_mode == "PROVISIONED" ? var.write_capacity : null
# https://www.terraform.io/docs/backends/types/s3.html#dynamodb_table
hash_key = "LockID"
server_side_encryption {
enabled = true
}
point_in_time_recovery {
enabled = var.enable_point_in_time_recovery
}
lifecycle {
ignore_changes = [
billing_mode,
read_capacity,
write_capacity,
]
}
attribute {
name = "LockID"
type = "S"
}
tags = module.dynamodb_table_label.tags
}
resource "aws_dynamodb_table" "without_server_side_encryption" {
count = var.enable_server_side_encryption ? 0 : 1
name = module.dynamodb_table_label.id
billing_mode = var.billing_mode
read_capacity = var.billing_mode == "PROVISIONED" ? var.read_capacity : null
write_capacity = var.billing_mode == "PROVISIONED" ? var.write_capacity : null
# https://www.terraform.io/docs/backends/types/s3.html#dynamodb_table
hash_key = "LockID"
point_in_time_recovery {
enabled = var.enable_point_in_time_recovery
}
lifecycle {
ignore_changes = [
billing_mode,
read_capacity,
write_capacity,
]
}
attribute {
name = "LockID"
type = "S"
}
tags = module.dynamodb_table_label.tags
}
data "aws_region" "current" {}
data "template_file" "terraform_backend_config" {
template = file(local.terraform_backend_config_template_file)
vars = {
region = data.aws_region.current.name
bucket = aws_s3_bucket.default.id
dynamodb_table = element(
coalescelist(
aws_dynamodb_table.with_server_side_encryption.*.name,
aws_dynamodb_table.without_server_side_encryption.*.name
),
0
)
encrypt = var.enable_server_side_encryption ? "true" : "false"
role_arn = var.role_arn
profile = var.profile
terraform_version = var.terraform_version
terraform_state_file = var.terraform_state_file
namespace = var.namespace
stage = var.stage
environment = var.environment
name = var.name
}
}
resource "local_file" "terraform_backend_config" {
count = var.terraform_backend_config_file_path != "" ? 1 : 0
content = data.template_file.terraform_backend_config.rendered
filename = local.terraform_backend_config_file
file_permission = "0644"
}