2. Upon creation of the function you are greeted with some template code:

3. Add some pre-made data.

For the purposes of this tutorial, the Lambda will include a dictionary with some pre-made data. We can see that we have two cars and two trucks in our fleet, one in each category is en route and the other one on standby.

4. Get the requested category.

Let’s modify the template code to return the information about the requested category. Our function code now looks like this:

Final function code

5. Set up a test event in the Lambda console. Click on Configure Test Events.

We will be using Lambda Proxy Integration in API Gateway which will pass everything to our backend resource (in this case, our Lambda function) as a JSON document including the entire resource path, not just the event body itself. This is why we include “body” key in the test event:

Lambda test event

Let’s try querying for the status of all cars by clicking on the Test button. You should see the following response providing us the status of all vehicles in the category “cars” in our fleet:

Before we move onto setting up the API Gateway, let’s query for a category that does not exist in our data dictionary.

Modify the test event to query for category “trains” instead:

Hit the Test button and Lambda responds with a Python KeyError because “trains” key does not exist in our data dictionary:

We could write application logic that checks if there is a KeyError and handle the issue in our function or we can explore how we can validate what reaches our Lambda function in the first place. Let’s do the latter. (Of course in a serious case you would have exception handling in place in your function as well).

Set up API Gateway