-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.tf
82 lines (69 loc) · 1.77 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
# ~~~~~~~~~~~~~~~~~~~~~~~
# DEPLOY A STATIC WEBSITE
# ~~~~~~~~~~~~~~~~~~~~~~~
# REQUIRE A SPECIFIC TERRAFORM VERSION OR HIGHER
terraform {
required_version = ">= 0.12.20"
}
# ---------
# VARIABLES
# ---------
variable "website_name" {
description = "The name of your static website."
type = string
}
variable "region" {
description = "AWS region this bucket should reside in"
type = string
}
variable "force_destroy" {
description = "Delete all objects from the bucket so that the bucket can be destroyed even when not empty"
type = bool
default = false
}
variable "tags" {
description = "Tags to add to resources"
type = map(string)
default = {}
}
# ------------------------------------------------
# CREATE THE S3 WEBSITE BUCKET AND ATTACH A POLICY
# ------------------------------------------------
resource "aws_s3_bucket" "website" {
bucket = var.website_name
acl = "public-read"
tags = var.tags
region = var.region
force_destroy = var.force_destroy
website {
index_document = "index.html"
error_document = "error.html"
}
}
resource "aws_s3_bucket_policy" "website" {
bucket = aws_s3_bucket.website.id
policy = data.aws_iam_policy_document.website.json
}
data "aws_iam_policy_document" "website" {
statement {
actions = [
"s3:GetObject"]
resources = [
"${aws_s3_bucket.website.arn}/*"]
principals {
type = "AWS"
identifiers = ["*"]
}
}
}
# -------
# OUTPUTS
# -------
output "s3_bucket_website_endpoint" {
value = aws_s3_bucket.website.website_endpoint
description = "Website endpoint for the S3 bucket"
}
output "s3_bucket_name" {
description = "Name of of website bucket"
value = aws_s3_bucket.website.id
}