简体   繁体   English

通过 API Gateway 在 EC2 上运行函数

[英]Running a function on EC2 through API Gateway

I want to link Amazon API Gateway to a function in my EC2 instance but am finding little online about hwo to do this.我想将 Amazon API Gateway 链接到我的 EC2 实例中的一个函数,但在网上几乎没有找到关于 hwo 来执行此操作的信息。

Currently I have set up the API call as follows:目前,我已按如下方式设置 API 调用: 在此处输入图片说明

Can anyone shed any light as to how I could connect the API call to my python function called 'test.py' in the root folder of my EC2 instance任何人都可以说明我如何将 API 调用连接到我的 EC2 实例根文件夹中名为“test.py”的 python 函数

I suppose you might be able do this with the AWS Run Command service, but it is a weird way of doing things.我想您可以使用AWS Run Command服务来做到这一点,但这是一种奇怪的做事方式。 The AWS Service Proxy proxies the AWS API. AWS 服务代理代理 AWS API。 So telling it to proxy the AWS EC2 service exposes the AWS API to manage EC2 instances.因此,告诉它代理 AWS EC2 服务会公开 AWS API 来管理 EC2 实例。 Managing EC2 instances includes things like creating and deleting servers.管理 EC2 实例包括创建和删除服务器等内容。 It does not include things initiating an SSH connection to the server, logging into the server, and then running a command on the server.它不包括启动到服务器的 SSH 连接、登录到服务器,然后在服务器上运行命令的事情。

The standard way to run a script on a server via API Gateway is to expose that script via a web server on the EC2 server, and then have API Gateway hit the appropriate URL.通过 API Gateway 在服务器上运行脚本的标准方法是通过 EC2 服务器上的 Web 服务器公开该脚本,然后让 API Gateway 访问适当的 URL。

API Gateway cannot directly execute a Python function sitting on the file system of your EC2 instance. API Gateway 无法直接执行位于 EC2 实例文件系统上的 Python 函数。 API Gateway can only interact with EC2 instances via http/https endpoints. API Gateway 只能通过 http/https 端点与 EC2 实例交互。 If you must run you Python function on an EC2 instance then you'll need to run a web server or application server on your EC2 instance and set it up to execute your Python function when it gets a request on a specific path.如果您必须在 EC2 实例上运行 Python 函数,那么您需要在 EC2 实例上运行 Web 服务器或应用程序服务器,并将其设置为在收到特定路径上的请求时执行您的 Python 函数。 Then set-up your API Gateway http integration endpoint to use that path.然后设置您的 API Gateway http 集成端点以使用该路径。

If you just need to execute this Python function and don't necessarily need it to run on this EC2 instance, then you could set-up a Lambda function containing your Python function.如果您只需要执行此 Python 函数并且不一定需要它在此 EC2 实例上运行,那么您可以设置一个包含您的 Python 函数的 Lambda 函数。 Then set-up your API Gateway to call the Lambda function.然后设置您的 API 网关以调用 Lambda 函数。 Using the Lambda approach means that you don't need to manage the EC2 instance.使用 Lambda 方法意味着您不需要管理 EC2 实例。 Also, for low-volume use cases, Lambda can be much more cost effective than running a dedicated EC2 instance.此外,对于低容量用例,Lambda 比运行专用 EC2 实例更具成本效益。

you can do it by invoking System manager "Send Command" from API Gateway Integration Request.您可以通过从 API 网关集成请求调用系统管理器“发送命令”来实现。 EC2 instance has to be managed by SSM and instance Role associated with your EC2 instance. EC2 实例必须由与您的 EC2 实例关联的 SSM 和实例角色管理。

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

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