Skip to content

Instantly share code, notes, and snippets.

@sarkis
Last active November 30, 2020 21:00
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save sarkis/27672db85ab89189b7e430929c9376b0 to your computer and use it in GitHub Desktop.
Save sarkis/27672db85ab89189b7e430929c9376b0 to your computer and use it in GitHub Desktop.
Terraform S3 website redirect (http and https) using S3, CloudFront, ACM (Example redirects (http/https)://www.example.com -> https://example.com)
data "aws_route53_zone" "example_com" {
name = "example.com."
private_zone = false
}
resource "aws_acm_certificate" "example_com" {
domain_name = "example.com"
subject_alternative_names = ["www.example.com"]
validation_method = "DNS"
lifecycle {
create_before_destroy = true
}
}
resource "aws_route53_record" "example_com_acm_verification" {
name = "${aws_acm_certificate.example_com.domain_validation_options.0.resource_record_name}"
type = "${aws_acm_certificate.example_com.domain_validation_options.0.resource_record_type}"
zone_id = "${data.aws_route53_zone.example_com.zone_id}"
records = ["${aws_acm_certificate.example_com.domain_validation_options.0.resource_record_value}"]
ttl = "60"
}
resource "aws_route53_record" "www_example_com_acm_verification" {
name = "${aws_acm_certificate.example_com.domain_validation_options.1.resource_record_name}"
type = "${aws_acm_certificate.example_com.domain_validation_options.1.resource_record_type}"
zone_id = "${data.aws_route53_zone.example_com.zone_id}"
records = ["${aws_acm_certificate.example_com.domain_validation_options.1.resource_record_value}"]
ttl = "60"
}
resource "aws_acm_certificate_validation" "example_com" {
certificate_arn = "${aws_acm_certificate.example_com.arn}"
validation_record_fqdns = ["${aws_route53_record.example_com_acm_verification.fqdn}", "${aws_route53_record.www_example_com_acm_verification.fqdn}"]
}
resource "aws_cloudfront_distribution" "www_example_com" {
aliases = ["www.example.com"]
enabled = true
is_ipv6_enabled = true
origin {
domain_name = "${aws_s3_bucket.www_example_com.website_endpoint}"
origin_id = "${aws_s3_bucket.www_example_com.id}-S3-origin"
custom_origin_config {
http_port = "80"
https_port = "443"
origin_protocol_policy = "http-only"
origin_ssl_protocols = ["TLSv1", "TLSv1.1", "TLSv1.2"]
}
}
default_cache_behavior {
allowed_methods = ["DELETE", "GET", "HEAD", "OPTIONS", "PATCH", "POST", "PUT"]
cached_methods = ["GET", "HEAD"]
target_origin_id = "${aws_s3_bucket.www_example_com.id}-S3-origin"
forwarded_values {
query_string = false
cookies {
forward = "none"
}
}
viewer_protocol_policy = "allow-all"
min_ttl = 0
default_ttl = 3600
max_ttl = 86400
}
restrictions {
geo_restriction {
restriction_type = "none"
}
}
viewer_certificate {
acm_certificate_arn = "${aws_acm_certificate_validation.example_com.certificate_arn}"
ssl_support_method = "sni-only"
}
}
resource "aws_s3_bucket" "www_example_com" {
bucket = "www.example.com"
acl = "public-read"
region = "us-east-1"
website {
redirect_all_requests_to = "https://example.com"
}
}
resource "aws_route53_record" "www_example_com" {
zone_id = "${data.aws_route53_zone.example_com.zone_id}"
name = "www"
type = "A"
alias {
name = "${aws_cloudfront_distribution.www_example_com.domain_name}"
zone_id = "${aws_cloudfront_distribution.www_example_com.hosted_zone_id}"
evaluate_target_health = true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment