-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.tf
104 lines (93 loc) · 2.39 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
terraform {
backend "s3" {
bucket = "hands-on-cloud-terraform-remote-state-s3"
key = "demo-tf-project.tfstate"
region = "us-west-2"
encrypt = "true"
}
}
data "aws_caller_identity" "current" {}
locals {
common_tags = {
Project = "hands-on-cloud"
ManagedBy = "Terraform"
Environment = var.env
}
}
resource "aws_s3_bucket" "test" {
#ts:skip=AC_AWS_0497 We don't need logging for this S3 bucket
bucket = var.name
acl = "private"
versioning {
enabled = true
}
server_side_encryption_configuration {
rule {
apply_server_side_encryption_by_default {
kms_master_key_id = aws_kms_key.this.arn
sse_algorithm = "aws:kms"
}
}
}
lifecycle {
prevent_destroy = false
}
tags = local.common_tags
}
resource "aws_s3_bucket_public_access_block" "s3Public_test" {
depends_on = [aws_s3_bucket_policy.test]
bucket = aws_s3_bucket.test.id
block_public_acls = true
block_public_policy = true
ignore_public_acls = true
restrict_public_buckets = true
}
resource "aws_s3_bucket_policy" "test" {
bucket = aws_s3_bucket.test.id
policy = <<POLICY
{
"Version": "2008-10-17",
"Statement": [
{
"Sid": "AllowedAccess",
"Effect": "allow",
"Principal": { "AWS": "${data.aws_caller_identity.current.account_id}" },
"Action": "s3:*",
"Resource": [
"${aws_s3_bucket.test.arn}",
"${aws_s3_bucket.test.arn}/*"
]
},
{
"Sid": "DenyInsecureAccess",
"Effect": "Deny",
"Principal": "*",
"Action": "s3:*",
"Resource": [
"${aws_s3_bucket.test.arn}",
"${aws_s3_bucket.test.arn}/*"
],
"Condition": {
"Bool": {
"aws:SecureTransport": "false"
}
}
},
{
"Sid": "EnforceEncryption",
"Effect": "Deny",
"Principal": "*",
"Action": "s3:PutObject",
"Resource": [
"${aws_s3_bucket.test.arn}/*"
],
"Condition": {
"StringNotEquals": {
"s3:x-amz-server-side-encryption": "aws:kms"
}
}
}
]
}
POLICY
}