使用BOTO3 SSM上传没有SSH KeyPair的文件到EC2实例

发布于 2025-02-13 23:36:49 字数 767 浏览 1 评论 0原文

BOTO3的SSM客户端使用AWS配置设置作为身份验证机制,允许Python程序在远程EC2实例上运行命令。

我想将文件上传到EC2实例。上一篇问题(如何使用BOTO3通过SSM代理到EC2实例并发送文件)表示这是可以通过SSH进行的。

是否可以在没有SSH Keypair的情况下使用SSM上传文件?

做到这一点的一种方法可能是:

with open('path/to/file', r) as f:
  contents = f.read()

resp = boto3('ssm').send_command(
  InstanceIds=[...],
  Commands=[f'echo "{contents}" > file.txt']
)

但这似乎很脆弱。

上下文:我正在构建一个本应由非技术用户运行的脚本。该脚本设置了一个新的EC2实例,并在该实例上以编程方式运行多个命令来设置HTTP服务器。据我所知,没有一种很好的方法可以自动生成SSH Keypair,而且我不想在每个部署的EC2实例中手动管理多个SSH Keypairs。

The SSM client for boto3 uses AWS config settings as an authentication mechanism, allowing a python program to run commands on a remote ec2 instance.

I would like to upload files to the ec2 instance. Previous SO questions (How to scp to ec2 instance via ssm agent using boto3 and send file) indicate that this is possible over ssh.

Is it possible to upload files to the instance using SSM without an SSH keypair?

One way to do this may be something like:

with open('path/to/file', r) as f:
  contents = f.read()

resp = boto3('ssm').send_command(
  InstanceIds=[...],
  Commands=[f'echo "{contents}" > file.txt']
)

but this seems very fragile.

Context: I am building a script that is meant to be run by non-technical users. The script sets up a new EC2 instance and programmatically runs several commands on that instance to set up a http server. As far as I know, there is not a good way to automatically generate ssh keypairs, and I dont want to have to manually manage multiple ssh keypairs for every ec2 instance that is deployed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

抽个烟儿 2025-02-20 23:36:49

并不是一个答案,但是我最终提出了 @Markb的建议,将S3文件作为中介创建并从那里上传/下载。

Not quite an answer, but I ended up going with @MarkB's suggestion to create an S3 file as an intermediary and upload/download from there.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文