Building a REST API Gateway with path and query string parameters
Lab 89
Services Covered
- Amazon API Gateway
- Lambda
Lab description
This lab practice’s on creating an API Gateway with path and query string parameters that will be integrated with Lambda function
- Create a Lambda Function
- Create API Gateway
- Create resources for API
- Send API calls and use parameter strings
Lab date
03-10-2021
Prerequisites
- AWS account
Lab steps
- Create a Lambda Function. Choose Python as runtime language, create a new execution role using the policy template: Basic Lambda@Edge permissions. The function code:
import json def lambda_handler(event, context): #Path parameter passed to Lambda function servername = event["params"]["path"]["servername"] #Query string parameter passed to Lambda function Email_id = event['params']['querystring']['emailid'] return { "Path Parameter" : servername, "Query string Parameter" : Email_id }
- Create a REST API Gateway. Then under Actions choose Create Resource, give it a name and type that name as Resource Path then click on Create Resource
- Next create a Method in Actions and in drop down menu choose GET. As Integration type choose Lambda Function and then type the earlier created functions name. After that you’ll be presented with the API structure.
- On the same screen choose Method Request and in the settings change the Request Validator field to Validate body, query string parameters and headers. Then under URL Query String Parameters add query string, enter emailid and make it Required.
Go back to Method Execution by cklicking the link on the top. - Go to the Integration Request and under Mapping templates for the Request body passthrough choose When there are no templates defined and Add mapping template with value application/json. Then generate template Method Request passthrough:
- Go back and deploy the API. Select the GET method resource and in Actions choose Deploy API. Give it a name and deploy. You’ll see the Invoke URL. Then under Stages choose the name resource and GET method and copy the URL. Now if you replace name with Production and add emailid you will receive GET request’s response from the API: