-
Notifications
You must be signed in to change notification settings - Fork 33
/
main.tf
161 lines (151 loc) · 4.3 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
/*
* Module: terraform-aws-backend
*
* Bootstrap your terraform backend on AWS.
*
* This module configures resources for state locking for terraform >= 0.9.0
* https://github.com/hashicorp/terraform/blob/master/CHANGELOG.md#090-march-15-2017
*
* This template creates and/or manages the following resources
* - An S3 Bucket for storing terraform state
* - An S3 Bucket for storing logs from the state bucket
* - A DynamoDB table to be used for state locking and consistency
*
* The DynamoDB state locking table is optional: to disable,
* set the 'dynamodb_lock_table_enabled' variable to false.
* For more info on how terraform handles boolean variables:
* - https://www.terraform.io/docs/configuration/variables.html
*
* If using an existing S3 Bucket, perform a terraform import on your bucket
* into your terraform-aws-backend module instance:
*
* $ terraform import module.backend.aws_s3_bucket.tf_backend_bucket <your_s3_bucket_name>
*
* where the 'backend' portion is the name you choose:
*
* module "backend" {
* source = "github.com/samstav/terraform-aws-backend"
* }
*
*/
data "aws_caller_identity" "current" {
}
resource "aws_dynamodb_table" "tf_backend_state_lock_table" {
count = var.dynamodb_lock_table_enabled ? 1 : 0
name = var.dynamodb_lock_table_name
read_capacity = var.lock_table_read_capacity
write_capacity = var.lock_table_write_capacity
hash_key = "LockID"
stream_enabled = var.dynamodb_lock_table_stream_enabled
stream_view_type = var.dynamodb_lock_table_stream_enabled ? var.dynamodb_lock_table_stream_view_type : ""
attribute {
name = "LockID"
type = "S"
}
tags = {
Description = "Terraform state locking table for account ${data.aws_caller_identity.current.account_id}."
ManagedByTerraform = "true"
TerraformModule = "terraform-aws-backend"
}
lifecycle {
prevent_destroy = true
}
}
resource "aws_s3_bucket" "tf_backend_bucket" {
bucket = var.backend_bucket
acl = "private"
versioning {
enabled = true
}
logging {
target_bucket = aws_s3_bucket.tf_backend_logs_bucket.id
target_prefix = "log/"
}
tags = {
Description = "Terraform S3 Backend bucket which stores the terraform state for account ${data.aws_caller_identity.current.account_id}."
ManagedByTerraform = "true"
TerraformModule = "terraform-aws-backend"
}
server_side_encryption_configuration {
rule {
apply_server_side_encryption_by_default {
kms_master_key_id = var.kms_key_id
sse_algorithm = var.kms_key_id == "" ? "AES256" : "aws:kms"
}
}
}
lifecycle {
prevent_destroy = true
}
}
data "aws_iam_policy_document" "tf_backend_bucket_policy" {
statement {
sid = "RequireEncryptedTransport"
effect = "Deny"
actions = [
"s3:*",
]
resources = [
"${aws_s3_bucket.tf_backend_bucket.arn}/*",
]
condition {
test = "Bool"
variable = "aws:SecureTransport"
values = [
false,
]
}
principals {
type = "*"
identifiers = ["*"]
}
}
statement {
sid = "RequireEncryptedStorage"
effect = "Deny"
actions = [
"s3:PutObject",
]
resources = [
"${aws_s3_bucket.tf_backend_bucket.arn}/*",
]
condition {
test = "StringNotEquals"
variable = "s3:x-amz-server-side-encryption"
values = [
var.kms_key_id == "" ? "AES256" : "aws:kms",
]
}
principals {
type = "*"
identifiers = ["*"]
}
}
}
resource "aws_s3_bucket_policy" "tf_backend_bucket_policy" {
bucket = aws_s3_bucket.tf_backend_bucket.id
policy = data.aws_iam_policy_document.tf_backend_bucket_policy.json
}
resource "aws_s3_bucket" "tf_backend_logs_bucket" {
bucket = "${var.backend_bucket}-logs"
acl = "log-delivery-write"
versioning {
enabled = true
}
server_side_encryption_configuration {
rule {
apply_server_side_encryption_by_default {
kms_master_key_id = var.kms_key_id
sse_algorithm = var.kms_key_id == "" ? "AES256" : "aws:kms"
}
}
}
tags = {
Purpose = "Logging bucket for ${var.backend_bucket}"
ManagedByTerraform = "true"
TerraformModule = "terraform-aws-backend"
}
lifecycle {
prevent_destroy = true
}
}