在AWS上的现有EC2实例中运行Python脚本 [英] Running Python Script in an existing EC2 instance on AWS

查看:446
本文介绍了在AWS上的现有EC2实例中运行Python脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个API(使用python),该API必须更改已经在运行的EC2实例中的文件.我正在搜索boto3文档,但只能找到启动新EC2实例的功能,而不能连接到已经存在的实例.

I have an API (in python) which has to alter files inside an EC2 instance that is already running. I'm searching on boto3 documentation, but could only find functions to start new EC2 instances, not to connect to an already existing one.

我目前正在考虑复制API函数以更改EC2实例内部脚本中的文件,并让API可以通过使用某种SSH库访问EC2实例来简单地启动该脚本.

I am currently thinking of replicating the APIs functions to alter the files in a script inside the EC2 instance, and having the API simply start that script on the EC2 instance by accessing it using some sort of SSH library.

这将是正确的方法吗?或者是否有一些boto3函数(或其他一些Amazon/AWS库中的函数)可以让我在现有实例中启动脚本?

Would that be the correct approach, or is there some boto3 function (or in some of the other Amazon/AWS libraries) that allows me to start a script inside existing instances?

推荐答案

Amazon EC2实例就像Internet上的任何计算机一样.它运行的是操作系统(例如Linux或Windows),并且具有内置的标准安全性.它是Amazon EC2实例这一事实没有影响.

An Amazon EC2 instance is just like any computer on the Internet. It is running an operating system (eg Linux or Windows), and it has standard security in-built. The fact that it is an Amazon EC2 instance has no impact.

因此,问题实际上变成了: 我如何在远程计算机上运行命令?

So, the question really becomes: How do I run a command on a remote computer?

典型的方法包括:

  • 连接到计算机(例如通过SSH)并运行命令
  • 在侦听特定端口的计算机上运行服务(例如,响应API请求)
  • 使用远程shell命令在另一台计算机上运行操作

幸运的是,AWS提供了一个附加选项:使用

Fortunately, AWS offers an additional option: Use the AWS Systems Manager Run Command:

AWS Systems Manager运行命令使您可以远程安全地管理托管实例的配置.受管实例是混合环境中已为Systems Manager配置的任何Amazon EC2实例或本地计算机.通过运行命令,您可以自动执行常见的管理任务并大规模执行临时配置更改.您可以从AWS控制台,AWS命令行界面,适用于Windows PowerShell的AWS工具或AWS开发工具包使用运行命令.免费提供运行命令.

AWS Systems Manager Run Command lets you remotely and securely manage the configuration of your managed instances. A managed instance is any Amazon EC2 instance or on-premises machine in your hybrid environment that has been configured for Systems Manager. Run Command enables you to automate common administrative tasks and perform ad hoc configuration changes at scale. You can use Run Command from the AWS console, the AWS Command Line Interface, AWS Tools for Windows PowerShell, or the AWS SDKs. Run Command is offered at no additional cost.

管理员使用运行命令"在其托管实例上执行以下类型的任务:安装或引导应用程序,构建部署管道,在实例从Auto Scaling组终止时捕获日志文件,并将实例加入Windows域,仅举几例.

Administrators use Run Command to perform the following types of tasks on their managed instances: install or bootstrap applications, build a deployment pipeline, capture log files when an instance is terminated from an Auto Scaling group, and join instances to a Windows domain, to name a few.

基本上,它是实例上安装的代理(或就此而言,在Internet上的任何计算机上),并且可以将命令发送到由代理执行的计算机.实际上,如果需要,可以将同一命令发送到数百台计算机.

Basically, it is an agent installed on the instance (or, for that matter, on any computer on the Internet) and commands can be sent to the computer that are executed by the agent. In fact, the same command can be sent to hundreds of computers if desired.

可以通过API调用触发AWS Systems Manager运行命令,例如使用boto3的程序.

The AWS Systems Manager Run Command can be triggered by an API call, such as a program using boto3.

这篇关于在AWS上的现有EC2实例中运行Python脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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