Using Amazon API Gateway with Lambda
Tags: aws, python, api, beginners
Published: 2022-02-10 02:12:19

## Table Of Contents
* [Introduction](#introduction)
* [Amazon API Gateway](#amazon_api_gateway)
* [Setting up an API](#setting_up_an_api)
* [Adding the Trigger](#adding_the_trigger)
* [Setting the Methods](#setting_the_methods)
* [Conclusion](#conclusion)

### Introductions <a name="Introduction"></a>

I follow this movie podcast where they review and talk about well movies. It’s a fantastic podcast with a diverse set of people who’ve worked in the industry a long time, and I enjoy their opinions. At the end of each episode, they do a “Staff Pick” where they pick out a movie that shares themes with the movie they just reviewed or movies they think people should see. As I don’t have time to watch all the movies they suggest every week, I need to keep a running list of these films. They offer some good stuff.

<!--more-->

This presented a good opportunity to get some practice using React. I could build a tiny site cataloging all the suggested films. I have no React knowledge, so I started by creating a database to store their suggested films to stick with the iterative approach. Now I need to set up something to query the database and get back a list of the movies. That is where Amazon API Gateway and Lambda come in.

### Amazon API Gateway <a name="amazon_api_gateway"></a>

[I’ve covered AWS Lambda before](https://georgeoffley.com/blog/creating-a-twitter-bot-using-aws-lambda-and-go.html), so I won’t go into it much. Amazon API Gateway, however, is an AWS service that allows you to quickly spin up API endpoints for use in your web applications. When you need to request data for your website, an API endpoint is what you’ll use. An *API endpoint* is a URL that your application can reach out to request some data. For example, [this API endpoint](https://ghibliapi.herokuapp.com/films/58611129-2dbc-4a81-a72f-77ddfc1b1b49) lets me look up Studio Ghibli movies. 

These can scale like crazy, and API Gateway lets you manage, monitor, and add endpoints to your service quickly. For this project I am spinning up, we only need to return a list of the movies in the table. So let’s begin.

### Setting up an API <a name="setting_up_an_api"></a>

The first thing is setting up something to process the request once our service reaches out via an API endpoint. AWS Lambda is what we’ll use to create the process to query our database and return all our stuff. 
- So we go into the AWS Console and create our Lambda. We’re returning all our items with a simple query, so it’s an easy setup. Here’s the code:

```python
import boto3
import os

def lambda_handler(event, context):
    client = boto3.client(
        'dynamodb',
        aws_access_key_id=os.environ.get('KEY'),
        aws_secret_access_key=os.environ.get('SECRET')
        )

    def get_all_items():
        
        response = client.scan(
            TableName='MaxFilmStaffPicks',
        )

        return response
        
        
    return get_all_items()
```

### Adding the Trigger <a name="adding_the_trigger"></a>

- After we have the Lambda function up and running, we need to create something to initiate it, like a trigger.
- You can see a button to *Add trigger* at the top of the Lambda menu. You can click that and select *API Gateway* in the proceeding menu.

![Trigger](https://georgeoffley-blog-images.s3.amazonaws.com/2022-02-09-using-api-gateway-with-lambda/trigger.png)

- Then you can hit *Create API*, which brings up the options for APIs. In this case, I’m making a *REST API*, so I select that. 
- For security, I pick *Open*, as this is just a practice app. You can see what I chose below.

![Create API Screen](https://georgeoffley-blog-images.s3.amazonaws.com/2022-02-09-using-api-gateway-with-lambda/create_api_screen.png)

### Setting the Methods <a name="setting_the_methods"></a>

- I finish off by going into the API Gateway menu to check out the newly created endpoint. 
- After that, you’ll be greeted by a screen with all our methods. The default created method is the *ANY* method; I want this to be a [*GET*](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/GET) method. The difference between HTTP messages is a bit out of scope, [but you should know them](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods). We are using this endpoint only to request information for our application, so we stick to GET. 
- We’ll select the ANY method, click the *Action* menu above it, and delete the method.
- Then we go back to the same menu and click Create Method. 
- From the dropdown, we select GET. 
- Following this, we enter the name of the Lambda function this is for, and we’re done.

![Method Creation](https://georgeoffley-blog-images.s3.amazonaws.com/2022-02-09-using-api-gateway-with-lambda/method_creation.png)

Very last step is to deploy the API. 

- We click the same menu and click *Deploy API*.
- You’ll then be asked which *Stage* you want to deploy in. When creating APIs, you can create many stages for your APIs, which will affect how they are used—for example, setting up a dev stage or creating different versions of your API. Whatever stage you set will reflect in the endpoint. See below.

![Stage URL](https://georgeoffley-blog-images.s3.amazonaws.com/2022-02-09-using-api-gateway-with-lambda/stage_url.png)

- For this instance, it is one API endpoint, in one app, for one specific purpose. I just used the default. 
- Now we’re done. We test the endpoint, and we get data!

![Data!](https://georgeoffley-blog-images.s3.amazonaws.com/2022-02-09-using-api-gateway-with-lambda/data.png)

### Conclusion <a name="conclusion"></a>

Now I have a working API for grabbing my data. React confuses me, as I thought I’d be able just to pull data like I would a Python app. However, I am sure I will learn much more as I continue. And now you also have some knowledge of setting up endpoints in Amazon API Gateway.