Skip to content

Commit

Permalink
Merge pull request #5 from fillup/develop
Browse files Browse the repository at this point in the history
Upgrade to Terraform 0.12
  • Loading branch information
fillup authored Feb 10, 2020
2 parents a94f104 + 338ddfd commit d22b0a4
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 46 deletions.
48 changes: 24 additions & 24 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,48 @@
* Create S3 bucket with appropriate permissions
*/
data "template_file" "bucket_policy" {
template = "${file("${path.module}/bucket-policy.json")}"
template = file("${path.module}/bucket-policy.json")

vars {
bucket_name = "${var.bucket_name}"
deployment_user_arn = "${var.deployment_user_arn}"
vars = {
bucket_name = var.bucket_name
deployment_user_arn = var.deployment_user_arn
}
}

resource "aws_s3_bucket" "hugo" {
bucket = "${var.bucket_name}"
bucket = var.bucket_name
acl = "public-read"
policy = "${data.template_file.bucket_policy.rendered}"
policy = data.template_file.bucket_policy.rendered
force_destroy = true

website {
index_document = "index.html"
error_document = "${var.origin_path}/404.html"

// Routing rule is needed to support hugo friendly urls
routing_rules = "${var.routing_rules}"
routing_rules = var.routing_rules
}

cors_rule {
allowed_headers = "${var.cors_allowed_headers}"
allowed_methods = "${var.cors_allowed_methods}"
allowed_origins = "${var.cors_allowed_origins}"
expose_headers = "${var.cors_expose_headers}"
max_age_seconds = "${var.cors_max_age_seconds}"
allowed_headers = var.cors_allowed_headers
allowed_methods = var.cors_allowed_methods
allowed_origins = var.cors_allowed_origins
expose_headers = var.cors_expose_headers
max_age_seconds = var.cors_max_age_seconds
}
}

// Get ACM cert for use with CloudFront
data "aws_acm_certificate" "cert" {
domain = "${var.cert_domain}"
domain = var.cert_domain
}

/*
* Create CloudFront distribution for SSL support but caching disabled, leave that to Cloudflare
*/
resource "aws_cloudfront_distribution" "hugo" {
count = 1
depends_on = ["aws_s3_bucket.hugo"]
depends_on = [aws_s3_bucket.hugo]

origin {
custom_origin_config {
Expand All @@ -57,20 +57,20 @@ resource "aws_cloudfront_distribution" "hugo" {
// supports S3 redirects with CloudFront
domain_name = "${var.bucket_name}.s3-website-${var.aws_region}.amazonaws.com"

origin_id = "${var.s3_origin_id}"
origin_path = "${var.origin_path}"
origin_id = var.s3_origin_id
origin_path = var.origin_path
}

enabled = true
is_ipv6_enabled = true
default_root_object = "index.html"

aliases = ["${var.aliases}"]
aliases = var.aliases

default_cache_behavior {
allowed_methods = ["GET", "HEAD", "OPTIONS"]
cached_methods = ["GET", "HEAD"]
target_origin_id = "${var.s3_origin_id}"
target_origin_id = var.s3_origin_id

forwarded_values {
query_string = false
Expand All @@ -80,18 +80,18 @@ resource "aws_cloudfront_distribution" "hugo" {
}
}

viewer_protocol_policy = "${var.viewer_protocol_policy}"
viewer_protocol_policy = var.viewer_protocol_policy

// Using CloudFront defaults, tune to liking
min_ttl = "${var.cf_min_ttl}"
default_ttl = "${var.cf_default_ttl}"
max_ttl = "${var.cf_max_ttl}"
min_ttl = var.cf_min_ttl
default_ttl = var.cf_default_ttl
max_ttl = var.cf_max_ttl
}

price_class = "${var.cf_price_class}"
price_class = var.cf_price_class

viewer_certificate {
acm_certificate_arn = "${data.aws_acm_certificate.cert.arn}"
acm_certificate_arn = data.aws_acm_certificate.cert.arn
ssl_support_method = "sni-only"
minimum_protocol_version = "TLSv1"
}
Expand Down
10 changes: 7 additions & 3 deletions outputs.tf
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
output "acm_certificate_arn" {
value = "${data.aws_acm_certificate.cert.arn}"
value = data.aws_acm_certificate.cert.arn
}

output "cloudfront_hostname" {
value = "${aws_cloudfront_distribution.hugo.domain_name}"
value = aws_cloudfront_distribution.hugo[0].domain_name
}

output "cloudfront_zone_id" {
value = aws_cloudfront_distribution.hugo[0].hosted_zone_id
}

output "s3_bucket_arn" {
value = "${aws_s3_bucket.hugo.arn}"
value = aws_s3_bucket.hugo.arn
}
39 changes: 20 additions & 19 deletions vars.tf
Original file line number Diff line number Diff line change
@@ -1,87 +1,87 @@
// Provider vars for authentication
variable "aliases" {
description = "List of hostnames to serve site on. E.g. with and without www"
type = "list"
type = list(string)
}

variable "aws_region" {
type = "string"
type = string
default = "us-east-1"
}

variable "bucket_name" {
description = "Name of bucket to be created in S3. Must be globally unique."
type = "string"
type = string
}

variable "cert_domain" {
description = "Domain name on ACM certificate"
type = "string"
type = string
}

variable "cf_default_ttl" {
description = "CloudFront default TTL for cachine"
type = "string"
type = string
default = "86400"
}

variable "cf_min_ttl" {
description = "CloudFront minimum TTL for caching"
type = "string"
type = string
default = "0"
}

variable "cf_max_ttl" {
description = "CloudFront maximum TTL for caching"
type = "string"
type = string
default = "31536000"
}

variable "cf_price_class" {
description = "CloudFront price class"
type = "string"
type = string
default = "PriceClass_All"
}

variable "cors_allowed_headers" {
description = "List of headers allowed in CORS"
type = "list"
type = list(string)
default = []
}

variable "cors_allowed_methods" {
description = "List of methods allowed in CORS"
type = "list"
type = list(string)
default = ["GET"]
}

variable "cors_allowed_origins" {
description = "List of origins allowed to make CORS requests"
type = "list"
type = list(string)
default = ["https://s3.amazonaws.com"]
}

variable "cors_expose_headers" {
description = "List of headers to expose in CORS response"
type = "list"
type = list(string)
default = []
}

variable "cors_max_age_seconds" {
description = "Specifies time in seconds that browser can cache the response for a preflight request"
type = "string"
type = string
default = 3000
}

variable "origin_path" {
description = "Path in S3 bucket for hosted files, with leading slash"
type = "string"
type = string
default = "/public"
}

variable "routing_rules" {
description = "A json array containing routing rules describing redirect behavior and when redirects are applied"
type = "string"
type = string

default = <<EOF
[{
Expand All @@ -93,21 +93,22 @@ variable "routing_rules" {
}
}]
EOF

}

variable "s3_origin_id" {
description = "Origin ID used in CloudFront"
type = "string"
type = string
default = "hugo-s3-origin"
}

variable "viewer_protocol_policy" {
description = "One of allow-all, https-only, or redirect-to-https"
type = "string"
type = string
default = "redirect-to-https"
}

variable "deployment_user_arn" {
description = "ARN for user who is able to put objects into S3 bucket"
type = "string"
}
type = string
}
4 changes: 4 additions & 0 deletions versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

terraform {
required_version = ">= 0.12"
}

0 comments on commit d22b0a4

Please sign in to comment.