-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.tf
122 lines (94 loc) · 2.79 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
data "aws_caller_identity" "current" {}
data "aws_partition" "current" {}
data "aws_region" "current" {}
locals {
account_id = data.aws_caller_identity.current.account_id
partition = data.aws_partition.current.partition
region = data.aws_region.current.name
# Resolve resource names
bucket_name = try(var.bucket_name, "${local.account_id}-${local.region}-tfstate")
kms_key_id = try(aws_kms_key.this[0].arn, var.kms_key_id)
}
# tfsec is not yet smart enough to know new tf syntax for crypto/logging
#tfsec:ignore:AWS017 #tfsec:ignore:AWS002
resource "aws_s3_bucket" "this" {
bucket = local.bucket_name
tags = merge(var.tags, {
"Name" = local.bucket_name
})
}
resource "aws_s3_bucket_ownership_controls" "this" {
bucket = aws_s3_bucket.this.id
rule {
object_ownership = "BucketOwnerEnforced"
}
}
resource "aws_s3_bucket_lifecycle_configuration" "this" {
count = var.lifecycle_rules == null ? 0 : 1
bucket = aws_s3_bucket.this.id
dynamic "rule" {
iterator = rule
for_each = var.lifecycle_rules
content {
id = rule.value.id
status = rule.value.enabled ? "Enabled" : "Disabled"
filter {
prefix = lookup(rule.value, "prefix", null)
}
expiration {
days = lookup(rule.value, "expiration", 2147483647)
}
noncurrent_version_expiration {
noncurrent_days = lookup(rule.value, "noncurrent_version_expiration", 2147483647)
}
}
}
depends_on = [aws_s3_bucket_versioning.this]
}
resource "aws_s3_bucket_logging" "this" {
count = var.logging_target_bucket == null ? 0 : 1
bucket = aws_s3_bucket.this.id
target_bucket = var.logging_target_bucket
target_prefix = var.logging_target_prefix
}
resource "aws_s3_bucket_server_side_encryption_configuration" "this" {
bucket = aws_s3_bucket.this.bucket
rule {
apply_server_side_encryption_by_default {
kms_master_key_id = local.kms_key_id
sse_algorithm = "aws:kms"
}
}
}
resource "aws_s3_bucket_versioning" "this" {
bucket = aws_s3_bucket.this.id
versioning_configuration {
status = "Enabled"
}
}
resource "aws_s3_bucket_public_access_block" "this" {
bucket = aws_s3_bucket.this.id
block_public_acls = true
block_public_policy = true
ignore_public_acls = true
restrict_public_buckets = true
}
resource "aws_dynamodb_table" "this" {
name = var.dynamo_locktable_name
billing_mode = "PAY_PER_REQUEST"
hash_key = "LockID"
tags = merge(var.tags, {
"Name" = var.dynamo_locktable_name
})
attribute {
name = "LockID"
type = "S"
}
point_in_time_recovery {
enabled = var.dynamodb_point_in_time_recovery
}
server_side_encryption {
enabled = var.dynamodb_server_side_encryption
kms_key_arn = var.dynamodb_kms_key_arn
}
}