与EC2-API的工具,新推出的一个实例获取ID [英] Getting ID of an instance newly launched with ec2-api-tools
问题描述
我启动EC2实例,通过调用<一href="http://docs.amazonwebservices.com/AWSEC2/latest/CommandLineReference/index.html?ApiReference-cmd-RunInstances.html"><$c$c>ec2-run-instances$c$c>从简单的bash脚本,并希望在该实例上进行进一步的操作(如准弹性IP),为此,我需要的实例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.
该命令是一样的东西 EC2-运行情况AMI-dd8ea5a9 -K pk.pem -C cert.pem --region欧盟 - 西-1 -t -n c1.medium 1
,它的输出:
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
在这个例子中, I-945af9e3
的ID,我后。
In this example, i-945af9e3
is the id I'm after.
所以,我需要一个简单的方法来分析,从该命令返回的ID - 你将如何去这样做呢?我AWK是有点生疏......随意使用任何工具可以在一个典型的Linux机器。 (如果有一种方式来获得它直接使用EC2-API的工具,那就更好了。但AFAIK没有EC2命令,例如返回最近推出的实例的ID。)
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.)
推荐答案
好吧,至少这样的事情应该工作:
Ok, at least something like this should work:
instance_id=$(ec2-run-instances ami-dd8ea5a9 [...] | awk '/INSTANCE/{print $2}')
诚然我是有点懒的思维,它是更快地问就因此比重新学习一些基本的AWK ...: - )
Admittedly I was a bit lazy thinking that it's quicker to ask on SO than to relearn some AWK basics... :-)
修改:简化AWK用法丹尼斯建议。此外,使用 $()
,而不是``为了清楚起见,并摆脱了中间变量。
Edit: simplified AWK usage as Dennis suggested. Also, using $()
instead of `` for clarity, and got rid of intermediate variable.
这篇关于与EC2-API的工具,新推出的一个实例获取ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!