Creating an endpoint to support filtering data based on query string parameters - ActoKids/api GitHub Wiki
Note: This wiki assumes an existing API configured with a custom domain name, and an existing Lambda function.
- From the API Gateway console, select the appropriate API from the list of available APIs
- Click to highlight a resource (e.g. '/events'), click the 'Actions' button, and select 'Create Resource'.
- Enter a name for the resource (e.g 'eventsFilter'). This will also be the Resource Path name (e.g. /events/eventsFilter).
- Check the box beside 'Enable API Gateway CORS'.
- Leave the box beside 'Configure as proxy resource' unchecked.
- Click 'Create Resource'. Note: resource creation may take a few minutes, and you may have to refresh the page.
- Click to highlight the newly-created resource, click the 'Actions' button, and select 'Create Method'. From the tiny drop-drown menu that appears beneath the resource name, select 'GET' and click the check-mark button to confirm.
- For the Integration type, select 'Lambda Function'.
- Leave the box beside 'Use Lambda Proxy Integration' unchecked.
- Select your region
- Start typing the name of your Lambda function, then select it from the list that appears beneath
- Check the box beside 'Use Default Timeout'
- Click the 'Save' button
- In the modal that pops up, click 'OK' to add permission to the specified lambda function
- Click on the newly-created method, then click 'Integration Request' in the Method Execution diagram to the right.
- Scroll down and click the black arrow beside 'Mapping Templates'.
- For 'Request body passthrough', select 'When there are no templates defined (recommended)'.
- Click the 'plus' button beside 'Add mapping template, enter 'application/json', and click the check-mark button.
- In the modal that pops up, click 'Yes, secure this integration'.
- Scroll down and click the drop-down menu beside 'Generate template' and select 'Method Request passthrough'. This will populate the box with boilerplate code.
- Click 'Save'. Note: nothing will happen upon clicking 'save' -- that's normal. Use your browser's 'Back' button to return to the Method Execution diagram.
- From the Method Execution diagram, click 'Method Response'
- Click the 'plus' button beside 'Add Response'
- Enter '200' and click the check-mark button.
- Click the method name from the Resources tree to return to the Method Execution diagram.
- Click to highlight the 'Options' method
- For the Integration type, select 'Mock'
- Click the 'Save' button
- Click the 'Actions' button, select 'Deploy API', choose a Deployment stage, and click 'Deploy'.
-
Import the AWS sdk, and instantiate a reference to the DynamoDB Document client: For example:
const AWS = require('aws-sdk'); const docClient = new AWS.DynamoDB.DocumentClient({ region: 'us-east-2'}); -
API Gateway attaches query string parameters to the event object. They can be accessed by referencing the event.params.querystring object. For example:
var qs = event.params.querystring; -
Define an object to store the parameters passed to DynamoDB. Attributes include:
- TableName : <name of DynamoDB table>
- FilterExpression : <string of the form AttributeName OPERATOR :AttributeValueKey>
For example:
FilterExpression: 'contact_email = :contact_email'
...where ':contact_email' is a defined value in the ExpressionAttributeValues object. - ExpressionAttributeValues : < Keys (strings preceded by a colon) and values on which the data shall be filtered >
For example:
ExpressionAttributeValues: {':contact_name' : qs.contact_name}
...where 'contact_name' is a db attribute value, and qs.contact_name is a reference to the value of the query string parameter, 'contact_name'.
-
Pass parameters to the 'scan' method of the DocumentClient and await results.
For example:
const items = await docClient.scan(params).promise();