-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
122 lines (87 loc) · 3.21 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
### Defines the infrastructure for hosting a static website using S3 buckets and a CloudFront distribution
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}
variable "bucket-name" {
type = string
description = "name of the website S3 buckets - Note that this has to be globally unique (i.e., for any S3 bucket of any user worldwide)"
}
variable "enable-replication" {
type = bool
default = false
description = "enable replication between S3 buckets - Note that this might incur costs, and contents inside 'website/content' will not be managed by terraform"
}
variable "enable-cloudfront-logging" {
type = bool
default = true
description = "enable standard logging of the CloudFront distribution - Note that this will create a separate S3 bucket"
}
variable "index-document" {
type = string
default = "index.html"
description = "name of the index document for the website"
}
#Create primary and failover S3 buckets
#Because of a limitation in terraform (https://github.com/hashicorp/terraform/issues/24476) providers cannot be defined dynamically
#therefore the websites cannot be created with a for_each, and if you want another failover bucket you have to duplicate the code below
variable "primary-region" {
type = string
description = "region for primary S3 bucket"
}
variable "failover-region" {
type = string
description = "region for failover S3 bucket"
}
provider "aws" {
alias = "primary"
region = var.primary-region
}
provider "aws" {
alias = "failover"
region = var.failover-region
}
module "primary-website" {
source = "./website"
providers = { aws = aws.primary }
name = var.bucket-name
enable-versioning = var.enable-replication
upload-content = var.enable-replication ? false : true
}
module "failover-website" {
source = "./website"
providers = { aws = aws.failover }
name = "${var.bucket-name}-failover"
enable-versioning = var.enable-replication
upload-content = var.enable-replication ? false : true
}
# Enable replication
module "replication" {
source = "./replication"
count = var.enable-replication ? 1 : 0
depends_on = [module.primary-website.versioning, module.failover-website.versioning] #add additional failover versioning here
providers = { aws = aws.primary }
source-bucket = module.primary-website.bucket
destination-buckets = [module.failover-website.bucket] #add additional failover buckets here
}
# Configure CloudFront distribution
provider "aws" {
alias = "cloudfront"
region = "us-east-1" #default CloudFront region independent of website regions - Do not change if you want to enable logging
}
module "cloudfront" {
source = "./cloudfront"
providers = { aws = aws.cloudfront }
origins = [module.primary-website.bucket.bucket_regional_domain_name
, module.failover-website.bucket.bucket_regional_domain_name] #add additional failover bucket endpoints here
enable-logging = var.enable-cloudfront-logging
index-document = var.index-document
}
#Outputs
output "URL" {
value = "https://${module.cloudfront.distribution.domain_name}"
}