传递AWS Lambda函数的查询参数 [英] passing query params for aws lambda function

查看:84
本文介绍了传递AWS Lambda函数的查询参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置一个Lambda函数,该函数将提取传递到所创建的API网关URL中的查询参数. (侧边栏:在编程方面,我还是很环保的,所以请原谅我对如何命名事物的任何无意的困惑).我已经使用Synchronize.JS在光纤中包装了一些REST调用,当我对要传递到各种REST URL中的变量进行硬编码时,它的工作原理非常好,但是我们的目标是能够传递将要传递的不同参数.充当进行不同的REST调用的全局变量.这是我目前拥有的...

I am trying to set up a Lambda function that will pull query params that are passed into the API Gateway URL that is created. (Sidebar: I am still pretty green when it comes to programming, so please forgive any unintentional confusion regarding how to name things on my part). I've wrapped a few REST calls within a fiber using Synchronize.JS, and it works great when I hard code the variables that I want to pass into the various REST urls, but our goal is to be able to pass different parameters that will act as global variables for the different REST calls being made. This is what I currently have...

// 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!!");
        }
    });
};

与此功能链接时生成的API网关URL如下: https://someID.execute-api.us-east-1.amazonaws.com/syncFunctionalParams

The API Gateway URL that was generated when I linked it with this function is as such: https://someID.execute-api.us-east-1.amazonaws.com/syncFunctionalParams

但是我希望能够传递这样的信息,并在我的Lambda函数中使用params从其余的调用中返回正确的信息: https://someID .execute-api.us-east-1.amazonaws.com/syncFunctionalParams?userId = 212733& limit = 25& offset = 0

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

这可能吗?我在这方面偏离了轨道吗?

Is this possible?? Am I way off track with this?

推荐答案

您需要设置

You need to setup a mapping template in API Gateway. If you know the name of your parameters ahead of time your template could look like this:

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

当事件传递给Lambda时,将评估每个$input.params('...')的位置,并将查询字符串中的值放在其位置.

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

如果您不提前知道参数名称,则必须在Lambda中进行一些解析.您的映射模板如下所示:

If you don't know the parameter names ahead of time you will have to do some parsing in Lambda. Your mapping template would look like this:

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

在传递给Lambda的事件中,最终会变成这样:

Which will end up looking like this in the event that is passed to Lambda:

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

然后您将解析event.querystring而不是getParam()中的window.location.search.显然,您将需要更改一些逻辑,因为您将使用逗号而不是&符进行分隔,并且需要除去花括号.顺便说一下,由于您目前在服务器上,因此您没有window对象.

And then you would parse event.querystring instead of window.location.search in your getParam(). Obviously you would need to change some of the logic since you'll be splitting on commas instead of ampersands and you'd need to get rid of the curly brackets. Which by the way, since you are on the server at this point you don't have a window object.

这篇关于传递AWS Lambda函数的查询参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆