Skip to content
This repository has been archived by the owner on Apr 18, 2022. It is now read-only.

Commit

Permalink
Add preview site support for static sites (#26)
Browse files Browse the repository at this point in the history
* Add preview site support for static sites

* Fix Route53 for preview site to go to website version

* Typo
  • Loading branch information
robertfairhead authored Jan 3, 2019
1 parent d645a33 commit e6bdb97
Showing 1 changed file with 54 additions and 1 deletion.
55 changes: 54 additions & 1 deletion static_site/main.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
####
# Internal DNS names to allow for predictable names in code and as light-weight service discovery
# Main infrastructure to host a static website
####

# Setup DNS records for the static site
Expand Down Expand Up @@ -82,6 +82,9 @@ resource "aws_cloudfront_distribution" "s3_distribution" {
cached_methods = ["GET", "HEAD"]
target_origin_id = "${var.subdomain}-${var.domain_name}"

# Use gzip compression
compress = true

forwarded_values {
query_string = false

Expand Down Expand Up @@ -113,3 +116,53 @@ resource "aws_cloudfront_distribution" "s3_distribution" {
name = "cdn-${var.subdomain}.${var.domain_name}"
}
}

####
# Create preview site to hold builds of PRs
####

resource "aws_route53_record" "preview" {
zone_id = "${data.aws_route53_zone.domain.zone_id}"
name = "preview.${var.subdomain}"
type = "CNAME"
ttl = "300"

records = ["${aws_s3_bucket.preview.website_domain}"]
}

# Create bucket to host the content

resource "aws_s3_bucket" "preview" {
bucket = "preview.${var.subdomain}.${var.domain_name}"
acl = "public-read"

policy = <<POLICY
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "Stmt1516660482311",
"Effect": "Allow",
"Principal": "*",
"Action": [
"s3:GetObject"
],
"Resource": [
"arn:aws:s3:::preview.${var.subdomain}.${var.domain_name}/*"
]
}
]
}
POLICY

website {
index_document = "index.html"
error_document = "error.html"
}

tags {
env = "${var.env}"
terraform = "true"
name = "preview.${var.subdomain}.${var.domain_name}"
}
}

0 comments on commit e6bdb97

Please sign in to comment.