forked from cloudposse/terraform-aws-tfstate-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
226 lines (182 loc) · 5.52 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
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
)
bucket_name = var.s3_bucket_name != "" ? var.s3_bucket_name : module.s3_bucket_label.id
}
module "base_label" {
source = "git::https://github.com/cloudposse/terraform-null-label.git?ref=tags/0.13.0"
namespace = var.namespace
environment = var.environment
stage = var.stage
name = var.name
delimiter = var.delimiter
attributes = var.attributes
tags = var.tags
additional_tag_map = var.additional_tag_map
context = var.context
label_order = var.label_order
regex_replace_chars = var.regex_replace_chars
}
module "s3_bucket_label" {
source = "git::https://github.com/cloudposse/terraform-null-label.git?ref=tags/0.13.0"
context = module.base_label.context
}
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 = [
"arn:aws:s3:::${local.bucket_name}/*",
]
condition {
test = "StringNotEquals"
variable = "s3:x-amz-server-side-encryption"
values = [
"AES256",
]
}
}
statement {
sid = "DenyUnEncryptedObjectUploads"
effect = "Deny"
principals {
identifiers = ["*"]
type = "AWS"
}
actions = [
"s3:PutObject",
]
resources = [
"arn:aws:s3:::${local.bucket_name}/*",
]
condition {
test = "Null"
variable = "s3:x-amz-server-side-encryption"
values = [
"true",
]
}
}
}
resource "aws_s3_bucket" "default" {
bucket = substr(local.bucket_name, 0, 63)
acl = var.acl
region = var.region
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"
}
}
}
tags = module.s3_bucket_label.tags
}
resource "aws_s3_bucket_public_access_block" "default" {
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 = "git::https://github.com/cloudposse/terraform-null-label.git?ref=tags/0.13.0"
context = module.base_label.context
attributes = compact(concat(var.attributes, ["lock"]))
}
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 "template_file" "terraform_backend_config" {
template = file("${path.module}/templates/terraform.tf.tpl")
vars = {
region = var.region
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
}
}
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
}