-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
122 lines (106 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
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
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.0"
}
random = {
source = "hashicorp/random"
version = "~> 3.4"
}
}
}
provider "aws" {
region = var.region
default_tags {
tags = {
environment = var.env
department = "SEA"
application = "HashiCafe website"
}
}
}
locals {
timestamp = timestamp()
}
resource "random_integer" "product" {
min = 0
max = length(var.hashi_products) - 1
keepers = {
"timestamp" = local.timestamp
}
}
resource "aws_s3_bucket" "www_bucket" {
bucket_prefix = "${var.prefix}-hashicafe-website-${lower(var.env)}-"
force_destroy = true
tags = {
hc-internet-facing = "true"
}
}
resource "aws_s3_bucket_website_configuration" "www_bucket" {
bucket = aws_s3_bucket.www_bucket.id
index_document {
suffix = "index.html"
}
}
resource "aws_s3_bucket_ownership_controls" "www_bucket" {
bucket = aws_s3_bucket.www_bucket.id
rule {
object_ownership = "BucketOwnerEnforced"
}
}
resource "aws_s3_bucket_public_access_block" "www_bucket" {
bucket = aws_s3_bucket.www_bucket.id
block_public_acls = false
block_public_policy = false
ignore_public_acls = false
restrict_public_buckets = false
}
resource "aws_s3_bucket_policy" "www_bucket" {
bucket = aws_s3_bucket.www_bucket.id
policy = data.aws_iam_policy_document.s3_public_access_policy.json
depends_on = [aws_s3_bucket_public_access_block.www_bucket]
}
data "aws_iam_policy_document" "s3_public_access_policy" {
statement {
sid = "PublicAccess"
effect = "Allow"
actions = ["s3:GetObject"]
principals {
type = "*"
identifiers = ["*"]
}
resources = ["${aws_s3_bucket.www_bucket.arn}/*"]
}
}
resource "aws_s3_bucket_server_side_encryption_configuration" "www_bucket" {
bucket = aws_s3_bucket.www_bucket.id
rule {
apply_server_side_encryption_by_default {
sse_algorithm = "AES256"
}
}
}
resource "aws_s3_bucket_versioning" "www_bucket" {
bucket = aws_s3_bucket.www_bucket.id
versioning_configuration {
status = "Enabled"
}
}
resource "aws_s3_object" "index" {
key = "index.html"
bucket = aws_s3_bucket.www_bucket.id
content = templatefile("${path.module}/files/index.html", {
product_name = var.hashi_products[random_integer.product.result].name
product_color = var.hashi_products[random_integer.product.result].color
product_image = var.hashi_products[random_integer.product.result].image_file
})
content_type = "text/html"
}
resource "aws_s3_object" "images" {
for_each = fileset("${path.module}/files/img/", "*.png")
bucket = aws_s3_bucket.www_bucket.id
key = "img/${each.value}"
source = "${path.module}/files/img/${each.value}"
content_type = "image/png"
}