Adding Path Variables in AWS API Gateway

Lou Bagel Logo

Lou Bagel

October 03, 2022

Hero Image with Lou Bagel
AWS API Gateway Path Variable Tutorial

What This Is:

For a longer introduction and further context, check out the context and longer intro at the end.

There is a ton of AWS Docs and info out there, but if you are like me, when you are doing something for the first time, it is much easier to see a quick example of precisely what you are looking for.

This is a quick guide to adding a path variable to your API* in AWS API Gateway.

For example:

https://api-whatever/scores/name-of-the-game

You can see above that the path after scores will be used to filter or search the results to look for scores from a specific game.

This tutorial will show you how to set that up and pass it to AWS Lambda.

*I found this is either easier or only possible with a REST API. (Please reach out to me if it is possible with http)

Tutorial

Overview

Click one to jump down to that section

Create Resource

The first step is kind of obvious, create the resource that will be used as the path variable.

Click the Actions button and select Create Resource.

creating a new child resource in api gateway
Use curly brackets to signify a variable

The main takeaway here is to use curly brackets to create the variable. For example, I put {game} as my path variable.

resources and methods in api gateway
Game path variable, following a game path

For example, the above can be called with:
https://api-address.com/game/name-of-the-game

Integration Request

Much to my chagrin, simply setting up the Path Variable does not actually get this variable to the Lambda. Or at least not in a simple way that I found. Setting up this Integration request will get the variable to the Lambda in the event.

First step is to find the correct screen: Integration Request

Click on the method itself, such as GET, to get to the screen below.

(click actions and add a method, if you haven't already)

api gateway method screen
Click Integration Request

Then click on Ingetgration Request.

REST API Mapping Templates
Click "Add Mapping Template"

Expand Mapping Templates, the last section.

I selected "When there are no templates defined (recommended)", but cannot remember the details behind this.

Click add mapping template

Type application/json

setting up application/json mapping template
type "application/json"; click checkmark

After pressing the checkmark, another box will appear below:

blank mapping template in aws api gateway
new field appears after clicking the checkmark

Here you can define the object to be passed along. Use "$input.params('path_variable_name')" to grab your path variable.

Of course, replace the red text with the name of your path variable.

Here is an example of the object I am passing to Lambda:

aws api gateway mapping template for high scores
object that will get passed to Lambda

Very simple. At this time this is all I need.

Receiving in Lambda

Now that the API is set up to pass along your object, you can check in your Lambda Function.

The event should be this object, if following the default setup.

exports.handler equals async event const game equals game.event
accessing the game in Lambda

The next steps may of course vary by what you are doing. For me, I'm using this game to query DynamoDB so I only get results for the specific game.

References

I didn't figure this out on my own, of course. Here are a few references I used to get this set up correctly.

Hopefully my tutorial here is simpler and clearer than the above. Feel free to send me any other relevant docs.

Context and Longer Intro

Lately, I've been using APIs more often. Some for work and some for personal. One example is for a high scores table, which is currently implemented in Sunnyside Smash.

Sunnyside Smash is a really tiny game that I don't expect will be played often. It would be kind of silly to set up a high scores table only for it. So I decided to set up my high scores table that can be used for any of my games.

Each entry will of course have which game the score refers to. Therefore when retrieving scores, a specific game will need to be queried.

I don't want to have to add a new resource to the API everytime I add a game. One method to accomplish this could be query parameters, but I find those messy, or just annoying, to work with.

Querying something like https://api.com/game/dave-man seems way cleaner.

Therefore a Path Variable is required to be set up.

AWS Workflow

This tutorial is specifically for the path variables. For context though, here is how I have everything set up:

Adding scores works in the exact same flow but with a different endpoint.

Feedback or Questions?

I'm no expert here. Honestly, my main reason for making this is that I'm sure I will forget some of the steps in a few months. This is the easiest place for me to find my notes later!

Feel free to reach out to me on social media if any questions, comments, or I have anything innacurate here. Thanks!