forked from cloudposse/terraform-aws-s3-bucket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
95 lines (81 loc) · 2.81 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
module "default_label" {
source = "git::https://github.com/cloudposse/terraform-null-label.git?ref=tags/0.3.3"
enabled = "${var.enabled}"
namespace = "${var.namespace}"
stage = "${var.stage}"
name = "${var.name}"
delimiter = "${var.delimiter}"
attributes = "${var.attributes}"
tags = "${var.tags}"
}
resource "aws_s3_bucket" "default" {
count = "${var.enabled == "true" ? 1 : 0}"
bucket = "${module.default_label.id}"
acl = "${var.acl}"
region = "${var.region}"
force_destroy = "${var.force_destroy}"
policy = "${var.policy}"
versioning {
enabled = "${var.versioning_enabled}"
}
# https://docs.aws.amazon.com/AmazonS3/latest/dev/bucket-encryption.html
# https://www.terraform.io/docs/providers/aws/r/s3_bucket.html#enable-default-server-side-encryption
server_side_encryption_configuration {
rule {
apply_server_side_encryption_by_default {
sse_algorithm = "${var.sse_algorithm}"
kms_master_key_id = "${var.kms_master_key_id}"
}
}
}
tags = "${module.default_label.tags}"
}
module "s3_user" {
source = "git::https://github.com/cloudposse/terraform-aws-iam-s3-user.git?ref=tags/0.3.1"
namespace = "${var.namespace}"
stage = "${var.stage}"
name = "${var.name}"
attributes = "${var.attributes}"
tags = "${var.tags}"
enabled = "${var.enabled == "true" && var.user_enabled == "true" ? "true" : "false"}"
s3_actions = ["${var.allowed_bucket_actions}"]
s3_resources = ["${join("", aws_s3_bucket.default.*.arn)}/*", "${join("", aws_s3_bucket.default.*.arn)}"]
}
data "aws_iam_policy_document" "bucket_policy" {
count = "${var.enabled == "true" && var.allow_encrypted_uploads_only == "true" ? 1 : 0}"
statement {
sid = "DenyIncorrectEncryptionHeader"
effect = "Deny"
actions = ["s3:PutObject"]
resources = ["arn:aws:s3:::${aws_s3_bucket.default.id}/*"]
principals {
identifiers = ["*"]
type = "*"
}
condition {
test = "StringNotEquals"
values = ["${var.sse_algorithm}"]
variable = "s3:x-amz-server-side-encryption"
}
}
statement {
sid = "DenyUnEncryptedObjectUploads"
effect = "Deny"
actions = ["s3:PutObject"]
resources = ["arn:aws:s3:::${aws_s3_bucket.default.id}/*"]
principals {
identifiers = ["*"]
type = "*"
}
condition {
test = "Null"
values = ["true"]
variable = "s3:x-amz-server-side-encryption"
}
}
}
resource "aws_s3_bucket_policy" "default" {
count = "${var.enabled == "true" && var.allow_encrypted_uploads_only == "true" ? 1 : 0}"
bucket = "${join("", aws_s3_bucket.default.*.id)}"
policy = "${join("", data.aws_iam_policy_document.bucket_policy.*.json)}"
}