Adding Path Variables in AWS API Gateway
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
- https://api-whatever/scores/dave-man
- https://api-whatever/scores/sunnyside-smash
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.
The main takeaway here is to use curly brackets to create the variable. For example, I put {game} as my path variable.
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)
Then click on Ingetgration Request.
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
After pressing the checkmark, another box will appear below:
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:
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.
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.
- Working with Routes for HTTP APIs
- How do I configure my API Gateway REST API to pass query string parameters to a backend Lambda function or HTTP endpoint?
- Tranforming API requests and responses
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:
- API Gateway: Code in my game hits an API.
- Lambda: The API has a Lambda integration, which means the serverless code runs everytime the API is hit.
- DynamoDB: Scores are stored in a DynamoDB table. The code inside the Lambda queries the database.
- IAM: If you are new to AWS, get familiar with IAM. You will need to set up access for your Lambda to query the DynamoDB table.
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!
Comments?
Below are links to where this post is shared on each social media
Instead of a comment section on the blog, I invite you to bring the comments to your social media of choice!