forked from Smartbrood/terraform-aws-s3-bucket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
105 lines (94 loc) · 2.63 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
locals {
defaultLoggingBucket = "${var.s3_fqdn}-bucket-log"
}
resource "aws_s3_bucket" "bucket_log" {
count = var.loggingBucket == "" && var.create_logging_bucket && var.create_bucket ? 1 : 0
bucket = local.defaultLoggingBucket
acl = "log-delivery-write"
tags = {
name = "LoggingBucket"
}
}
resource "aws_s3_bucket" "this" {
count = var.create_bucket ? 1 : 0
bucket = var.s3_fqdn
force_destroy = true
tags = merge(var.tags, tomap({"Name" = format("%s", var.s3_fqdn)}))
dynamic "logging" {
for_each = var.create_logging_bucket == true ? [1] : []
content {
target_bucket = var.loggingBucket != "" ? var.loggingBucket : local.defaultLoggingBucket
target_prefix = "log/"
}
}
versioning {
enabled = var.enable_versioning
}
}
resource "aws_s3_bucket_policy" "private" {
count = var.allow_public != true && var.create_bucket ? 1 : 0
bucket = aws_s3_bucket.this[0].id
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "",
"Action": "s3:*",
"Effect": "Allow",
"Resource": ["arn:aws:s3:::${var.s3_fqdn}",
"arn:aws:s3:::${var.s3_fqdn}/*"],
"Principal": {
"AWS": "arn:aws:iam::${var.aws_account_id}:user/${var.aws_username}"
}
}
]
}
EOF
}
resource "aws_s3_bucket_policy" "public" {
count = var.allow_public && var.create_bucket ? 1 : 0
bucket = aws_s3_bucket.this[0].id
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "",
"Action": "s3:*",
"Effect": "Allow",
"Resource": ["arn:aws:s3:::${var.s3_fqdn}",
"arn:aws:s3:::${var.s3_fqdn}/*"],
"Principal": {
"AWS": "arn:aws:iam::${var.aws_account_id}:user/${var.aws_username}"
}
},
{
"Sid": "",
"Action": [
"s3:GetObject"
],
"Effect": "Allow",
"Resource": ["arn:aws:s3:::${var.s3_fqdn}",
"arn:aws:s3:::${var.s3_fqdn}/*"],
"Principal": {
"AWS": "*"
}
}
]
}
EOF
}
resource "aws_s3_bucket_object" "file" {
count = var.upload_files ? length(var.files) : 0
bucket = var.s3_fqdn
key = element(keys(var.files), count.index)
source = lookup(var.files, element(keys(var.files), count.index))
etag = md5(file(lookup(var.files, element(keys(var.files), count.index))))
}
resource "aws_s3_bucket_object" "base64_file" {
count = var.upload_files ? length(var.base64_files) : 0
bucket = var.s3_fqdn
key = element(keys(var.base64_files), count.index)
content_base64 = lookup(var.base64_files, element(keys(var.base64_files), count.index))
}