-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
158 lines (122 loc) · 2.99 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
/*
* Copyright (c) 2019 Netic A/S. All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
locals {
tags = {
Terraform = "true"
}
}
// KMS Key
resource "aws_kms_key" "tf_enc_key" {
count = var.bootstrap
description = "Global Terraform state encryption key"
deletion_window_in_days = 30
enable_key_rotation = true
tags = merge(var.tags, local.tags)
lifecycle {
prevent_destroy = true
}
}
resource "random_id" "kms" {
count = var.bootstrap
keepers = {
key_id = "${aws_kms_key.tf_enc_key[count.index].key_id}"
arn = "${aws_kms_key.tf_enc_key[count.index].arn}"
}
byte_length = 8
}
// KMW Key alias
resource "aws_kms_alias" "tf_enc_key" {
count = var.bootstrap
name = "alias/tf-state-${random_id.kms[count.index].dec}"
target_key_id = random_id.kms[count.index].keepers.key_id
}
// S3 Bucket
resource "aws_s3_bucket" "terraform_state" {
count = var.bootstrap
bucket = var.bucket
acl = "private"
versioning {
enabled = var.bucket_versioning_enabled
}
lifecycle_rule {
id = "expire"
enabled = var.bucket_lifecycle_enabled
noncurrent_version_expiration {
days = var.bucket_lifecycle_expiration_days
}
}
server_side_encryption_configuration {
rule {
apply_server_side_encryption_by_default {
kms_master_key_id = random_id.kms[count.index].keepers.arn
sse_algorithm = "aws:kms"
}
}
}
dynamic "logging" {
for_each = var.bucket_logging
content {
target_bucket = logging.value.target_bucket
}
}
tags = merge(var.tags, local.tags)
lifecycle {
prevent_destroy = true
}
}
// S3 Bucket Policy
data "aws_iam_policy_document" "terraform_state" {
statement {
sid = "TerraformStateListBucket"
effect = "Allow"
actions = [
"s3:ListBucket"
]
principals {
type = "AWS"
identifiers = [var.operator_role_arn]
}
resources = [join("", aws_s3_bucket.terraform_state.*.arn)]
}
statement {
sid = "TerraformStateManageObjects"
effect = "Allow"
actions = [
"s3:GetObject",
"s3:PutObject",
]
principals {
type = "AWS"
identifiers = [var.operator_role_arn]
}
resources = [
"${join("", aws_s3_bucket.terraform_state.*.arn)}/${var.key}",
"${join("", aws_s3_bucket.terraform_state.*.arn)}/env:/*/${var.key}",
]
}
}
resource "aws_s3_bucket_policy" "terraform_state" {
count = var.bootstrap
bucket = aws_s3_bucket.terraform_state[count.index].id
policy = data.aws_iam_policy_document.terraform_state.json
}
// DynamoDB
resource "aws_dynamodb_table" "terraform_statelock" {
count = var.bootstrap
name = var.dynamodb_table
read_capacity = 1
write_capacity = 1
hash_key = "LockID"
attribute {
name = "LockID"
type = "S"
}
tags = merge(var.tags, local.tags)
lifecycle {
prevent_destroy = true
}
}