AWS API Gateway Resources
georgeoffley
TL;DR
Amazon API Gateway only allows for one path variable per resource. So these endpoints work:
<url>/user/{id}/update-status
<url>/user/{id}/search-user
These will throw an error since I already have the id
as a path variable, and trying to set another is a Nah dawg.
<url>/user/{id}/update-status
<url>/user/{id}/search-user
<url>/user/{status}
The error is this:
An error occurred: ApiGatewayResourceChecksIdVar - A sibling ({id}) of this resource already has a variable path part -- only one is allowed.
More Context
So today, while charging full speed into a deliverable deadline, I spent an hour tracking down why my deployment to AWS kept failing. So, for some context, I’m making API endpoints for an internal tool at work.
So to work, I wrote the Cloudformation files for the new feature I was adding, only to run into an error with my deployment.
In my case, we had a resource for /user
with a bunch of endpoints under it. Like so:
-
<url>/user/{id}/update-status
-
<url>/user/{id}/search-user
If you notice the
{id}
in those endpoints, those are path variables. These let us pass information into the API call. In the case above, we use them to pass in a user’s ID to know which user to perform an action.
For this new feature I was working on, I needed to add the ability to create users with a specific user-status
; my solution was to make an endpoint where the user-status
was passed in via a path variable. Like so:
<url>/user/{status}
The Problem
API Gateway didn’t like that one bit and threw this error when I deployed:
An error occurred: ApiGatewayResourceChecksIdVar - A sibling ({id}) of this resource already has a variable path part -- only one is allowed.
It turns out API Gateway only allows one path variable per resource, and my user
resource already had the id
path variable, so API Gateway threw an error when trying to set another.
Figuring It Out
This problem was easily solvable since it was a POST
endpoint, and I would send a body of JSON
data to it. So my solution was to add the status to the request payload. I know a little more about API Gateway; hopefully, you do too!
-George