Click here to Skip to main content
15,886,963 members
Articles / General Programming / Architecture

How to Configure AWS HTTP API Gateway for ECS with HTTPS Listener

Rate me:
Please Sign up or sign in to vote.
5.00/5 (6 votes)
7 Oct 2023CPOL3 min read 8.5K   7   4
Use Amazon ECS and API Gateway to implement Microservice
This article explores implementing a Microservice architecture using Amazon ECS and AWS API Gateway, focusing on setting up private integrations between an Application Load Balancer and HTTP APIs, with step-by-step instructions and configuration details.

Introduction

Amazon Elastic Container Service (Amazon ECS) is a comprehensive container orchestration service designed to streamline the deployment, management, and scaling of containerized applications. On the other hand, Amazon API Gateway is an AWS service tailored for creating, publishing, maintaining, monitoring, and securing REST, HTTP, and WebSocket APIs at any scale. In this article, we will explore the process of implementing a Microservice architecture using Amazon ECS in conjunction with AWS API Gateway.

Image 1

Create ECS with Application Load Balancer

By default, when you create a VPC (Virtual Private Cloud) in AWS, it sets up four subnets, two of which are public, and two are private. For security reasons, it’s advisable to deploy your ECS (Elastic Container Service) and load balancer in the private subnets.

First, let’s create an Application Load Balancer (ALB). Since it doesn’t need to be publicly accessible, choose the “Internal” type during creation. Select your VPC and ensure you pick the private subnets. Configure an HTTPS listener, making sure to select the correct SSL certificate. In the HTTPS listener settings, set it to forward traffic to a new target group. Create a new target group, choosing IP addresses as the target type and using the HTTP protocol. Don’t forget to specify a health check path. The key here is that the HTTPS listener forwards traffic to an HTTP target group.

Image 2

Now, when creating an ECS cluster, configure load balancing with the previously created ALB and target group. Ensure that the ECS network security group allows all TCP traffic from the security group associated with the Application Load Balancer.

Image 3

AWS HTTP Gateway

Next, let’s create an AWS HTTP Gateway. AWS API Gateway offers both REST Gateway and HTTP Gateway options, with HTTP Gateway being the faster and more cost-effective choice. To establish private integrations connecting your HTTP API routes to resources in a VPC, such as Application Load Balancers or Amazon ECS container-based applications, you can use VPC links. VPC links facilitate secure connections between API Gateway and your VPC resources. They are reusable across different routes and APIs.

To set this up:

  1. Navigate to the API Gateway in the AWS Management Console.
  2. Select VPC Links and add a VPC Link for HTTP APIs. Choose the VPC and subnets used for your ECS and ALB.

Image 4

Now, proceed to create an HTTP API. At this stage, you don’t need to add integrations; just provide an API name, leaving everything else at default settings. Once the API is created, add a route for Any -> /{proxy+}. Now, go to Integrations, select Attach integrations to routes, and create and attach an integration. Choose Integration Type as Private Resource, select the integration method manually, and opt for ALB/NLB. In the dropdown list, choose the ALB you created earlier. For the listener, select HTTPS:443 since your ALB has only one HTTPS listener. Finally, select the VPC link you created earlier from the VPC link dropdown list. After attaching the integration, an invoke URL will be generated on the $default stage.

Image 5

You can now use this invoke URL as the base URL to test your API, e.g., {invoke URL}/health. If you receive a 400 Bad Request — The plain HTTP request was sent to the HTTPS port, it’s because you forgot to set the secured host in your integration.

Image 6

To fix this issue, go to Integrations -> Manage Integrations -> Edit, and select Advanced Settings. The Secure server name field should not be left blank. It should match the domain of the HTTPS certificate on your ALB (Application Load Balancer). Copy the certificate domain to the Secure Server Name field. Now, try {invoke URL}/health again, and it should work seamlessly.

History

  • 12th September, 2023: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Australia Australia
Fred is a senior software developer who lives in Melbourne, Australia. In 1993, he started Programming using Visual C++, Visual Basic, Java, and Oracle Developer Tools. From 2003, He started with .Net using C#, and then expertise .Net development.

Fred is often working with software projects in different business domains based on different Microsoft Technologies like SQL-Server, C#, VC++, ASP.NET, ASP.Net MVC, WCF,WPF, Silverlight, .Net Core and Angular, although he also did some development works on IBM AS400.

Comments and Discussions

 
QuestionIssue with Secure server name in API gateway Pin
Member 1614220317-Nov-23 0:46
Member 1614220317-Nov-23 0:46 
GeneralMy vote of 5 Pin
Ștefan-Mihai MOGA18-Oct-23 4:06
professionalȘtefan-Mihai MOGA18-Oct-23 4:06 
GeneralMy vote of 5 Pin
Member 121366198-Oct-23 13:09
Member 121366198-Oct-23 13:09 
GeneralMy vote of 5 Pin
Judy Wang 201212-Sep-23 19:37
Judy Wang 201212-Sep-23 19:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.