-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
67 lines (61 loc) · 1.73 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
data "aws_canonical_user_id" "current" {}
resource "aws_kms_key" "objects" {
description = "KMS key is used to encrypt bucket objects"
deletion_window_in_days = 7
}
resource "aws_iam_role" "this" {
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "ec2.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}
data "aws_iam_policy_document" "bucket_policy" {
for_each = var.buckets
statement {
principals {
type = "AWS"
identifiers = [aws_iam_role.this.arn]
}
actions = each.value.bucket_policy_actions
resources = [
"arn:aws:s3:::${each.value.bucket}",
"arn:aws:s3:::${each.value.bucket}/*",
]
}
}
module "s3_bucket" {
source = "./terraform-aws-s3-bucket"
for_each = var.buckets
bucket = each.value.bucket
acl = each.value.acl
force_destroy = each.value.force_destroy
attach_policy = each.value.attach_policy
policy = data.aws_iam_policy_document.bucket_policy[each.key].json
tags = each.value.tags
versioning = each.value.versioning
lifecycle_rule = each.value.lifecycle_rule
object_lock_configuration = each.value.object_lock_configuration
server_side_encryption_configuration = {
rule = {
apply_server_side_encryption_by_default = {
kms_master_key_id = aws_kms_key.objects.arn
sse_algorithm = "aws:kms"
}
}
}
block_public_acls = each.value.block_public_acls
block_public_policy = each.value.block_public_policy
ignore_public_acls = each.value.ignore_public_acls
restrict_public_buckets = each.value.restrict_public_buckets
}