Exposing an AWS Application Load Balancer Endpoint via a Custom Domain Name with API Gateway

Samuel Ajisafe - Aug 13 - - Dev Community

Introduction:

Are you a DevOps or Cloud Engineer looking to make your Application Load Balancer (ALB) with multiple dynamic endpoints accessible to the public without directly exposing the endpoints themselves? This guide will walk you through the process of creating a public-facing ALB endpoint that is accessible via a custom domain name using AWS API Gateway, with Namecheap as your DNS manager.

Scenario:
Imagine you have an existing service architecture that includes API Gateway, AWS Lambda, AWS Rekognition, and DynamoDB. Your service is working well, but now there's a request to replicate the service on an EC2 instance with dynamic endpoints managed by an ALB. You want to retain your current serverless setup while integrating the new EC2-based setup, either as a failover option or in an active-active configuration.

Since your current environment uses API Gateway to expose the endpoint, it's logical to place the ALB behind the API Gateway. This ensures that you don't need to change the URL or subdomain, providing a seamless transition.

If you have a similar scenario and need a solution to expose both serverless and compute-based endpoints through a unified URL, this guide is for you.

Prerequisites:
An AWS Account
IAM Administrator Access
Amazon ALB
API Gateway
DNS Manager (Namecheap or any other Domain Name Manager)

Assumptions:
You have already created an EC2 instance and deployed your services there, listening on port 8080.
You are load balancing traffic to the EC2 instances on port 443, with the ALB routing traffic to different endpoints, such as /api/v1, /test/v2, etc.

Step 1: Build an API Gateway Endpoint
Create the API:

Open the API Gateway console.
Choose Create API and select REST.
Choose New API and name it (e.g., Invocation-API).
Select Regional as the endpoint type and create the API.

Link on how to create API Gateway on AWS

Create a Resource:

In the Resources section, choose Create Resource.
Turn on the Proxy resource toggle.
In the Resource Path, leave it as /, and for the Resource name, use {matches+}.
Enable CORS (Cross-Origin Resource Sharing).
Select the created resource.

Image description

Configure the Integration Request:

Select the ANY method and click on Edit Integration Request.
Under Integration Type, select HTTP for the ALB endpoint type.
Choose the appropriate HTTP method (e.g., ANY).
For the Endpoint URL, enter the URL of your ALB or custom domain name, followed by the listening port, e.g., https://invocation-api.example.com:443/.
Leave other configurations as default and click Save.

Image description

Deploy the API:

Select the created resource /{matches+}.
Click on Deploy API.
Set a new stage if one doesn’t exist.
Provide the Stage Name, then click Deploy.

Image description

Image description

You can now test the setup using Postman or any other API testing tool. Copy the invoke URL, for example: https://crvnerf201.execute-api.us-east-2.amazonaws.com/test. You can now append any of your dynamic API endpoints from the ALB to this base URL for testing and production use.

Create a Custom Domain Name in API Gateway

Link to create Custom Domain Name in API Gateway

Cloud #AWS #DevOps #SRE #AI #API #Automation

. . . . . . . . . . . . . . . .
Terabox Video Player