Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
May 3, 2021 07:49 pm GMT

Intro to API Gateway

Were halfway through and we have already discussed so much! Today, were going to start tying our knowledge together so that we can create practical applications.

Kicking things off, were 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, well 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 youre 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 days 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:

basic lambda creation

Log into your account and search for API Gateway from the top search bar.

Next, follow the steps below:

  1. Under HTTP API, select Build
  2. Select Add Integration, then select Lambda
  3. Select the region you used to create your Lambda function
  4. Click in the Lambda Function box and select your lambda
  5. In the API name field, name your API pets-api
  6. When you're done, select Next

Almost done!

On the next page, well be configuring our API endpoint. Go ahead and follow these steps:

  1. For the Method field, change it to GET
  2. For the Resource Path, change that to /pets
  3. Click Next

Were 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

Review and create screen

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 doesnt work

Thats because we didnt assign anything to the root or / resource. Our resource is /pets.

Copy the Invoke URL paste it in your browsers search bar, ensuring that you appends /pets to it. You should now see your list of pets!

If youre just wanting to use this endpoint on your server, then youre all done! However, chances are you want to use this in your frontend applications too. Unfortunately, if you try that now, youll be greeted by a friend that weve all met:

CORS

CORS

Its saying hey, server to server is fine, but server to the browser isnt configured.

Lets fix that.

Back in the AWS Console:

  1. Select the CORS tab on the left-hand side (under Develop)
  2. Select the Configure button Under Access-Control-Allow-Origin enter an asterisk
  3. Click Add, then Save

Access-Control-Allow-Origin with an asterisk as the value allows anyone to call our endpoint. In many scenarios, youll want to lock that down to just your websites domain.

Congratulations! You just enabled CORS for your publicly available API endpoint. Feel free to test it out in a Codesandbox!

Pricing

API Gateway doesnt 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, well learn how to persist data to a NoSQL database by taking a look at DynamoDB.

See ya then!


Original Link: https://dev.to/mtliendo/intro-to-api-gateway-g8e

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To