In this module you'll use API Gateway to expose the Lambda function you built in the previous module as a RESTful API.
Each of the following sections provide an implementation overview and detailed, step-by-step instructions. The overview should provide enough context for you to complete the implementation if you're already familiar with the AWS Management Console or you want to explore the services yourself without following a walkthrough.
-
Go to your AWS Console
-
Login as IAM user which have enough privilege
-
Select AWS region: N. Virginia
-
In the AWS Management Console, click Services then select API Gateway under Application Services.
-
Choose Create API.
-
Select New API and enter
SuperHeroes
for the API Name. -
Choose Create API
-
In the left navigation, click on Resources under your SuperHeroes API.
-
Click / (the root of the folder you're looking at)
-
From the Actions dropdown select Create Resource.
-
For Resource Name, type
getheroeslist
-
For Resource Path, type
getheroeslist
(this path may be auto-populated for you) -
Click Create Resource
-
Now you will create another child resource.
-
Click / (again, the root folder)
-
Click Actions, then click Create Resource
-
For Resource Name, type
getmissiondetails
-
For Resource Path, type
getmissiondetails
(this path may be auto-populated for you) -
Click Create Resource
Create a new resource within your API. Then create a POST method for that resource and configure it to use a Lambda integration backed by the Lambda function you created in the first step of this module.
-
Click the getheroeslist resource
-
With the getheroeslist resource selected, click Actions, then click Create Method
-
You will see a dropdown menu. Click Post. Then click the check mark
-
Now you will setup your method. For Integration type, click Lambda Function
-
For Lambda Region, choose your working region (ex. us-east-1)
-
For Lambda Function, select
GetHeroesList
-
Click Save
-
You will see a warning that you are about to give the API Gateway service permission to invoke your lambda function. Click OK.
-
Click the getmissiondetails resource.
-
With the getmissiondetails resource selected, click Actions, then click Create Method
-
You will see a dropdown menu. Click Post. Then click the check mark
-
Now you will setup your method. For Integration type, click Lambda Function
-
For Lambda Region, choose your working region (ex. us-east-1)
-
For Lambda Function, select
GetMissionDetails
-
Click Save
-
You will see the same warning. Click OK.
You will enable CORS for methods you just created in API Gateway. CORS (Cross-Origin Resource Sharing) allows browsers to make HTTP requests to servers with a different domain/origin.
For simplicity, you can set the Access-Control-Allow-Origin header value to '*', but in a production application you should always explicitly whitelist authorized domains to mitigate cross-site request forgery (CSRF) attacks.
For more information about CORS configurations in general, see https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS
-
Click the
getheroeslist
resource -
With the
getheroeslist
resource selected, click Actions, then click Enable CORS -
Make sure both methods, Post and Options are selected.
-
Click Enable CORS and replace existing CORS headers.
-
Confirm that you want to Yes, replace existing values.
-
Use the same process to enable CORS, using exactly the same settings for getmissiondetails resource.
-
Click the root folder /
-
Click Actions, then click Deploy API.
-
For Deployment Stage, select New Stage.
-
Enter
prod
for the stage name. -
Click Deploy
-
Note the Invoke URL. You will use it in the next section.
-
You can use browser plugin to test RESTful Webservice in API Gateway
-
If you don't have any RESTful API client, you can download and install one
- Firefox: RESTClient
- Chrome: Advanced REST client or Postman
-
Test getheroeslist API
- Method: POST
- URL: Your API Gateway Invoke URL with getheroeslist (ex. https://zxxxxxxxxx.execute-api.us-east-1.amazonaws.com/prod/getheroeslist)
- Invoke the URL
- You will expect 200 HTTP response code with list of superheroes in the response body
-
Test getmissiondetails API
- Method: POST
- URL: Your API Gateway Invoke URL with getmissiondetails (ex. https://zxxxxxxxxx.execute-api.us-east-1.amazonaws.com/prod/getmissiondetails)
- Request Body:
{"superhero": "Superman"}
- Invoke the URL
- You will expect 200 HTTP response code with detail information of related superhero in the response body