如何触发 EC2 实例并在每个实例上上传/运行启动脚本? [英] How to fire EC2 instances and upload/run a startup script on each of them?

查看:25
本文介绍了如何触发 EC2 实例并在每个实例上上传/运行启动脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想自动启动一组 Linux EC2 实例.

I want to automate the launch of a set of Linux EC2 instances.

基本上,我想编写一个脚本/程序:

Basically, I want to write a script/program that would :

  • 实例化我的给定 AMI 的 N 次出现.
  • 对于每个启动的实例,它会上传一个定制的脚本并让脚本运行到实例中.

使用 VMWare,我通常会使用 vmrun 或 Vix SDK.

Using VMWare, I would typically do that using vmrun or the Vix SDK.

Amazon AWS/EC2 中有哪些选项?

What are the options in Amazon AWS/EC2?

推荐答案

答案在一定程度上取决于您正在运行的 AMI,因为所提供的功能完全取决于 AMI.

The answer depends a bit on what AMI you are running as the features provided are entirely AMI dependent.

Amazon Linux AMIS 和官方 Ubuntu AMI 已安装 cloud-init 软件包.这有多种方法可以触发启动操作,但最符合您的请求的方法(也是我最喜欢的,因为我发明了它)是 用户数据脚本.

The Amazon Linux AMIS and the official Ubuntu AMIs have the cloud-init package installed. This has a number of ways you can trigger startup actions, but the one that matches your request most closely (and my favorite because I invented it) is the concept of a user-data script.

您可以在启动 EC2 实例时简单地将任何脚本(以两个字符 #! 开头)作为用户数据传递.它将在实例第一次启动时以 root 身份运行.

You can simply pass any script (starting with the two characters #!) as the user-data when starting the EC2 instances. It will be run as root on the first boot of the instance.

有关其工作原理的具体示例,我在最近的文章中使用了这种确切的技术:在 EC2 用户数据脚本中上传已知的 ssh 主机密钥

For a specific example of how this works, I use this exact technique in my recent article: Uploading Known ssh Host Key in EC2 user-data Script

您还想使用相同的脚本运行多个 EC2 实例.ec2-run-instances 命令以及相关的 API 和 Web 控制台允许您指定任意数量的实例以使用相同的用户数据启动.例如:

You also wanted to run more than one EC2 instance with the same script. The ec2-run-instances command and the related APIs and web console allow you to specify any number of instances to start with the same user-data. For example:

ec2-run-instances            
  --instance-count 10        
  --user-data-file $MYSCRIPT 
  --key $USER                
  $SOMEAMI

如果您当前正在运行未安装 cloud-init 的 AMI,您可以执行以下操作之一:

If you are currently running an AMI that does not have cloud-init installed, you could do one of:

  • 切换到安装了 cloud-init 的 AMI,或

  • Switch to an AMI that has cloud-init installed, or

构建已安装 cloud-init 的自定义 AMI 版本,或

Build a custom version of your AMI that has cloud-init installed, or

编写一个更复杂的封装脚本,在实例启动后记录所有实例 ID,等待所有实例进入运行状态,等待 sshd 接受连接,上传您的启动脚本到每个实例,并在每个实例上运行启动脚本.

Write a more complicated wrapper script that makes a record of all of the instance ids after they are kicked off, waits for all of the instances to move to the running state, waits for the sshd to accept connections, uploads your startup script to each instance, and runs the startup script on each instance.

这篇关于如何触发 EC2 实例并在每个实例上上传/运行启动脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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