Intro to API Gateway

Weâre halfway through and we have already discussed so much! Today, weâre going to start tying our knowledge together so that we can create practical applications.
Kicking things off, weâre going to talk about API Gateway.
API Gateway + Need to Know Terms
This service allows us to quickly create endpoints for us to retrieve data.
Much like the post on S3, API Gateway is a service that can help quite a bit once you get up and running. However, in practical terms, weâll be focusing on how we can quickly use it in our frontend applications.
API Gateway comes in 3 flavors: REST, WebSocket, and the more newly announced HTTP. As usual, now is a pretty good time to knock out some definitions.
Method: The action that should be applied when interacting with your data endpoint ie) PUT, POST, GET, DELETE
Resource: A user-named item regarding your data. Ie, if you are creating an API about pets, then /pets is a likely resource. Each resource has one or more methods attached to it.
REST Endpoint: A URL that will perform This allows full customization over how we would like to create our APIs.
WebSocket Endpoint: Creates an open connection to allow for near-real-time communication.
đĽProtip: AWS has better options if youâre considering WebSockets with API Gateway.
HTTP Endpoint: Slightly less flexible than REST Endpoints, while offering 40% of the cost savings and much of main functionality.
Stage: An environment where your API lives. Typically used to keep separate production and development environments.
đď¸ Creating our API
đď¸ This section assumes that you have completed the previous dayâs task of creating a pet Lambda. If not, refer back to that email before continuing. However, if you just need a quick refresher, we created and deployed the following lambda function:

Log into your account and search for API Gateway from the top search bar.
Next, follow the steps below:
- Under HTTP API, select âBuildâ
- Select âAdd Integrationâ, then select Lambda
- Select the region you used to create your Lambda function
- Click in the âLambda Functionâ box and select your lambda
- In the âAPI nameâ field, name your API pets-api
- When you're done, select âNextâ
Almost done!
On the next page, weâll be configuring our API endpoint. Go ahead and follow these steps:
- For the Method field, change it to GET
- For the Resource Path, change that to /pets
- Click Next
Weâre not going to configure another stage, so go ahead and click next.
If all went well, your screen should look just like mine in the screenshot below

đ Congratulations! You just created an API endpoint on AWS without writing any code!
Testing your endpoint
With your API selected, you should see an âInvoke URLâ. If you were hasty and clicked it, you already know it doesnât work đ
Thatâs because we didnât assign anything to the root or / resource. Our resource is /pets.
Copy the Invoke URL paste it in your browserâs search bar, ensuring that you appends /pets to it. You should now see your list of pets!
If youâre just wanting to use this endpoint on your server, then youâre all done! However, chances are you want to use this in your frontend applications too. Unfortunately, if you try that now, youâll be greeted by a friend that weâve all met:
CORS

Itâs saying âhey, server to server is fine, but server to the browser isnât configuredâ.
Letâs fix that.
Back in the AWS Console:
- Select the CORS tab on the left-hand side (under Develop)
- Select the Configure button Under Access-Control-Allow-Origin enter an asterisk
- Click Add, then Save
đď¸ Access-Control-Allow-Origin with an asterisk as the value allows anyone to call our endpoint. In many scenarios, youâll want to lock that down to just your websiteâs domain.
đ Congratulations! You just enabled CORS for your publicly available API endpoint. Feel free to test it out in a Codesandbox!
Pricing
API Gateway doesnât charge for the first one million API calls. In my region (and most others) pricing after the free amount is $1.00 for every million requests.
Not bad at all!
https://aws.amazon.com/api-gateway/pricing/
Over the course of a few posts, we have already covered a lot! Next time, weâll learn how to persist data to a NoSQL database by taking a look at DynamoDB.
See ya then!






