Created
December 29, 2019 11:25
-
-
Save jasonforte/9c9697e9b5da8ec43082a9bc7ffb213b to your computer and use it in GitHub Desktop.
S3 Static Site w/ CloudFront - Terraform
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| hostname = "" | |
| acm_certificate_arn = "" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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