Passing request parameters for aws lambda function

I am trying to configure a Lambda function that pulls out request parameters that are passed to the URL of the API gateway that is being created. (Sidebar: I'm still pretty green when it comes to programming, so please forgive any inadvertent confusion about how to name things on my part). I wrapped some REST calls inside the fiber using Synchronize.JS, and it works fine when I hardcode the variables that I want to pass to different REST URLs, but our goal is to pass in various parameters that will act as global variables for various REST calls. This is what I have now ...

// Dependendies var request = require('superagent'); var sync = require('synchronize'); exports.handler = function(event, context) { sync.fiber(function() { var followingArray = []; var followersArray = []; var postsCountArray = []; var allSqorCountArray = []; var trendingPostAuthorArray = []; var hashtagCountArray = []; var notificationCountArray = []; var getParam = function(query){ var SearchString = window.location.search.substring(1); var VariableArray = SearchString.split('&'); for(var i = 0; i < VariableArray.length; i++){ var KeyValuePair = VariableArray[i].split('='); if(KeyValuePair[0] == query){ return KeyValuePair[1]; } }; }; var userId = getParam('userId'); var limit = getParam('limit'); var offset = getParam('offset'); var restCallOne = function() { request('https://someurl.com/etc/' + userId + '/follows?limit=' + limit + '&offset=' + offset) .end(function(err, res) { if (err) { console.log('Request 1'); context.fail("Danger Will Robinson!!! Follows Count Does Not Like You!!"); } else { var followsCount = res.body.total_count; followingArray.push(followsCount); } }); }; var restCallTwo = function() { request .get('https://someurl.com/etc/etc/etc?limit=' + limit + '&offset=' + offset) .set({'access-token': 'someAccessToken'}) .end(function(err, res) { if(err) { console.log('Request 4'); context.fail("Danger Will Robinson!! The All Sqor Feed is moody"); } else { var allSqorCount = res.body.total_count; allSqorCountArray.push(allSqorCount); } }); }; var restCallThree = function() { request .get('https://someUrl.com/etc/' + userId + '/followers?limit=' + limit + '&offset=' + offset) .end(function(err, res) { if (err) { console.log('Request 3'); context.fail("Danger Will Robinson!!! Following Count Done Smacked You Upside The Head!!"); } else { var count = res.body.total_count; followersArray.push(count); context.done(null, 'The total number of people that follow Andy is ' + followersArray[0] + ', and the number of people that Andy follows is ' + followingArray[0] + ', and the number of posts in his feed is ' + allSqorCountArray[0]); } }); }; try { restCallOne(userId, limit, offset); } catch(errOne) { console.log("Error on call 1!!"); } try { restCallTwo(limit, offset); } catch(errTwo) { console.log("Error on call 2!!"); } try { restCallThree(userId, limit, offset); } catch(errThree) { console.log("Error on call 3!!"); } }); }; 

The URL of the API gateway that was generated when I linked it to this function is: https://someID.execute-api.us-east-1.amazonaws.com/syncFunctionalParams

but I would like to pass something like this and use the parameters in my Lambda function to return the correct information from the rest of the calls: https://someID.execute-api.us-east-1.amazonaws.com/syncFunctionalParams?userId=212733&limit = 25 & offset = 0

Is it possible? Am I okay with this?

+2
source share
1 answer

You need to configure the mapping template in the Gateway API. If you know the name of your parameters in advance, your template may look like this:

 { "userId": "$input.params('userId')", "limit": "$input.params('limit')", "offset": "$input.params('offset')" } 

Where each $input.params('...') will be evaluated, and the value in your query string will be put in its place when the event is sent to Lambda.

If you do not know the parameter names in advance, you will need to parse in Lambda. Your pattern matching will look like this:

 { "querystring": "$input.params().querystring" } 

In the end, it will look like the one shown in Lambda:

 { "querystring": "{limit=25, offset=0, userId=212733}" } 

And then you will parse event.querystring instead of window.location.search in getParam() . Obviously, you will need to change some of the logic, since you will be sharing commas, not ampersands, and you will need to get rid of curly braces. By the way, since you are on the server at the moment you do not have a window object.

+3
source

Source: https://habr.com/ru/post/1240046/


All Articles