How can i automate script executions in aws EC2 using go sdk?

huangapple go评论90阅读模式
英文:

How can i automate script executions in aws EC2 using go sdk?

问题

我正在使用Go SDK构建一个管理多个EC2实例的应用程序。我想以自动化的方式在这些实例上运行脚本。

我该如何实现这一点?我不认为os.command => ssh => 在代码中以字符串形式存储的原始脚本是最佳实践。有没有更好的方法来实现这个?

谢谢。

英文:

I'm building an app that manages multiple ec2 instances using the go sdk. I would like to run scripts on these instances in an automated way.

How can I achieve that ? I don't think os.command => ssh => raw script stored as string in code is the best practice. Is there any clean way to achieve this ?
Thanks

答案1

得分: 1

有没有一种简洁的方法来实现这个?

要引导您的实例,您可以创建一个UserData脚本。该脚本仅在实例启动后运行一次。

对于其他远程执行命令的情况,您可以使用SSM Run Command在单个或多个实例上运行命令。

英文:

> Is there any clean way to achieve this ?

To bootstrap your instance, you would create a UserData script. The script runs only once, just after your instance is launched.

For other execution of commands remotely, you can use SSM Run Command to run command on a single or multiple instances.

答案2

得分: 1

你建议的方法实际上是有效的,可以工作。虽然我同意你的观点,但这不会是我的首选。我会使用标准库中的golang.org/x/crypto/ssh包,或者使用像github.com/appleboy/easyssh-proxy这样的外部解决方案。

我更倾向于使用默认库,但如果你没有偏好的话,后者包中的Scp函数可能对你特别有兴趣。你可以在该项目的自述文件中找到相关示例。

安全拷贝协议(SCP)是一种在本地主机和远程主机之间,或者在两个远程主机之间安全传输计算机文件的方法。它基于安全外壳(SSH)协议。

编辑:在看到马尔钦的答案之后,我认为我的答案更偏向于纯SSH的解决方案,与AWS无关。如果你想要AWS的惯用解决方案,请务必查看他提供的建议!

英文:

The way you suggest is actually valid and can work. I agree with you though, it wouldn't be my first choice either. I would either use the package golang.org/x/crypto/ssh in the standard library or an external solution like github.com/appleboy/easyssh-proxy.

I would lean towards the default library but if you don't have a preference there then the Scp function of the latter package might be especially of interest to you. You can find examples of this in the readme of the project.

> Secure copy protocol (SCP) is a means of securely transferring computer files between a local host and a remote host or between two remote hosts. It is based on the Secure Shell (SSH) protocol.

EDIT: After seeing Marcin's answer, I think my answer is more the plain SSH answer, AWS independent. For the idiomatic answer for AWS please definitely look at his suggested solution!

huangapple
  • 本文由 发表于 2021年9月8日 14:12:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/69097796.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定