Boto3:创建,执行 shell 命令,在 ec2 实例上终止

Boto3: create, execute shell command, terminate on ec2 instance

我是 EC2boto 的新手。我必须创建一个 EC2 运行ning 实例,我可以在其中发送带有 S3 文件的命令并执行 shell 脚本。

我搜索了很多,找到了 botoparamiko 的方法。我不知道,是否可以使用 boto3 在 ec2 实例中 运行 一个 shell 脚本。此参考资料中的任何线索或示例都会有很大帮助。

谢谢!

boto.manage.cmdshell 模块可用于执行此操作。要使用它,您必须安装 paramiko 包。一个简单的使用示例:

import boto.ec2
from boto.manage.cmdshell import sshclient_from_instance

# Connect to your region of choice
conn = boto.ec2.connect_to_region('us-west-2')

# Find the instance object related to my instanceId
instance = conn.get_all_instances(['i-12345678'])[0].instances[0]

# Create an SSH client for our instance
#    key_path is the path to the SSH private key associated with instance
#    user_name is the user to login as on the instance (e.g. ubuntu, ec2-user, etc.)
ssh_client = sshclient_from_instance(instance,
                                     '<path to SSH keyfile>',
                                     user_name='ec2-user')
# Run the command. Returns a tuple consisting of:
#    The integer status of the command
#    A string containing the output of the command
#    A string containing the stderr output of the command
status, stdout, stderr = ssh_client.run('ls -al')

Boto 和 Boto3 之间存在差异。问题指定boto,但问题标题说Boto3。

接受的答案对于 boto 是正确的。但是,对于 boto3,这个问题得到了回答 here :)