简体   繁体   English

在AWS上的现有EC2实例中运行Python脚本

[英]Running Python Script in an existing EC2 instance on AWS

I have an API (in python) which has to alter files inside an EC2 instance that is already running. 我有一个必须在已经运行的EC2实例内更改文件的API(在python中)。 I'm searching on boto3 documentation, but could only find functions to start new EC2 instances, not to connect to an already existing one. 我正在搜索boto3文档,但只能找到启动新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. 我目前正在考虑复制API函数以更改EC2实例内部脚本中的文件,并让API可以通过使用某种SSH库对其进行访问来在EC2实例上简单地启动该脚本。

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? 那将是正确的方法吗?或者是否存在一些boto3函数(或在其他一些Amazon / AWS库中)允许我在现有实例中启动脚本?

Unless you have a specific service running on that machine which allows you to modify mentioned files. 除非您在该计算机上运行了特定的服务,该服务允许您修改提到的文件。 I would make an attempt to log onto EC2 instance as to any other machine via network. 我将尝试通过网络登录到EC2实例以及其他任何计算机上。

You can access EC2 machine via ssh with use of paramiko or pexpect libraries. 您可以使用paramiko或pexpect库通过ssh访问EC2机器。

If you want to use the execute a script inside of an existing EC2 instance - you could use the reference from the existing answer here : Boto Execute shell command on ec2 instance 如果要在现有EC2实例中使用执行脚本-您可以在此处使用现有答案中的引用: Boto在ec2实例上执行shell命令

IMO, to be able to start a script inside the EC2, the script should be present on the EC2. IMO为了能够在EC2内启动脚本,该脚本应位于EC2上。

An Amazon EC2 instance is just like any computer on the Internet. Amazon EC2实例就像互联网上的任何计算机一样。 It is running an operating system (eg Linux or Windows), and it has standard security in-built. 它运行的是操作系统(例如Linux或Windows),并且具有内置的标准安全性。 The fact that it is an Amazon EC2 instance has no impact. 它是Amazon EC2实例这一事实没有影响。

So, the question really becomes: How do I run a command on a remote computer? 因此,问题实际上变成了: 如何在远程计算机上运行命令?

Typical ways of doing this include: 这样做的典型方法包括:

  • Connecting to the computer (eg via SSH) and running a command 连接到计算机(例如通过SSH)并运行命令
  • Running a service on the computer that listens on a particular port (eg responding to an API request) 在侦听特定端口的计算机上运行服务(例如,响应API请求)
  • Using remote shell commands to run an operation on another computer 使用远程Shell命令在另一台计算机上运行操作

Fortunately, AWS offers an additional option: Use the AWS Systems Manager Run Command : 幸运的是,AWS提供了一个附加选项:使用AWS Systems Manager Run Command

AWS Systems Manager Run Command lets you remotely and securely manage the configuration of your managed instances. 通过AWS Systems Manager Run Command,您可以远程安全地管理托管实例的配置。 A managed instance is any Amazon EC2 instance or on-premises machine in your hybrid environment that has been configured for Systems Manager. 受管实例是混合环境中已为Systems Manager配置的任何Amazon EC2实例或本地计算机。 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. 您可以从AWS控制台,AWS命令行界面,适用于Windows PowerShell的AWS工具或AWS开发工具包使用运行命令。 Run Command is offered at no additional cost. 免费提供运行命令。

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. 管理员使用“运行命令”在其托管实例上执行以下类型的任务: 安装或引导应用程序 ,构建部署管道,在实例从Auto Scaling组终止时捕获日志文件,以及将实例加入Windows域以进行命名一些。

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. 基本上,它是安装在实例 (或就此而言,在Internet上的任何计算机上)上的代理,并且可以将命令发送到由代理执行的计算机。 In fact, the same command can be sent to hundreds of computers if desired. 实际上,如果需要,可以将同一命令发送到数百台计算机。

The AWS Systems Manager Run Command can be triggered by an API call , such as a program using boto3. 可以通过API调用 (例如使用boto3的程序)来触发 AWS Systems Manager运行命令。

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

相关问题 使用AWS API网关触发在EC2实例上运行的python脚本并传递参数 - Use AWS API gateway to trigger python script running on EC2 instance and pass arguments 通过 AWS lambda 在 EC2 实例上执行 python 脚本 - Execute python script on EC2 instance via AWS lambda 通过HTML / Python / Bash脚本停止AWS EC2实例 - Stopping an AWS EC2 Instance through HTML/Python/Bash Script Crontab 未在 AWS EC2 Linux 实例上执行 Python3 脚本 - Crontab not executing Python3 script on AWS EC2 Linux instance Python脚本是否可以“识别”它在哪个EC2实例上运行? - Is there a way for a Python script to “recognize” which EC2 instance it is running on? 运行 python 项目的 EC2 实例上的 HTTPS - HTTPS on EC2 instance running python project Python中的Selenium PhantomJS无法在ec2实例上运行 - Selenium PhantomJS in Python not running on ec2 instance AWS EC2 实例似乎从用户数据运行错误的 Python 版本 - AWS EC2 Instance appears to be running wrong python version from User Data AWS EC2 实例启动时一次性运行 Python 代码 - One-time running Python code when AWS EC2 instance start 如何正确/安全地从 AWS SSM 参数存储中访问 EC2 实例上我的 Python 脚本的参数? - How to correctly/safely access parameters from AWS SSM Parameter store for my Python script on EC2 instance?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM