All Projects → ModusCreateOrg → lambda-redirector

ModusCreateOrg / lambda-redirector

Licence: MIT License
Redirect an entire website using AWS Lambda

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to lambda-redirector

shim
HTTP Handler shim for Go projects running on AWS Lambda
Stars: ✭ 64 (+204.76%)
Mutual labels:  lambda, api-gateway
super-serverless-sample
Backend serverless que simula o sistema de votação do BBB
Stars: ✭ 30 (+42.86%)
Mutual labels:  lambda, api-gateway
Zappa
Serverless Python
Stars: ✭ 11,859 (+56371.43%)
Mutual labels:  lambda, api-gateway
Hello Lambda
🔥 An example of a Python (AWS) Lambda exposed with API Gateway, configured with Terraform.
Stars: ✭ 114 (+442.86%)
Mutual labels:  lambda, api-gateway
Hands-On-Serverless-Applications-with-Go
Hands-On Serverless Applications with Go, published by Packt.
Stars: ✭ 92 (+338.1%)
Mutual labels:  lambda, api-gateway
Hexaville
The modern serverless web application engine and framework for Swift
Stars: ✭ 123 (+485.71%)
Mutual labels:  lambda, api-gateway
Serverless Sinatra Sample
Demo code for running Ruby Sinatra on AWS Lambda
Stars: ✭ 195 (+828.57%)
Mutual labels:  lambda, api-gateway
Serverless Es Logs
A Serverless plugin to transport logs to ElasticSearch
Stars: ✭ 51 (+142.86%)
Mutual labels:  lambda, api-gateway
yake
A Rake-like DSL for writing AWS Lambda handlers
Stars: ✭ 146 (+595.24%)
Mutual labels:  lambda, api-gateway
Apilogs
Easy logging and debugging for Amazon API Gateway and AWS Lambda Serverless APIs
Stars: ✭ 216 (+928.57%)
Mutual labels:  lambda, api-gateway
Serverless Sharp
Serverless image optimizer for S3, Lambda, and Cloudfront
Stars: ✭ 102 (+385.71%)
Mutual labels:  lambda, api-gateway
httpz
Fat-free hardenable opportunistic encryption for Firefox
Stars: ✭ 60 (+185.71%)
Mutual labels:  https, redirect
Aws Cli Cheatsheet
☁️ AWS CLI + JQ = Make life easier
Stars: ✭ 94 (+347.62%)
Mutual labels:  lambda, api-gateway
Serverless Next.js
⚡ Deploy your Next.js apps on AWS Lambda@Edge via Serverless Components
Stars: ✭ 2,977 (+14076.19%)
Mutual labels:  lambda, api-gateway
Up
Up focuses on deploying "vanilla" HTTP servers so there's nothing new to learn, just develop with your favorite existing frameworks such as Express, Koa, Django, Golang net/http or others.
Stars: ✭ 8,439 (+40085.71%)
Mutual labels:  lambda, api-gateway
Aws Lambda Fastify
Insipired by aws-serverless-express to work with Fastify with inject functionality.
Stars: ✭ 190 (+804.76%)
Mutual labels:  lambda, api-gateway
Terraform Nextjs Plugin
A plugin to generate terraform configuration for Nextjs 8 and 9
Stars: ✭ 41 (+95.24%)
Mutual labels:  lambda, api-gateway
Corgi
AWS Lambda / API Gateway native, fast and simple web framework
Stars: ✭ 44 (+109.52%)
Mutual labels:  lambda, api-gateway
Aws Mobile React Native Starter
AWS Mobile React Native Starter App https://aws.amazon.com/mobile
Stars: ✭ 2,247 (+10600%)
Mutual labels:  lambda, api-gateway
serverless-certificate-creator
serverless plugin to manage the certificate of your lambdas custom domain (API Gateway=
Stars: ✭ 33 (+57.14%)
Mutual labels:  lambda, api-gateway

Lambda Redirector

FOSSA Status

Redirect an entire website using AWS Lambda and API Gateway, packaged with CloudFormation and deployed with Serverless Application Model (SAM).

Table of Contents

About

The Problem

If you've migrated from one domain to another and you want to redirect all incoming requests from the old domain to the new one, you'd typically do the following:

  1. Create a webserver with either Apache or NGINX that listens to incoming requests.
  2. Rewrite all requests to the new domain while passing appropriate headers (301/302 HTTP status with a Location header).

This works fine except you have to run a whole webserver just to manage this.

The Solution

We could use API Gateway as an entrypoint for requests and map those requests to our Lambda function. This way, we utilize a resource only when it's needed. Lambda is ideal for this use case since this is not a long lived process and it's also very specialized and the behaviour well defined.

We've used CloudFormation to define our Lambda functions and in the function definition, we've specified that requests coming into API Gateway would act as a trigger for our Lambda functions. We've also specified that GET requests are handled by one function and all other HTTP methods are handled by another.

Finally, we use AWS SAM to package and deploy our code.

Usage

Assumptions:

  • MyRedirectorBucket is the name of the bucket we'll be uploading our packaged Lambda code to.
  • Redirector is the name of our CloudFormation stack.
  • http://example.org/ is the new domain we are redirecting to.

Setting Up

Make sure our S3 bucket exists, if not, create it

aws s3 ls s3://MyRedirectorBucket/ > /dev/null 2>&1 || aws s3 mb s3://MyRedirectorBucket

Validate our CloudFormation template to make sure there are no errors

aws cloudformation validate-template --template-body file://./app_spec.yml

Package

Package and upload local artifacts to S3

aws cloudformation package \
    --template-file app_spec.yml \
    --output-template-file packaged.yml \
    --s3-bucket MyRedirectorBucket

Deploy

Deploys the CloudFormation template by creating and executing a changeset.

aws cloudformation deploy \
    --template-file packaged.yml \
    --stack-name Redirector \
    --capabilities CAPABILITY_IAM \
    --parameter-overrides \
        NewDomain=http://example.org \
        HTTPResponse=301

Note: Ensure that NewDomain is prefixed with either http:// or https:// or you will get a chain of redirects to a never-ending path on the source domain. It will be amusing but will not meet your goal.

Note: This redirector has a special feature where if you specify either a query parameter or a non-slash-terminated string in the NewDomain URL (e.g. ``https://www.example.com/snafu` or `https://www.example.com/some/path?foo=bar") it will disregard the path and any query parameters passed in by the user and will redirect strictly to the NewDomain URL.

Parameters

Parameters Type Description
HTTPResponse Number The HTTP response to redirect with. Only 301 and 302 are allowed.
NewDomain String New domain to redirect to.

Deleting a Stack

While testing, you may need to delete a stack

aws cloudformation delete-stack --stack-name Redirector

Configuring target domains for redirection

In order to fully configure a target domain for redirection, first establish a Redirector API Gateway through the CloudFormation Stack as described above. Then add a Custom Domain Mapping through the AWS Console. You can add a Custom Domain Name through the console, give it a DNS name, tell it to use an Edge Optimized endpoint configuration, and give it a valid ACM certificate for the domain you are targeting. This can be a wildcard certificate, which might make things simpler for you if you are configuring multiple domains. Then Edit the custom domain name and do an Add mapping operation to add a Base Path Mapping to map / to the Redirector Prod stage.

Once this is configured, look for the Target Domain Name under the Custom Domain Name / Endpoint Configuration in the console and chnage that to the DNS name you want to redirect from.

Caveat:

The first version of this software had the same issue as SAM bug #191. If you look at the Stages in your API Gateway, you may find a stage named Stage. As of 2019-11-15 this issue is resolved and new deployments of lambda-redirector should not have a Stage stage defined.

Contributing:

See: http://www.contribution-guide.org/

Legal

Copyright 2018 by Modus Create, Inc.

This software is licensed under the permissive MIT License.

License

FOSSA Status

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].