forked from nozaq/terraform-aws-remote-state-s3-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bucket.tf
143 lines (115 loc) · 3.9 KB
/
bucket.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
locals {
define_lifecycle_rule = var.noncurrent_version_expiration != null || length(var.noncurrent_version_transitions) > 0
}
data "aws_region" "state" {
}
#---------------------------------------------------------------------------------------------------
# KMS Key to Encrypt S3 Bucket
#---------------------------------------------------------------------------------------------------
resource "aws_kms_key" "this" {
description = var.kms_key_description
deletion_window_in_days = var.kms_key_deletion_window_in_days
enable_key_rotation = var.kms_key_enable_key_rotation
tags = var.tags
}
resource "aws_kms_alias" "this" {
name = "alias/${var.kms_key_alias}"
target_key_id = aws_kms_key.this.key_id
}
#---------------------------------------------------------------------------------------------------
# Bucket Policies
#---------------------------------------------------------------------------------------------------
data "aws_iam_policy_document" "state_force_ssl" {
statement {
sid = "AllowSSLRequestsOnly"
actions = ["s3:*"]
effect = "Deny"
resources = [
aws_s3_bucket.state.arn,
"${aws_s3_bucket.state.arn}/*"
]
condition {
test = "Bool"
variable = "aws:SecureTransport"
values = ["false"]
}
principals {
type = "*"
identifiers = ["*"]
}
}
}
#---------------------------------------------------------------------------------------------------
# Bucket
#---------------------------------------------------------------------------------------------------
resource "aws_s3_bucket_policy" "state_force_ssl" {
bucket = aws_s3_bucket.state.id
policy = data.aws_iam_policy_document.state_force_ssl.json
depends_on = [aws_s3_bucket_public_access_block.state]
}
resource "aws_s3_bucket" "state" {
bucket_prefix = var.override_s3_bucket_name ? null : var.state_bucket_prefix
bucket = var.override_s3_bucket_name ? var.s3_bucket_name : null
force_destroy = var.s3_bucket_force_destroy
tags = var.tags
}
resource "aws_s3_bucket_ownership_controls" "state" {
bucket = aws_s3_bucket.state.id
rule {
object_ownership = "BucketOwnerPreferred"
}
}
resource "aws_s3_bucket_acl" "state" {
depends_on = [aws_s3_bucket_ownership_controls.state]
bucket = aws_s3_bucket.state.id
acl = "private"
}
resource "aws_s3_bucket_versioning" "state" {
bucket = aws_s3_bucket.state.id
versioning_configuration {
status = "Enabled"
}
}
resource "aws_s3_bucket_logging" "state" {
count = var.s3_logging_target_bucket != null ? 1 : 0
bucket = aws_s3_bucket.state.id
target_bucket = var.s3_logging_target_bucket
target_prefix = var.s3_logging_target_prefix
}
resource "aws_s3_bucket_server_side_encryption_configuration" "state" {
bucket = aws_s3_bucket.state.id
rule {
apply_server_side_encryption_by_default {
sse_algorithm = "aws:kms"
kms_master_key_id = aws_kms_key.this.arn
}
}
}
resource "aws_s3_bucket_lifecycle_configuration" "state" {
count = local.define_lifecycle_rule ? 1 : 0
bucket = aws_s3_bucket.state.id
rule {
id = "auto-archive"
status = "Enabled"
dynamic "noncurrent_version_transition" {
for_each = var.noncurrent_version_transitions
content {
noncurrent_days = noncurrent_version_transition.value.days
storage_class = noncurrent_version_transition.value.storage_class
}
}
dynamic "noncurrent_version_expiration" {
for_each = var.noncurrent_version_expiration != null ? [var.noncurrent_version_expiration] : []
content {
noncurrent_days = noncurrent_version_expiration.value.days
}
}
}
}
resource "aws_s3_bucket_public_access_block" "state" {
bucket = aws_s3_bucket.state.id
block_public_acls = true
block_public_policy = true
ignore_public_acls = true
restrict_public_buckets = true
}