Skip to content

Commit

Permalink
Upgrade codebase to 0.12 syntax (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
simonrondelez authored Sep 23, 2019
1 parent 8888087 commit 3741d09
Show file tree
Hide file tree
Showing 17 changed files with 214 additions and 111 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
# Compiled files
*.tfstate
*.tfstate.backup
.terraform/
9 changes: 5 additions & 4 deletions r53-alias/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
## Create a Route53 ALIAS record to the Cloudfront website distribution
################################################################################################################
resource "aws_route53_record" "cdn-alias" {
zone_id = "${var.route53_zone_id}"
name = "${var.domain}"
zone_id = var.route53_zone_id
name = var.domain
type = "A"

alias {
name = "${var.target}"
zone_id = "${var.cdn_hosted_zone_id}"
name = var.target
zone_id = var.cdn_hosted_zone_id
evaluate_target_health = false
}
}

1 change: 0 additions & 1 deletion r53-alias/outputs.tf

This file was deleted.

19 changes: 15 additions & 4 deletions r53-alias/variables.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
variable domain {}
variable target {}
variable route53_zone_id {}
variable cdn_hosted_zone_id {}
variable "domain" {
type = string
}

variable "target" {
type = string
}

variable "route53_zone_id" {
type = string
}

variable "cdn_hosted_zone_id" {
type = string
}
4 changes: 4 additions & 0 deletions r53-alias/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

terraform {
required_version = ">= 0.12"
}
7 changes: 4 additions & 3 deletions r53-cname/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
## Create a Route53 CNAME record to the Cloudfront distribution
################################################################################################################
resource "aws_route53_record" "cdn-cname" {
zone_id = "${var.route53_zone_id}"
name = "${var.domain}"
zone_id = var.route53_zone_id
name = var.domain
type = "CNAME"
ttl = "300"
records = ["${var.target}"]
records = [var.target]
}

1 change: 0 additions & 1 deletion r53-cname/outputs.tf

This file was deleted.

14 changes: 11 additions & 3 deletions r53-cname/variables.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
variable domain {}
variable target {}
variable route53_zone_id {}
variable "domain" {
type = string
}

variable "target" {
type = string
}

variable "route53_zone_id" {
type = string
}
4 changes: 4 additions & 0 deletions r53-cname/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

terraform {
required_version = ">= 0.12"
}
80 changes: 47 additions & 33 deletions site-main/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -16,72 +16,78 @@
## certificates must be requested in region us-east-1
################################################################################################################


################################################################################################################
## Configure the bucket and static website hosting
################################################################################################################
data "template_file" "bucket_policy" {
template = "${file("${path.module}/website_bucket_policy.json")}"
template = file("${path.module}/website_bucket_policy.json")

vars {
bucket = "${var.bucket_name}"
secret = "${var.duplicate-content-penalty-secret}"
vars = {
bucket = var.bucket_name
secret = var.duplicate-content-penalty-secret
}
}

resource "aws_s3_bucket" "website_bucket" {
bucket = "${var.bucket_name}"
policy = "${data.template_file.bucket_policy.rendered}"
bucket = var.bucket_name
policy = data.template_file.bucket_policy.rendered

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

// logging {
// target_bucket = "${var.log_bucket}"
// target_prefix = "${var.log_bucket_prefix}"
// }

tags = "${merge("${var.tags}",map("Name", "${var.project}-${var.environment}-${var.domain}", "Environment", "${var.environment}", "Project", "${var.project}"))}"
tags = merge(
var.tags,
{
"Name" = "${var.project}-${var.environment}-${var.domain}"
"Environment" = var.environment
"Project" = var.project
},
)
}

################################################################################################################
## Configure the credentials and access to the bucket for a deployment user
################################################################################################################
data "template_file" "deployer_role_policy_file" {
template = "${file("${path.module}/deployer_role_policy.json")}"
template = file("${path.module}/deployer_role_policy.json")

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

resource "aws_iam_policy" "site_deployer_policy" {
name = "${var.bucket_name}.deployer"
path = "/"
description = "Policy allowing to publish a new version of the website to the S3 bucket"
policy = "${data.template_file.deployer_role_policy_file.rendered}"
policy = data.template_file.deployer_role_policy_file.rendered
}

resource "aws_iam_policy_attachment" "site-deployer-attach-user-policy" {
name = "${var.bucket_name}-deployer-policy-attachment"
users = ["${var.deployer}"]
policy_arn = "${aws_iam_policy.site_deployer_policy.arn}"
users = [var.deployer]
policy_arn = aws_iam_policy.site_deployer_policy.arn
}

################################################################################################################
## Create a Cloudfront distribution for the static website
################################################################################################################
resource "aws_cloudfront_distribution" "website_cdn" {
enabled = true
price_class = "${var.price_class}"
price_class = var.price_class
http_version = "http2"

"origin" {
origin {
origin_id = "origin-bucket-${aws_s3_bucket.website_bucket.id}"
domain_name = "${aws_s3_bucket.website_bucket.website_endpoint}"
domain_name = aws_s3_bucket.website_bucket.website_endpoint

custom_origin_config {
origin_protocol_policy = "http-only"
Expand All @@ -92,56 +98,64 @@ resource "aws_cloudfront_distribution" "website_cdn" {

custom_header {
name = "User-Agent"
value = "${var.duplicate-content-penalty-secret}"
value = var.duplicate-content-penalty-secret
}
}

default_root_object = "${var.default-root-object}"
default_root_object = var.default-root-object

custom_error_response {
error_code = "404"
error_caching_min_ttl = "360"
response_code = "200"
response_page_path = "${var.not-found-response-path}"
response_page_path = var.not-found-response-path
}

"default_cache_behavior" {
default_cache_behavior {
allowed_methods = ["GET", "HEAD", "DELETE", "OPTIONS", "PATCH", "POST", "PUT"]
cached_methods = ["GET", "HEAD"]

"forwarded_values" {
query_string = "${var.forward-query-string}"
forwarded_values {
query_string = var.forward-query-string

cookies {
forward = "none"
}
}

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

min_ttl = "0"
default_ttl = "300" //3600
max_ttl = "1200" //86400
default_ttl = "300" //3600
max_ttl = "1200" //86400
target_origin_id = "origin-bucket-${aws_s3_bucket.website_bucket.id}"

// This redirects any HTTP request to HTTPS. Security first!
viewer_protocol_policy = "redirect-to-https"
compress = true
}

"restrictions" {
"geo_restriction" {
restrictions {
geo_restriction {
restriction_type = "none"
}
}

"viewer_certificate" {
acm_certificate_arn = "${var.acm-certificate-arn}"
viewer_certificate {
acm_certificate_arn = var.acm-certificate-arn
ssl_support_method = "sni-only"
minimum_protocol_version = "TLSv1"
}

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

tags = "${merge("${var.tags}",map("Name", "${var.project}-${var.environment}-${var.domain}", "Environment", "${var.environment}", "Project", "${var.project}"))}"
tags = merge(
var.tags,
{
"Name" = "${var.project}-${var.environment}-${var.domain}"
"Environment" = var.environment
"Project" = var.project
},
)
}

14 changes: 7 additions & 7 deletions site-main/outputs.tf
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
output "website_cdn_hostname" {
value = "${aws_cloudfront_distribution.website_cdn.domain_name}"
value = aws_cloudfront_distribution.website_cdn.domain_name
}

output "website_cdn_id" {
value = "${aws_cloudfront_distribution.website_cdn.id}"
value = aws_cloudfront_distribution.website_cdn.id
}

output "website_cdn_arn" {
value = "${aws_cloudfront_distribution.website_cdn.arn}"
value = aws_cloudfront_distribution.website_cdn.arn
}


output "website_cdn_zone_id" {
value = "${aws_cloudfront_distribution.website_cdn.hosted_zone_id}"
value = aws_cloudfront_distribution.website_cdn.hosted_zone_id
}

output "website_bucket_id" {
value = "${aws_s3_bucket.website_bucket.id}"
value = aws_s3_bucket.website_bucket.id
}

output "website_bucket_arn" {
value = "${aws_s3_bucket.website_bucket.arn}"
value = aws_s3_bucket.website_bucket.arn
}

44 changes: 31 additions & 13 deletions site-main/variables.tf
Original file line number Diff line number Diff line change
@@ -1,53 +1,71 @@
variable region {
variable "region" {
default = "us-east-1"
}

variable project {
variable "project" {
default = "noproject"
}
variable environment {

variable "environment" {
type = string
default = "default"
}

variable domain {}
variable "domain" {
type = string
}

variable bucket_name {
variable "bucket_name" {
type = string
description = "The name of the S3 bucket to create."
}

variable duplicate-content-penalty-secret {}
variable deployer {}
variable acm-certificate-arn {}
variable "duplicate-content-penalty-secret" {
type = string
}

variable "deployer" {
type = string
}

variable "acm-certificate-arn" {
type = string
}

variable routing_rules {
variable "routing_rules" {
type = string
default = ""
}

variable default-root-object {
variable "default-root-object" {
type = string
default = "index.html"
}

variable not-found-response-path {
variable "not-found-response-path" {
type = string
default = "/404.html"
}

variable "tags" {
type = "map"
type = map(string)
description = "Optional Tags"
default = {}
}

variable "trusted_signers" {
type = "list"
type = list(string)
default = []
}

variable "forward-query-string" {
type = bool
description = "Forward the query string to the origin"
default = false
}

variable "price_class" {
type = string
description = "CloudFront price class"
default = "PriceClass_200"
}
4 changes: 4 additions & 0 deletions site-main/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

terraform {
required_version = ">= 0.12"
}
Loading

0 comments on commit 3741d09

Please sign in to comment.