Skip to content

Instantly share code, notes, and snippets.

@jasonforte
Created December 29, 2019 11:25
Show Gist options
  • Select an option

  • Save jasonforte/9c9697e9b5da8ec43082a9bc7ffb213b to your computer and use it in GitHub Desktop.

Select an option

Save jasonforte/9c9697e9b5da8ec43082a9bc7ffb213b to your computer and use it in GitHub Desktop.
S3 Static Site w/ CloudFront - Terraform
hostname = ""
acm_certificate_arn = ""
provider "aws" {
region = "eu-west-1"
}
variable "hostname" {
}
variable "acm_certificate_arn" {
}
data "aws_s3_bucket" "frontend" {
bucket = "${var.hostname}"
}
data "aws_route53_zone" "frontend" {
name = "${var.hostname}"
}
locals {
acm_cert_arn = "${var.acm_certificate_arn}"
}
resource "aws_cloudfront_distribution" "frontend" {
aliases = ["www.${var.hostname}", "${var.hostname}"]
default_root_object = "index.html"
origin {
domain_name = "${data.aws_s3_bucket.frontend.bucket_regional_domain_name}"
origin_id = "S3-Origin"
}
enabled = true
default_cache_behavior {
target_origin_id = "S3-Origin"
allowed_methods = ["GET", "HEAD"]
cached_methods = ["GET", "HEAD"]
viewer_protocol_policy = "redirect-to-https"
forwarded_values {
query_string = false
cookies {
forward = "none"
}
}
}
restrictions {
geo_restriction {
restriction_type = "none"
}
}
viewer_certificate {
acm_certificate_arn = "${local.acm_cert_arn}"
ssl_support_method = "sni-only"
minimum_protocol_version = "TLSv1"
}
}
resource "aws_route53_record" "frontend" {
zone_id = "${data.aws_route53_zone.frontend.zone_id}"
name = "${var.hostname}"
type = "A"
alias {
name = "${aws_cloudfront_distribution.frontend.domain_name}"
zone_id = "${aws_cloudfront_distribution.frontend.hosted_zone_id}"
evaluate_target_health = false
}
}
output "cloudfront" {
value = "${aws_cloudfront_distribution.frontend.id}"
}
output "bucket" {
value = "${data.aws_s3_bucket.frontend.id}"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment