X
Popular Searches

How to Use AWS’s API Gateway as a Frontend for Lambda Functions

API Gateway Hero image

API Gateway is a fully managed service for building, testing, and hosting production APIs. Rather than renting an EC2 server and managing your API server yourself, API Gateway can handle it for you and streamline the whole process.

What Is API Gateway?

API Gateway is essentially a reverse proxy, fetching data from other services and returning it in a structured manner. Rather than running it yourself though, the burden of handling the traffic and infrastructure is passed off to AWS, who can get it done for much cheaper.

This basically allows API Gateway to act as a “front door” for a lot of other AWS services. For example, by connecting it to AWS Lambda, you can build a backend of microservices without using any EC2 servers. The Lambda function can be set up for connection.

API gateway sits in front of other services

API Gateway does a great job as a basic frontend for general purpose HTTP APIs, but it’s also very useful for managing the overall structure and schema of REST APIs. When building REST APIs, you’re able to neatly define all of the routes and methods, and connect them up to any AWS service you wish.

REST API Structure

API GateWay can also be used to manage WebSocket APIs, which are used for quick real-time communication by opening a direct connection from server to client.

How Much Does API Gateway Cost?

For generic HTTP APIs, API Gateway simply costs $1.00 per million requests, after you exceed the first million that come with the Free Tier.

For REST APIs, the price is higher at $3.50 per million requests. Optionally, you can also choose to enable caching for your REST API, which will improve performance at the cost of an hourly fee based on the size of your cache.

For WebSocket APIs, pricing is a bit different. Because they’re intended for short messages from server to client, you’re only charged $1 per billion requests, 1000 is cheap per request as the other two. However, you’re limited to 128 KB payloads, and you’re also charged $0.25 per million connection minutes. If you consistently have many clients connected to the WebSocket API, you’ll be paying for each of them.

One thing to note, though, is that while there’s no specific charge for data transfer, HTTP APIs are metered in 512 KB increments. For example, a single API request that returned a 1.5 MB response would be billed as three API requests. WebSocket APIs are billed in 32 KB increments. This can easily double your API Gateway costs if your payloads are particularly large.

Of course, if you’re connecting to another AWS service, you’ll have to pay all the costs associated with that services (such as charges for Lambda function invocations) as well as data transfer charges for moving data out of AWS.

Setting Up an HTTP API to Connect to Lambda

While REST APIs offer more organizational tools for managing the API itself, they’re a lot more complicated, and they cost quite a bit more to boot. Instead, we’ll use the basic HTTP APIs, which are easier to create and hook up to Lambda.

Choose to build an “HTTP API” from the creation menu. The first thing you’ll have to configure is your integrations; HTTP APIs support HTTP endpoints and Lambda functions. You can add multiple integrations, which can be useful if you want to have a seperate Lambda function handle each route of your API.

add integrations

Next, you’ll configure the routes for the API. These can be put on sub-URLs like /users, and will call different integration targets based on the method a client connects with. For example, GET /messages might return a list of messages, but POST /messages might upload a new message.

add routes

You’ll probably want a way to distinguish between development and production APIs. You’re able to create multiple environments in the form of “Stages,” which will serve this purpose. By default, the $default environment is automatically updated with any changes and serves as the development stage. You’ll probably want to create a “Production” stage that you can use to push changes from $default to.

add stages

After that, your API should be set up and ready for use. Under “Stages,” you’ll find the invocation URL for your API. This is tied to the API deployment stage itself, and will remain static. It should look something like the following:

https://api_id.execute-api.us-east-1.amazonaws.com

If you want to use this with a custom domain name, you’ll have to generate an ACM certificate to link API Gateway securely to your domain, and edit your DNS configuration to point to the Gateway itself. If you’re using Route 53, this process is streamlined quite a bit.

Under the “Authorization” tab, you’ll find settings for configuring your API with JWT authentication. This is currently the only method supported with HTTP APIs.

Anthony Heddings Anthony Heddings
Anthony Heddings is the resident cloud engineer for LifeSavvy Media, a technical writer, programmer, and an expert at Amazon's AWS platform. He's written hundreds of articles for How-To Geek and CloudSavvy IT that have been read millions of times. Read Full Bio »

The above article may contain affiliate links, which help support CloudSavvy IT.