简体   繁体   English

在没有 API 网关的 lambda 函数中路由

[英]Routing in a lambda function without API Gateway

I have an express app and is deployed via serverless.我有一个快速应用程序,并通过无服务器部署。 My stack is just a lambda function + API Gateway.我的堆栈只是一个 lambda 函数 + API 网关。 That works and it's accessible on Postman or httpxmlrequest.这有效并且可以在 Postman 或 httpxmlrequest 上访问。 But if I take out API Gateway piece from the stack, is it possible to still invoke this lambda function using the AWS SDK/cli, somehow do routing by passing in a path (eg: /books) and a method (eg: POST) together with the payload?但是,如果我从堆栈中取出 API Gateway 部分,是否仍然可以使用 AWS SDK/cli 调用此 lambda 函数,通过传入path (eg: /books)method (eg: POST)以某种方式进行路由连同有效载荷?

I'm new to the AWS ecosystem... just started using serverless + node.我是 AWS 生态系统的新手……刚开始使用无服务器 + 节点。

Say I have a simple express app like so:假设我有一个简单的 Express 应用程序,如下所示:

const serverless = require('serverless-http');
const express = require('express');
const bodyParser = require('body-parser');

const helpersRoute = require('./routes');
const booksRoute = require('./routes/books');

const app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

app.use('/', helpersRoute);
app.use('/books', booksRoute);

module.exports.handler = serverless(app);

And this is my serverless config:这是我的无服务器配置:

service: service-test

provider:
  name: aws
  runtime: nodejs8.10
  stage: dev

functions:
  app:
    handler: index.handler

Yes, it is possible by using the Lambda SDK.是的,可以使用 Lambda SDK。 Since you are running your function behind an express server though, you will have to pass an event which looks exactly like API Gateway's, so, from the invoked Lambda perspective, it would simply be an API Gateway's call.但是,由于您在 express 服务器后面运行您的函数,因此您必须传递一个与 API Gateway 完全一样的事件,因此,从调用的 Lambda 的角度来看,它只是一个 API Gateway 的调用。

You can check the docs to see what an API Gateway event looks like, but, essentially, you only need path and body (in case of POST, PUT, PATCH) requests .您可以查看文档以查看 API 网关事件是什么样的,但本质上,您只需要路径正文(在 POST、PUT、PATCH 的情况下)请求 If you need query params, etc you can also pass them along.如果您需要查询参数等,您也可以传递它们。

Make sure you set the InvocationType to RequestResponse确保将InvocationType设置为RequestResponse

Here's a sample in Node.js :这是Node.js的一个示例:

const lambda = new AWS.Lambda();

await lambda
    .invoke({
      FunctionName: 'FunctionName',
      InvocationType: 'RequestResponse',
      Payload: JSON.stringify({
        path: '/your_path',
      }),
    })
    .promise();

EDIT: The OP was having problems with the way the data comes from a given Lambda function to the Express-based Lambda function, so I decided to add my own configuration for comparison purposes:编辑: OP 在数据从给定 Lambda 函数到基于 Express 的 Lambda 函数的方式方面存在问题,因此我决定添加自己的配置以进行比较:

const serverless = require('serverless-http');
const express = require('express');
const cors = require('cors');

const app = express();
const bodyParser = require('body-parser');
const routes = require('./routes');

app.use(cors());

app.use(bodyParser.json({ strict: false }));

app.use('/api', routes);

module.exports.handler = serverless(app);

Sample controller method:示例控制器方法:

const fetchOne = async (req, res) =>
  res
    .status(200)
    .json(await MyService.doSomething(req.body.someField));

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

相关问题 获取Lambda函数的API网关端点 - Get API Gateway endpoints for Lambda function Nestjs 在 lambda 函数上运行而不创建与 AWS API Gateway 结合的实际服务器? - Nestjs run on lambda function without creating an actual server to combine with AWS API Gateway? AWS Lambda - 在 lambda 函数中获取 API 网关路径 - AWS Lambda - Get API Gateway path in the lambda function “生产化” Lambda 和 API 网关 - "Productionising" Lambda and API Gateway 在 AWS Lambda for Node 模板中返回没有 API 网关的 HTML 响应 - Returning HTML response without API gateway in AWS Lambda for Node template AWS API Gateway-更新文档时删除了lambda函数引用 - AWS API Gateway - Removed lambda function references when update the documentation 使用API​​网关反应调用Node.js PUT Lambda函数 - React calling a Node.js PUT Lambda function with API Gateway 试图理解 lambda 函数,它是 api 网关 websockets 连接逻辑的一部分 - Trying to understand lambda function that is part of the logic of api gateway websockets connection 邮递员调用 AWS API Gateway 以触发 AWS lambda 函数不起作用 - Postman call to AWS API Gateway to trigger AWS lambda function not working 如何在Lambda函数中访问API网关请求模型? - How to access API Gateway request Model in Lambda function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM