Create API Gateway to the Lambda Function

 Create API Gateway to the Lambda Function


==================================================

To set up the API Gateway linked to Lambda function:

1. From the AWS Management Console, select the API Gateway product link to get the API Gateway Management Console.

2. Click on the Create API button.

3. Under the Rest API section, click on the Build button.

4. Click OK on the welcome popup if this is the first time.

5. Select the New API radio button.

.....a. Enter an API name (i.e., pApi).

.....b. Enter a Description if you like.

.....c. For Endpoint Type, choose Regional (note- you have 600 available for basic accounts).

.....d. Click the Create API button.

6. On the Actions drop down button in the middle, select Create Method.

.....a. Choose the GET option and click the check mark next to it.

.....b. Select the Lambda Function radio button for integration type.

.....c. Select your region for the Lambda function.

.....d. Enter/select your Lambda function name (i.e. appUserService).

.....e. Click the Save button.

.....f. Click OK to add the permission for the API Gateway to execute the Lambda function.

.....g. Select Resources on the menu on the left.

.....h. Click on the Actions drop down button and select Enable CORS.

..........1. Click on the Enable CORS and Replace Existing CORS headers button. Confirm by clicking the Yes, Replace Existing Values button.

.....i. To add the ability to pass URL query parameters through the API Gateway to the Lambda function, set up resource mapping for the API.

..........1. Click on the Resources link on the left side menu

..........2. Click on the GET method link.

..........3. Click on the Integration Request link.

..........4. Open the Mapping Templates section.

..........5. Select the When There are No Templates defined radio button.

..........6. Click on the Add Mapping Template link.

..........7. Enter application/json and click on the check mark under Content Type.

..........8. Click on the application/json link.

..........9. Choose the Method Request Passthrough option on the Generate Template drop-down list.

..........10. Click Save.

.....j. Click on the Actions drop down button and select Deploy API.

..........1. On the Deployment Stage list box, select New Stage.

..........2. Enter a name for the new stage such as Release.

..........3. Enter descriptions if desired.

..........4. Click on the Deploy button.

.....k. Click on Stages on the menu on the left and find the invoke URL at the top.

..........1. Select the Release stage link.

..........2. The format of this URL is:

...............https://.execute-api..amazonaws.com/

..........3. Note the URL on the configuration information table.

..........4. Click on the URL link to open a new window and test access to the API (should have access to the prototype Lambda function showing no results as you have not passed in the parameters which will occur in the next section).

7. You now have access to the database via the Lambda function and the API Gateway. It is not yet secure yet but can be tested with the prototype web pages.

8. To test query parameter passing, added the following to the URL (only done once server configs are completed):

.....?action=getclientconfig&app_name=[app]

=====================================================================

Create an Authorizer in API Gateway:

1. From the AWS Management Console, select the API Gateway to get the API Gateway Console.

2. Select the API That you created for this prototype.

3. Select Authorizers from menu on the left click the Create New Authorizer button.

4. Enter a name for the authorizer (i.e., pAuth).

5. Select the Cognito radio button under Type.

6. Select your region under Cognito User Pool and select an available region on the drop-down list.

7. Select the Cognito User Pool for this prototype.

8. For Token Source enter the word Authorization.

9. Click the Create link.

10. Note: the test button will not work if you choose to use access tokens for Cognito.

11. Exit all browser windows and open a new browser window.

=====================================================================

Configure API Gateway to use Cognito Scope:

1. From the AWS Management Console, select the API Gateway to get the API Gateway Console.

2. Select your API created for this prototype.

3. Select Resources from the menu on the left.

4. Under the Resources column, select the GET resource.

5. Click on the Method Request panel, click on the Method Request link.

.....a. Click on the pencil icon next to Authorization.

.....b. On the Authorization drop down, select the Authorizer created for the Amazon Cognito User Pool.

.....c. Save settings with the check mark icon for the Authorization.

.....d. Click on the pencil next to OAuth Scopes.

.....e. Type in the scope and enter the scope full name configured (i.e., pId/pScope).

.....f. Click the check mark icon next to OAuth Scopes to save the changes.

.....g. On the Action drop-down, select Enable CORS and confirm that you want it enabled.

.....h. On the Action drop-down, select Deploy API.

.....i. Select the Deployment Stage and then click on the Deploy button.

.....j. Wait a minute or two for the API to deploy completely before testing.

.....k. You now need to go through the application and log in to get a token in order to be able to access the API URL. It no longer works directly.

Comments