riboseinc / terraform-aws-s3-cloudfront-website

Terraform module for creating a static S3 website with CloudFront with an SSL certificate (e.g., from ACM)
Apache License 2.0
74 stars 40 forks source link

Redirect a path? #18

Closed colindean closed 5 years ago

colindean commented 5 years ago

I know that this TF code can handle redirects at the domain level, but is there a way to handle redirects at the path level? E.g. I want https://example.com/foo to 301 redirect to https://example.com/bar.

ronaldtse commented 5 years ago

@colindean this is something I attempted previously too.

Path redirects are performed at the S3 bucket level, which requires the routing_rules to be provided directly to the aws_s3_bucket resource. (https://www.terraform.io/docs/providers/aws/r/s3_bucket.html)

Eventually I opted for a file-level direct (using Jekyll) because the redirects are to be made by those who manage the website content, not ones who administer the AWS resources.

Would you deem it useful if this module exposes the routing_rules variable for this purpose?

colindean commented 5 years ago

Yes, that would be useful!

ronaldtse commented 5 years ago

@colindean sorry -- darn -- it's actually already supported. Can't rely on my memory anymore.

variable routing_rules {
  type        = "string"
  description = "Routing rules for the S3 bucket"
  default     = ""
}
ronaldtse commented 5 years ago

So you'll want something like:

module "main" {
  source = "github.com/riboseinc/terraform-aws-s3-cloudfront-website"
  ...

  routing_rules = <<EOF [{
      "Condition": {
          "KeyPrefixEquals": "myprefix/"
      },
      "Redirect": {
          "HostName" : "www.example.com",
          "HttpRedirectCode" : "302",
          "Protocol" : "https"
      }
  }]
  EOF
}
lrhazi commented 5 years ago

sorry to re-open this for a question. When I try this I do get a redirect, but to the DNS name of the s3 bucket, not the cdn name. Is there way to do that?

scriptmgmt-2 :: ~/aws/tmp » curl -i https://test4.catholic.edu/foo
HTTP/1.1 301 Moved Permanently
Content-Length: 0
Connection: keep-alive
Date: Thu, 30 May 2019 18:08:54 GMT
Location: http://test4.catholic.edu.s3-website-us-east-1.amazonaws.com/bar.html
Server: AmazonS3
Age: 16
X-Cache: Hit from cloudfront
Via: 1.1 9b097dfab92228268a37145aac5629c1.cloudfront.net (CloudFront)
X-Amz-Cf-Id: bmm87A-08ina2Hl_kcFqSylCXC-Qp6sJ8WYhlpE3ouf5JoVFjZ7PXg==

scriptmgmt-2 :: ~/aws/tmp »
lrhazi commented 5 years ago

sorry, I should clarify that am using this:

routing_rules = <<EOF
    [
        {
            "Condition": {
                "KeyPrefixEquals": "foo"
            },
            "Redirect": {
                "ReplaceKeyWith": "bar.html"
            }
        }
    ]
EOF
lrhazi commented 5 years ago

Also, wondering if S3 routing could just do a "rewrite" instead of a redirect... is that possible ? Thanks a lot!

lrhazi commented 5 years ago

and answering my own first question, this seem to do it:

routing_rules = <<EOF
    [
        {
            "Condition": {
                "KeyPrefixEquals": "foo"
            },
            "Redirect": {
                "HostName": "${var.fqdn}",
                "HttpRedirectCode": "302",
                "Protocol": "https",
                "ReplaceKeyWith": "bar.html"
            }
        }
    ]
EOF

For my second question, I think S3 does not support "rewrite", just actual http redirect. which is too bad.