获取使用 ec2-api-tools 新启动的实例的 ID
我通过调用 < 来启动 EC2 实例code>ec2-run-instances 来自简单的 bash 脚本,并且想要对该实例执行进一步的操作(例如关联弹性 IP),为此我需要实例 ID。
该命令类似于 ec2-run-instances ami-dd8ea5a9 -K pk.pem -C cert.pem --region eu-west-1 -t c1.medium -n 1 及其输出:
RESERVATION r-b6ea58c1 696664755663 default
INSTANCE i-945af9e3 ami-dd8ea5b9 pending 0 c1.medium 2010-04-15T10:47:56+0000 eu-west-1a aki-b02a01c4 ari-39c2e94d
在此示例中,i-945af9e3
是我要查找的 id。
所以,我需要一种简单的方法来从命令返回的内容中解析 id - 你会如何去做呢?我的 AWK 有点生疏了...请随意使用典型 Linux 机器上可用的任何工具。 (如果有办法直接使用 EC2-API-tools 获取它,那就更好了。但是据我所知,没有 EC2 命令可以返回最近启动的实例的 id。)
I'm launching an EC2 instance, by invoking ec2-run-instances
from simple a bash script, and want to perform further operations on that instance (e.g. associate elastic IP), for which I need the instance id.
The command is something like ec2-run-instances ami-dd8ea5a9 -K pk.pem -C cert.pem --region eu-west-1 -t c1.medium -n 1
, and its output:
RESERVATION r-b6ea58c1 696664755663 default
INSTANCE i-945af9e3 ami-dd8ea5b9 pending 0 c1.medium 2010-04-15T10:47:56+0000 eu-west-1a aki-b02a01c4 ari-39c2e94d
In this example, i-945af9e3
is the id I'm after.
So, I'd need a simple way to parse the id from what the command returns - how would you go about doing it? My AWK is a little rusty... Feel free to use any tool available on a typical Linux box. (If there's a way to get it directly using EC2-API-tools, all the better. But afaik there's no EC2 command to e.g. return the id of the most recently launched instance.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
为了完成您的正确答案,这里有一个 shell 脚本,它创建一个实例,运行一些命令并删除该实例。它使用 awk 的方式与你的相同。
Completing your correct answer, here is a shell script which creates an instance, runs some commands and deletes the instance. It uses awk in the same way as yours.
好的,至少这样的东西应该有效:
不可否认,我有点懒惰,认为询问 SO 比重新学习一些 AWK 基础知识更快......:-)
编辑:简化 AWK 用法为 Dennis建议。另外,为了清楚起见,使用
$()
而不是 `` ,并摆脱了中间变量。Ok, at least something like this should work:
Admittedly I was a bit lazy thinking that it's quicker to ask on SO than to relearn some AWK basics... :-)
Edit: simplified AWK usage as Dennis suggested. Also, using
$()
instead of `` for clarity, and got rid of intermediate variable.作为
ec2-run-instances
的替代方案,您可以创建一个 ec2 实例并通过 awscli 运行实例:As an alternative to
ec2-run-instances
, you can create one ec2 instance and get InstanceId by one line by awscli run-instances:无需使用 awk:
来自 http://www.hulen。 com/post/22802124410/unattended-amazon-ec2-install-script
No need to use awk:
from http://www.hulen.com/post/22802124410/unattended-amazon-ec2-install-script