繁体   English   中英

如何将 EC2 实例用作 API 服务器? 目前使用的是 C++ lambda

[英]How do I use EC2 instance as an API server? Currently using a C++ lambda

AWS新手在这里。

目前我正在为 Lambda 使用 C++ 自定义运行时(如本教程所示 https://aws.amazon.com/blogs/lambda-runtime/introducing-the-c

I integrated it with the AWS API gateway so when the GET/POST on the API is called, my lambda function like this is called,

static invocation_response my_handler(invocation_request const& req)

“req.payload”将包含我发送的 JSON 请求。 到目前为止,一切都很好。

由于延迟/实验相关的原因(还有 memory 缓存中缺少的事实是一个问题),我想使用 EC2 实例做同样的事情。 ie When the REST API is called with a JSON payload, it forwards the request to the C++ code running on my EC2. 换句话说,就好像我的 lambda 在 EC2 实例上运行一样。

我如何实现这一目标?

您正在寻找不同的 API 网关集成。

对于 Lambda,这通常是Lambda 代理集成或 Lambda 非代理(自定义)集成 [1]

If you want to reach an EC2 instance, deploy an arbitrary C++ application which exposes an HTTP endpoint and use the HTTP proxy integration or the HTTP custom integration . [2]

如果您想保护您的 EC2 实例并使其只能从 API 网关访问,您可以使用私有集成。 [3]

我建议不要尝试在 EC2 上安装类似 Lambda 的环境,而是在 EC2 实例上使用 docker 容器。 这更容易部署和管理。 您可以通过将您的 EC2 实例指定为底层 ECS 容器实例,通过弹性容器服务 (ECS) 管理您的 C++ 实例。 [4]

使用 ECS,您可以利用 EC2 的全部功能(作为底层 EC2 实例的大内存)和易于部署(作为易于使用的容器部署工具)。 您可以使用内置 ALB 集成,通过 HTTP 集成将 API 网关指向 ECS 的集群 ALB 端点。 此外,您可以将 C++ 代码作为 docker 图像上传到 ECR,并在 ECS 任务集中引用它。

参考

[1] https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-integrations.html
[2] https://docs.aws.amazon.com/apigateway/latest/developerguide/setup-http-integrations.html#api-gateway-set-up-http-proxy-integration-on-proxy-resource
[3] https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-private-integration.html
[4] https://docs.aws.amazon.com/AmazonECS/latest/developerguide/ECS_instances.html

是否可以使用 SQS 队列来解耦您的应用程序? Lambda can push the message to the SQS queue, which you EC2 instance running C++ code is polling, and once the request is processed, EC2 can push the request in an output queue, to be consumed by lambda to sent the result back to the user .

“像 AWS 一样”运行它的最接近的可能方法是配置Firecracker Micro VM ,因为这正是 AWS 运行 LAmbdas/Fargate 的方式。

但是,尝试运行它的最简单方法是使用SAM Local 通过这样做,它将在本地 Docker 容器上调用Lambda function。

或者尝试查看localstack

暂无
暂无

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

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