-
Notifications
You must be signed in to change notification settings - Fork 17
/
main.tf
182 lines (148 loc) · 4.03 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
/**
* A Terraform module that configures an s3 bucket for use with Terraform's remote state feature.
*
* Useful for creating a common bucket naming convention and attaching a bucket policy using the specified role.
*/
# the primary role that will be used to access the tf remote state
variable "role" {
}
# additional roles that should be granted access to the tfstate
variable "additional_roles" {
type = list
default = []
}
# the application that will be using this remote state
variable "application" {
}
# tags
variable "tags" {
type = map(string)
}
//incomplete multipart upload deletion
variable "multipart_delete" {
default = true
}
variable "multipart_days" {
default = 3
}
# whether or not to set force_destroy on the bucket
variable "force_destroy" {
default = true
}
# ensure bucket access is "Bucket and objects not public"
variable "block_public_access" {
default = true
}
# if enabled, we will create a dynamodb table that can be used to store state file lock status
variable "dynamodb_state_locking" {
default = false
}
# bucket for storing tf state
resource "aws_s3_bucket" "bucket" {
bucket = "tf-state-${var.application}"
force_destroy = var.force_destroy
tags = var.tags
lifecycle {
ignore_changes = [ logging ]
}
}
resource "aws_s3_bucket_versioning" "bucket_versioning" {
bucket = aws_s3_bucket.bucket.id
versioning_configuration {
status = "Enabled"
}
}
resource "aws_s3_bucket_server_side_encryption_configuration" "bucket_encryption" {
bucket = aws_s3_bucket.bucket.id
rule {
apply_server_side_encryption_by_default {
sse_algorithm = "AES256"
}
}
}
resource "aws_s3_bucket_lifecycle_configuration" "bucket_lifecycle" {
bucket = aws_s3_bucket.bucket.id
rule {
id = "auto-delete-incomplete-after-x-days"
status = var.multipart_delete ? "Enabled" : "Disabled"
abort_incomplete_multipart_upload {
days_after_initiation = var.multipart_days
}
expiration {
expired_object_delete_marker = false
}
}
}
# explicitly block public access
resource "aws_s3_bucket_public_access_block" "bucket" {
count = var.block_public_access ? 1 : 0
depends_on = [aws_s3_bucket_policy.bucket_policy]
bucket = aws_s3_bucket.bucket.id
block_public_acls = true
block_public_policy = true
ignore_public_acls = true
restrict_public_buckets = true
}
# dynamodb table used to storing lock state
resource "aws_dynamodb_table" "state_lock_table" {
count = var.dynamodb_state_locking ? 1 : 0
name = "tf-state-lock-${var.application}"
billing_mode = "PAY_PER_REQUEST"
hash_key = "LockID"
tags = var.tags
attribute {
name = "LockID"
type = "S"
}
}
# grant bucket owner full control of all objects; disable per-object ACLs
resource "aws_s3_bucket_ownership_controls" "bucket_owner" {
bucket = aws_s3_bucket.bucket.id
rule {
object_ownership = "BucketOwnerPreferred"
}
}
# lookup the role arn
data "aws_iam_role" "role" {
name = var.role
}
data "aws_iam_role" "additional_roles" {
for_each = toset(var.additional_roles)
name = each.key
}
# grant the roles access to the bucket
resource "aws_s3_bucket_policy" "bucket_policy" {
depends_on = [aws_s3_bucket.bucket]
bucket = aws_s3_bucket.bucket.id
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal":{
"AWS": [
%{ for r in data.aws_iam_role.additional_roles }
"${r.arn}",
%{ endfor }
"${data.aws_iam_role.role.arn}"
]
},
"Action": [ "s3:*" ],
"Resource": [
"${aws_s3_bucket.bucket.arn}",
"${aws_s3_bucket.bucket.arn}/*"
]
}
]
}
EOF
}
# the created bucket
output "bucket" {
value = aws_s3_bucket.bucket.bucket
}
# dynamodb table
output "dynamodb_lock_table" {
value = var.dynamodb_state_locking ? aws_dynamodb_table.state_lock_table[0].name : null
}