简体   繁体   English

如何从API网关将请求类型传递给lambda?

[英]How to pass a request type to lambda from API gateway?

I have a lambda working from API gateway at the moment. 我现在有一个从API网关工作的lambda。 but it's just hardcoded to GET data. 但它只是硬编码到GET数据。 I want to post some data via gateway & lambda to RDS. 我想通过网关和Lambda将一些数据发布到RDS。 I want to do it in the same lambda and use some conditional blocks or switch statement inside it. 我想在同一lambda中执行此操作,并在其中使用一些条件块或switch语句。

Is there any way to tell lambda what the request type is (GET/POST etc) and then the payload with it so that the lambda correctly routes and only executes that particular part of the function? 有没有办法告诉lambda请求类型是什么(GET / POST等),然后是有效负载,以便lambda正确路由并只执行函数的特定部分?

something like: 就像是:

    exports.handler = (event, context, callback) => {
        context.callbackWaitsForEmptyEventLoop = false
        pool.getConnection(function(err, connection) {
          if (err) callback(err)

          if (request === GET && payload === exercises) {
             connection.query('SELECT * FROM exercises', function (error, results, fields) {
          } else if (request === POST && payload === workouts){
         // do some INSERT INTO sql statement
}


            connection.release();

            if (error) callback(error)
            else callback(null, results)

          });
        })
    }

This seems like a bad idea to begin with. 首先,这似乎是一个坏主意。 You need to attach lambda function to a specific resource in your API Gateway (POST, GET method). 您需要将lambda函数附加到API网关中的特定资源(POST,GET方法)。 When GET request comes to your API Gateway, API Gateway executes lambda function associated to that specific resource and the same goes for the POST request. 当GET请求进入您的API网关时,API网关执行与该特定资源相关联的lambda函数,同样适用于POST请求。 Therefore you don't need to handle routing inside of your lambda function as it is handled by API Gateway. 因此,您不需要处理lambda函数内部的路由,因为它由API Gateway处理。 You just need to attach correct lambda function to correct resource in your API Gateway. 您只需要附加正确的lambda函数即可更正API网关中的资源。

The only reason why you might need such functionality is that you are trying to attach the same lambda function to multiple resources which is a bad idea for more than just one reason. 您可能需要此类功能的唯一原因是您尝试将相同的lambda函数附加到多个资源,这不仅仅是一个原因。 Such function will be more complicated than necessary (harder to maintain and debug), as well as it will run longer (though probably not by much in this case) which will result in increased costs. 这样的功能将比必要的更复杂(更难维护和调试),并且它将运行更长时间(尽管在这种情况下可能不会太多),这将导致成本增加。

So the solution is simply create one function that will handle GET request and one that will handle POST request. 因此,解决方案只是创建一个将处理GET请求的函数和一个将处理POST请求的函数。

Considering payload, you can access it via event parameter. 考虑有效负载,您可以通过event参数访问它。

This is definitely possible with Lambda proxy integration , when you connect each resource method with the same Lambda: 当您使用相同的Lambda连接每个资源方法时,使用Lambda代理集成绝对可以实现这一点:

exports.handler = async (event) => {
    if ('POST' === event.httpMethod) {
        ...
    }
    if ('GET' === event.httpMethod) {
        ...
    }
}

And it's probably fine for the beginning, when you don't have enough knowledge yet how to cut your domain into functions (sometimes called nanoservices , as building blocks of microservices), but generally it's considered to be a bad design due to maintainability - you're service is doing too much and the code becomes too conditional and therefore complex. 刚开始时可能还不错,当您还没有足够的知识如何将域划分为功能时(有时称为nanoservices ,作为微服务的构建块),但由于可维护性,通常将其视为不好的设计-您服务工作太多,代码变得有条件,因此很复杂。

You can start with one Lambda and after domain boundaries are found, cut the logic into separated components, one per API resource, by its responsibility. 您可以从一个Lambda开始,并在找到域边界之后,根据其职责将逻辑切成单独的组件,每个API资源一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM