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.)
No need to use awk:
from http://www.hulen.com/post/22802124410/unattended-amazon-ec2-install-script
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.
As an alternative to
ec2-run-instances
, you can create one ec2 instance and get InstanceId by one line by awscli run-instances: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.