Skip to content

Latest commit

 

History

History

03_APIGW

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

Amazon API Gateway

In this module you'll use API Gateway to expose the Lambda function you built in the previous module as a RESTful API.

Serverless API Architecture

Implementation Instructions

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.

1. Login to AWS Console

  1. Go to your AWS Console

  2. Login as IAM user which have enough privilege

  3. Select AWS region: N. Virginia

2. Create a New REST API

  1. In the AWS Management Console, click Services then select API Gateway under Application Services.

  2. Choose Create API.

  3. Select New API and enter SuperHeroes for the API Name.

  4. Choose Create API

3. Create a new resource and method

  1. In the left navigation, click on Resources under your SuperHeroes API.

  2. Click / (the root of the folder you're looking at)

  3. From the Actions dropdown select Create Resource.

  4. For Resource Name, type getheroeslist

  5. For Resource Path, type getheroeslist (this path may be auto-populated for you)

  6. Click Create Resource

  7. Now you will create another child resource.

  8. Click / (again, the root folder)

  9. Click Actions, then click Create Resource

  10. For Resource Name, type getmissiondetails

  11. For Resource Path, type getmissiondetails (this path may be auto-populated for you)

  12. Click Create Resource

4. Lambda Function Integration

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.

  1. Click the getheroeslist resource

  2. With the getheroeslist resource selected, click Actions, then click Create Method

  3. You will see a dropdown menu. Click Post. Then click the check mark

  4. Now you will setup your method. For Integration type, click Lambda Function

  5. For Lambda Region, choose your working region (ex. us-east-1)

  6. For Lambda Function, select GetHeroesList

  7. Click Save

  8. You will see a warning that you are about to give the API Gateway service permission to invoke your lambda function. Click OK.

  9. Click the getmissiondetails resource.

  10. With the getmissiondetails resource selected, click Actions, then click Create Method

  11. You will see a dropdown menu. Click Post. Then click the check mark

  12. Now you will setup your method. For Integration type, click Lambda Function

  13. For Lambda Region, choose your working region (ex. us-east-1)

  14. For Lambda Function, select GetMissionDetails

  15. Click Save

  16. You will see the same warning. Click OK.

5. Enable CORS

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

  1. Click the getheroeslist resource

  2. With the getheroeslist resource selected, click Actions, then click Enable CORS

  3. Make sure both methods, Post and Options are selected.

  4. Click Enable CORS and replace existing CORS headers.

  5. Confirm that you want to Yes, replace existing values.

  6. Use the same process to enable CORS, using exactly the same settings for getmissiondetails resource.

6. Deploy API

  1. Click the root folder /

  2. Click Actions, then click Deploy API.

  3. For Deployment Stage, select New Stage.

  4. Enter prod for the stage name.

  5. Click Deploy

  6. Note the Invoke URL. You will use it in the next section.

7. Test API