Last active
October 7, 2022 03:06
-
-
Save deckerego/3edb565a51666aea8ddd674ca69a9906 to your computer and use it in GitHub Desktop.
HTTP (only) redirect for alternate domains using S3
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
locals { | |
www_zones = { | |
"www.domainone.egg" = "FEEDFACEBEEFFEED" | |
"www.domaintwo.egg" = "BEEFBEEFFEEDFACE" | |
"www.domainthree.egg" = "FACEFACEFEEDFACE" | |
} | |
} | |
resource "aws_s3_bucket" "redirect-www-bucket" { | |
for_each = local.www_zones | |
bucket = each.key | |
tags = { | |
STAGE = "${terraform.workspace}" | |
} | |
} | |
resource "aws_s3_bucket_acl" "redirect-www-acl" { | |
for_each = aws_s3_bucket.redirect-www-bucket | |
bucket = each.value.id | |
acl = "public-read" | |
} | |
resource "aws_s3_bucket_website_configuration" "redirect-www-siteconfig" { | |
for_each = aws_s3_bucket.redirect-www-bucket | |
bucket = each.value.id | |
redirect_all_requests_to { | |
host_name = "www.actualdomain.egg" | |
} | |
} | |
# The time_sleep is a hack workaround because `each.value.website_domain` | |
# doesn't seem to be defined after the S3 bucket is created - you somtimes | |
# need to wait for it to appear. If you see the error | |
# "The argument "alias.0.name" is required, but no definition was found" | |
# when applying these templates then it is probably becase S3 is taking its time | |
# so here we wait | |
resource "time_sleep" "redirect-www-wait" { | |
depends_on = [ aws_s3_bucket_website_configuration.redirect-www-siteconfig ] | |
create_duration = "10s" | |
} | |
resource "aws_route53_record" "redirect-www-dns-v4" { | |
for_each = aws_s3_bucket.redirect-www-bucket | |
depends_on = [ time_sleep.redirect-www-wait ] | |
zone_id = local.www_zones[each.value.id] | |
name = each.value.id | |
type = "A" | |
alias { | |
name = each.value.website_domain | |
zone_id = each.value.hosted_zone_id | |
evaluate_target_health = false | |
} | |
} | |
resource "aws_route53_record" "redirect-www-dns-v6" { | |
for_each = aws_s3_bucket.redirect-www-bucket | |
depends_on = [ time_sleep.redirect-www-wait ] | |
zone_id = local.www_zones[each.value.id] | |
name = each.value.id | |
type = "AAAA" | |
alias { | |
name = each.value.website_domain | |
zone_id = each.value.hosted_zone_id | |
evaluate_target_health = false | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment