Build a Serverless Web Application Part 4— Amazon API Gateway

Yst@IT
4 min readNov 28, 2019

--

AWS Build a Serverless Web Application

This is a walk through lab based on AWS project. For more details please refer here. This walk through breaks into four parts which are

  1. Host a static website
  2. Manage users
  3. Build serverless backend
  4. Deploy a RESTful API

Deploy a RESTful API

By far, I think this is the most complicated lab within the 4 parts. In this last lab, we will build an Amazon API Gateway as an entry point for the public to access Lambda function created in Part 3. API Gateway can also integrate with Amazon Cognito, executing Lambda function if user is successfully authenticated by Cognito.

Creating API Gateway:

Navigate to API Gateway service and click “Get Started”.

AWS Build a Serverless Web Application

Choose New API, give your API a name, choose Edge optimized as it is best for pubic services being accessed from internet, then click “Create API”

AWS Build a Serverless Web Application

Next, as mentioned earlier, API Gateway can use Cognito User Pools to authenticate API calls, to be specific, the JWT tokens returned by Cognito. So let’s set API Gateway up.

  1. Click Authorizers.
  2. Click Create New Authorizer.
  3. Give any name you like.
  4. Click Cognito.
  5. Click the input field and a list will show up then choose the Cognito User Pool created in Part 2.
  6. Input “Authorization”
  7. Hit “Create”.
AWS Build a Serverless Web Application

Finish view.

AWS Build a Serverless Web Application

Next we test if the Authorizer is setup correctly by getting the JWT from frontend first and use the Test function. From browser, append /ride.html to your S3 Static Website endpoint. Refer to Part 1 if you don’t remember. Sign in if you are logged out. Copy the JWT value after signed in.

AWS Build a Serverless Web Application

Back to API Gateway Authorizers page and click “Test” beside “Edit”, input JWT value and click “Test”.

AWS Build a Serverless Web Application

If everything is setup correctly, you will see the response replied by Cognito of the JWT given.

AWS Build a Serverless Web Application

Next we create resource and method. Resource is the sub url the browser will be calling. Method is what the browser uses to call the resource.

Follow the steps below to create a resource. Give it a name, enable CORS and create it.

AWS Build a Serverless Web Application

Click /ride, click “Actions”, Create Method. Choose “POST” in the list and save.

AWS Build a Serverless Web Application

Next, since API Gateway will trigger Lambda to execute action, we configure as below. For Lambda Function, input the name of your Lambda function. Click “OK” if any permission window is shown.

AWS Build a Serverless Web Application
AWS Build a Serverless Web Application

Next we setup the Authorizer authentication with Cognito created a while ago. Click “Method Request”

AWS Build a Serverless Web Application

Click on the “pencil” icon, select the Authorizer just created and save.

AWS Build a Serverless Web Application

Now we need to deploy the API Gateway so it is accessible from internet.

AWS Build a Serverless Web Application

Select “New Stage” and give it a name.

AWS Build a Serverless Web Application

Copy the URL as we will need it in the next step.

AWS Build a Serverless Web Application

Modify the config.js file in S3 as below. Save and replace it on S3.

AWS Build a Serverless Web Application

Finally we can validate our result. Visit your static website url with /ride.html. Click anywhere on the map and click “Request Unicorn”.

AWS Build a Serverless Web Application

You will see an unicorn appear and running to the point you selected. Status messages are also shown on the right side.

AWS Build a Serverless Web Application

And that’s it!

AWS Build a Serverless Web Application

CleanUP

Lastly, refer here to clean up all the resources used in the whole lab!

Reference

Module 4. RESTful APIs

--

--

Yst@IT

Cloud Solution Architect, focusing on Oracle Cloud Infrastructure currently.