AWS Lambda function to redirect a domain to
its subdomain or parent domain.
Meant to be used with Amazon API Gateway.
Add the function code to AWS Lambda with the following configuration options:
| Key | Value |
|---|---|
| Runtime | Node.js 6.10 |
| Handler | index.handler |
| Role | AWSLambdaBasicExecutionRole |
| Memory | 128 (MB) |
| Timeout | 3 sec |
Set the following optional environment variables for the Lambda function:
| Key | Value |
|---|---|
| label | The DNS label of the subdomain, defaults to "www". |
| status | The status code of the response, defaults to 302. |
| scheme | The scheme of the redirect URL, defaults to "https". |
| hsts | The value for the HSTS header, by default empty. |
| type | The value for the Content-Type header, defaults to "text/html". |
Select the US East (N. Virginia) region, request a certificate for your custom
domain with both the wildcard and naked domain - e.g. *.example.org and
example.org - and confirm the email request.
- Create a new API in Amazon API Gateway
with the name
subdomain-redirect. - Select
Resources => Actions => Create Methodand chooseANYas HTTP method. EnableUse Lambda Proxy integration, choose your lambda region, enter the function name (subdomain-redirect) and save the method. - Select
Resources => Actions => Create Resource, enableConfigure as proxy resourceand click onCreate Resource. Choose your lambda region, enter the function name (subdomain-redirect) and save the method. - Next, deploy the API via
Resources => Actions => Deploy APIwithprodas new stage name. - Create a custom domain name. Enter the naked domain (e.g.
example.org) if you want to redirect to thewwwsubdomain. Enter the subdomain (e.g.www.example.org) if you want to redirect to the parent domain. Add a base path mapping to thesubdomain-redirectAPI with thePathempty and the stage set toprodand save the custom domain.
In Amazon Route 53 in your hosted zone,
create a new record for your custom domain. Set the Type to A, Alias to
Yes and set the Alias Target to the Target Domain Name from the custom
domain setup in API Gateway.
Released under the MIT license.