Routing traffic between domains is a common requirement when managing multiple web services, consolidating resources, or transitioning to new infrastructure. Amazon Web Services (AWS) offers a powerful solution through API Gateway, enabling seamless redirection or proxying of traffic from one domain to another.

This guide outlines the steps to route traffic from one domain (e.g., www.oldsite.com) to another (e.g., www.newsite.com) using AWS API Gateway and Lambda, providing a secure, scalable, and serverless approach.

Step-by-Step Guide to Domain Redirection with AWS API Gateway

1. Set Up a New API in API Gateway

  • Log into the AWS Management Console. 
  • Navigate to API Gateway and create a new HTTP API or REST API. 
  • Assign a meaningful name, such as DomainRedirectAPI. 

2. Create a Lambda Function for Redirection (Optional but Recommended)

While API Gateway can proxy requests directly, using a Lambda function gives more control over the response (e.g., returning a 301/302 redirect).

Sample Python Lambda Function:

python

def lambda_handler(event, context):

    return {

        “statusCode”: 301,

        “headers”: {

            “Location”: “https://www.newsite.com”

        },

        “body”: “”

    }

  • Deploy the Lambda function. 
  • Ensure the Lambda execution role has the required permissions. 

3. Connect the API Gateway to Lambda

  • In your API configuration, create a new route (e.g., ANY /{proxy+}). 
  • Set the integration target to the Lambda function created above. 
  • Deploy the API to a new or existing stage (e.g., prod). 

4. Configure a Custom Domain Name

  • In the API Gateway console, go to Custom Domain Names. 
  • Add your old domain (e.g., www.oldsite.com). 
  • Configure an API mapping to link the domain to your API and stage. 
  • Upload or configure an SSL/TLS certificate using AWS Certificate Manager (ACM). 

5. Update DNS Settings

  • In your domain registrar or Route 53, point the old domain’s DNS records (e.g., A or CNAME) to the CloudFront distribution associated with your API Gateway custom domain. 

Benefits of Using AWS API Gateway for Domain Redirection

  • Serverless architecture reduces operational overhead. 
  • Custom domain management is built-in and scalable. 
  • HTTPS support with ACM integration ensures secure redirects. 
  • Fine-grained control using Lambda for redirect logic (conditional redirects, logging, etc.). 

Conclusion

Using AWS API Gateway to route traffic between domains is a robust and scalable solution for web developers and cloud architects. Whether managing domain migrations, branding changes, or service consolidations, this method ensures high availability and performance with minimal infrastructure maintenance.