sirrus233 / twilight-imperium-helper

Game assistant for Twilight Imperium
1 stars 0 forks source link

Redirect from www subdomain #23

Closed sirrus233 closed 3 months ago

sirrus233 commented 3 months ago

It's considered a best-practice to only have one canonical name for your website. This PR sets up an automatic redirect from www.twilightimperiumtools.bradleysherman.net to twilightimperiumtools.bradleysherman.net.

The redirect is implemented with an AWS feature called "Lambda@Edge", which is just a fancy way of saying "a lambda function that runs when the CloudFront endpoint is hit". This lambda function can modify the request before passing it on for further handling.

Lambda@Edge has some pretty severe limitations:

Since we don't have much testing infrastructure set up - I just deployed this to make sure it works. And it does! But if you think we need any changes, we can easily do that and redeploy.

***It's hard to have a Lambda@Edge function written in typescript, but not impossible. There's a little more information here: https://github.com/aws/aws-cdk/issues/6259

Here's the general deal (took a while to learn all this, so I'm writing it down!):

Lambda's NodeJS runtime doesn't support typescript in general. But if you want to write your functions that way, what you can do is write them in typescript, then transpile to standard JS before deploying to AWS. To avoid a multi-step build/deploy process, you'd ideally do the transpilation as part of your deployment -- and the good news is that CDK supports that! There is a NodeJSFunction construct that can accept a typescript file, then at deploy-time uses a docker container to transpile and deploy. Pretty nifty stuff.

Unfortunately, here you hit the Lambda@Edge limitation. The function needs to be in us-east-1, and that's not the region of our stack. That means, in order to use the NodeJSFunction construct, we'd have to spin up another stack in the us-east-1 region, define the function resource there, and then use a cross-stack dependency to import the resource from one stack to the other. This can get a little painful.

Enter: the EdgeFunction construct! This is a construct that exists solely to make deploying Lambda@Edge functions easier. You can define this construct in a stack for any region (as long as that region is explicitly specified), and it gets created in us-east-1 for you automagically! I think CloudFormation may be creating a separate regionalized stack under the hood, but I'm not 100% sure about that.

So why isn't the problem solved?? Because EdgeFunction lacks the automatic transpiling of NodeJSFunction!!!. It is truly maddening.

TL;DR Amazon provides constructs that allow for

...and you are allowed to have one or the other of these nice things, but not both. RIP :coffin:

chelseaSchmidt commented 3 months ago

It's considered a best-practice to only have one canonical name for your website. This PR sets up an automatic redirect from www.twilightimperiumtools.bradleysherman.net to twilightimperiumtools.bradleysherman.net.

Awesome 🚀 🎉 tested it out, works like a charm!

The redirect is implemented with an AWS feature called "Lambda@Edge", which is just a fancy way of saying "a lambda function that runs when the CloudFront endpoint is hit". This lambda function can modify the request before passing it on for further handling.

Checked out the AWS resource created in our stack, very cool!

No custom runtimes (I had a nifty haskell implementation 😩 ), only NodeJS or Python.

😭

Since we don't have much testing infrastructure set up - I just deployed this to make sure it works. And it does! But if you think we need any changes, we can easily do that and redeploy.

Thank you for deploying! Nice to be able to see it in action 💯

Here's the general deal (took a while to learn all this, so I'm writing it down!):

Thank you - one day I'm probably going to need this and will come straight back here for reference 🙏

I think CloudFormation may be creating a separate regionalized stack under the hood, but I'm not 100% sure about that.

Totally correct, looked in the console and there's a new CloudFormation stack that only shows up in us-east-1 now

So why isn't the problem solved?? Because EdgeFunction lacks the automatic transpiling of NodeJSFunction!!!. It is truly maddening. TL;DR Amazon provides constructs that allow for Automatic transpiling from typescript -> javascript during deploy Automatic cross-region referencing of the function resource ...and you are allowed to have one or the other of these nice things, but not both. RIP ⚰️

Heartbreaking but this did make me laugh! Awesome sleuthing dude