获取使用 ec2-api-tools 新启动的实例的 ID

发布于 2024-08-29 04:10:12 字数 791 浏览 5 评论 0原文

我通过调用 < 来启动 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技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

戈亓 2024-09-05 04:10:12

为了完成您的正确答案,这里有一个 shell 脚本,它创建一个实例,运行一些命令并删除该实例。它使用 awk 的方式与你的相同。

#!/bin/sh

# Creates an Amazon EC2 virtual machine (an instance) and runs some
# shell commands on it before terminating it. Just an example.
# Stephane Bortzmeyer <[email protected]>

# Parameters you can set.
# Choose an AMI you like (ami-02103876 is a Debian "lenny")
AMI=ami-02103876
# Create your key pair first, for instance on the Web interface
KEY=test-b
KEYDIR=.
# The user name to use depends on the AMI. "root" is common but check
# the documentation of the AMI.
USERNAME=root
# Needs to be a legal Unix group of commands
COMMANDS="(uname -a; df -h; cat /etc/debian_version)"
MAX_CONNECTS=4
MAX_TESTS=6

# If you want to change from the default region, set the environment
# variable EC2_URL for instance 'export
# EC2_URL=https://ec2.eu-west-1.amazonaws.com' to use the 'eu-west-1'
# region

# Also, be sure your default security group allows incoming SSH.

if [ "${EC2_PRIVATE_KEY}" = "" ] || [ "${EC2_CERT}" = "" ]; then
    echo "You need to have X.509 certificate and private key locally, and to set the environment variables EC2_PRIVATE_KEY and EC2_CERT to indicate their locations" 1>&2
    exit 1
fi

start=$(ec2-run-instances --key ${KEY} $AMI)
if [ $? != 0 ]; then
    echo "Machine did not start" 1>&2
    exit 1
fi

AMI_E=$(echo "$start" | awk '/^INSTANCE/ {print $3}')
if [ "$AMI_E" != "$AMI" ]; then
    echo "AMI does not match (got $AMI_E instead of $AMI), the machine probably did not start" 1>&2
    exit 1
fi
INSTANCE=$(echo "$start" | awk '/^INSTANCE/ {print $2}')

# I do not find a way to block until the machine is ready. We

# apparently have to poll.
OVER=0
TESTS=0
while [ $OVER != 1 ] && [ $TESTS -lt $MAX_TESTS ]; do
    description=$(ec2-describe-instances ${INSTANCE})
    STATE=$(echo "$description" | awk '/^INSTANCE/ {print $6}')
    NAME=$(echo "$description" | awk '/^INSTANCE/ {print $4}')
    if [ "$NAME" = "" ]; then
        echo "No instance ${INSTANCE} available. Crashed or was terminated." 1>&2
        exit 1
    fi
    if [ $STATE = "running" ]; then
        OVER=1
    else
        # I like bc but 'echo $(( TESTS+=1 ))' should work, too. Or expr.
        TESTS=$(echo $TESTS+1 | bc)
        sleep 2
    fi
done
if [ $TESTS = $MAX_TESTS ]; then
    echo "${INSTANCE} never got to running state" 1>&2
    ec2-terminate-instances ${INSTANCE}
    exit 1
fi
echo "$INSTANCE is running, name is $NAME"
# The SSH server does not seem reachable immediately. We again have to poll
OVER=0
TESTS=0
while [ $OVER != 1 ] && [ $TESTS -lt $MAX_CONNECTS ]; do
    ssh -o "StrictHostKeyChecking no" -i ${KEYDIR}/${KEY}.pem ${USERNAME}@$NAME "${COMMANDS}"
    if [ $? != 255 ]; then
        # It means we connected successfully (even if the remote command failed)
        OVER=1
    else
        TESTS=$(echo $TESTS+1 | bc)
        sleep 3
    fi
done
if [ $TESTS = $MAX_CONNECTS ]; then
    echo "Cannot connect to ${NAME}" 1>&2
fi
ec2-terminate-instances ${INSTANCE}

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.

#!/bin/sh

# Creates an Amazon EC2 virtual machine (an instance) and runs some
# shell commands on it before terminating it. Just an example.
# Stephane Bortzmeyer <[email protected]>

# Parameters you can set.
# Choose an AMI you like (ami-02103876 is a Debian "lenny")
AMI=ami-02103876
# Create your key pair first, for instance on the Web interface
KEY=test-b
KEYDIR=.
# The user name to use depends on the AMI. "root" is common but check
# the documentation of the AMI.
USERNAME=root
# Needs to be a legal Unix group of commands
COMMANDS="(uname -a; df -h; cat /etc/debian_version)"
MAX_CONNECTS=4
MAX_TESTS=6

# If you want to change from the default region, set the environment
# variable EC2_URL for instance 'export
# EC2_URL=https://ec2.eu-west-1.amazonaws.com' to use the 'eu-west-1'
# region

# Also, be sure your default security group allows incoming SSH.

if [ "${EC2_PRIVATE_KEY}" = "" ] || [ "${EC2_CERT}" = "" ]; then
    echo "You need to have X.509 certificate and private key locally, and to set the environment variables EC2_PRIVATE_KEY and EC2_CERT to indicate their locations" 1>&2
    exit 1
fi

start=$(ec2-run-instances --key ${KEY} $AMI)
if [ $? != 0 ]; then
    echo "Machine did not start" 1>&2
    exit 1
fi

AMI_E=$(echo "$start" | awk '/^INSTANCE/ {print $3}')
if [ "$AMI_E" != "$AMI" ]; then
    echo "AMI does not match (got $AMI_E instead of $AMI), the machine probably did not start" 1>&2
    exit 1
fi
INSTANCE=$(echo "$start" | awk '/^INSTANCE/ {print $2}')

# I do not find a way to block until the machine is ready. We

# apparently have to poll.
OVER=0
TESTS=0
while [ $OVER != 1 ] && [ $TESTS -lt $MAX_TESTS ]; do
    description=$(ec2-describe-instances ${INSTANCE})
    STATE=$(echo "$description" | awk '/^INSTANCE/ {print $6}')
    NAME=$(echo "$description" | awk '/^INSTANCE/ {print $4}')
    if [ "$NAME" = "" ]; then
        echo "No instance ${INSTANCE} available. Crashed or was terminated." 1>&2
        exit 1
    fi
    if [ $STATE = "running" ]; then
        OVER=1
    else
        # I like bc but 'echo $(( TESTS+=1 ))' should work, too. Or expr.
        TESTS=$(echo $TESTS+1 | bc)
        sleep 2
    fi
done
if [ $TESTS = $MAX_TESTS ]; then
    echo "${INSTANCE} never got to running state" 1>&2
    ec2-terminate-instances ${INSTANCE}
    exit 1
fi
echo "$INSTANCE is running, name is $NAME"
# The SSH server does not seem reachable immediately. We again have to poll
OVER=0
TESTS=0
while [ $OVER != 1 ] && [ $TESTS -lt $MAX_CONNECTS ]; do
    ssh -o "StrictHostKeyChecking no" -i ${KEYDIR}/${KEY}.pem ${USERNAME}@$NAME "${COMMANDS}"
    if [ $? != 255 ]; then
        # It means we connected successfully (even if the remote command failed)
        OVER=1
    else
        TESTS=$(echo $TESTS+1 | bc)
        sleep 3
    fi
done
if [ $TESTS = $MAX_CONNECTS ]; then
    echo "Cannot connect to ${NAME}" 1>&2
fi
ec2-terminate-instances ${INSTANCE}
红焚 2024-09-05 04:10:12

好的,至少这样的东西应该有效:

instance_id=$(ec2-run-instances ami-dd8ea5a9 [...] | awk '/INSTANCE/{print $2}') 

不可否认,我有点懒惰,认为询问 SO 比重新学习一些 AWK 基础知识更快......:-)

编辑:简化 AWK 用法为 Dennis建议。另外,为了清楚起见,使用 $() 而不是 `` ,并摆脱了中间变量。

Ok, at least something like this should work:

instance_id=$(ec2-run-instances ami-dd8ea5a9 [...] | awk '/INSTANCE/{print $2}') 

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.

深居我梦 2024-09-05 04:10:12

作为 ec2-run-instances 的替代方案,您可以创建一个 ec2 实例并通过 awscli 运行实例

export MyServerID=$(aws ec2 run-instances --image-id AMI --count 1 --instance-type t2.micro --key-name "my_ssh_key" --security-group-ids sg-xxx --subnet-id subnet-yyy --query 'Instances[0].InstanceId' --output text)

As an alternative to ec2-run-instances, you can create one ec2 instance and get InstanceId by one line by awscli run-instances:

export MyServerID=$(aws ec2 run-instances --image-id AMI --count 1 --instance-type t2.micro --key-name "my_ssh_key" --security-group-ids sg-xxx --subnet-id subnet-yyy --query 'Instances[0].InstanceId' --output text)
冷了相思 2024-09-05 04:10:12

无需使用 awk:

# create the instance and capture the instance id
echo "Launching instance..."
instanceid=$(ec2-run-instances --key $pemkeypair --availability-zone $avzone $ami | egrep ^INSTANCE | cut -f2)
if [ -z "$instanceid" ]; then
    echo "ERROR: could not create instance";
    exit;
else
    echo "Launched with instanceid=$instanceid"
fi

来自 http://www.hulen。 com/post/22802124410/unattended-amazon-ec2-install-script

No need to use awk:

# create the instance and capture the instance id
echo "Launching instance..."
instanceid=$(ec2-run-instances --key $pemkeypair --availability-zone $avzone $ami | egrep ^INSTANCE | cut -f2)
if [ -z "$instanceid" ]; then
    echo "ERROR: could not create instance";
    exit;
else
    echo "Launched with instanceid=$instanceid"
fi

from http://www.hulen.com/post/22802124410/unattended-amazon-ec2-install-script

迷荒 2024-09-05 04:10:12
http://www.tothenew.com/blog/how-to-parse-json-by-command-line-in-linux/
best tool to parse json in shell

#get instance id 
cat sample.json | jq '.Instances[0].InstanceId'|sed -e 's/^"//' -e 's/"$//' 
#check instances is running or not 
cat status.json | jq '.InstanceStatuses[0].InstanceState.Name'|sed -e 's/^"//' -e 's/"$//' 
http://www.tothenew.com/blog/how-to-parse-json-by-command-line-in-linux/
best tool to parse json in shell

#get instance id 
cat sample.json | jq '.Instances[0].InstanceId'|sed -e 's/^"//' -e 's/"$//' 
#check instances is running or not 
cat status.json | jq '.InstanceStatuses[0].InstanceState.Name'|sed -e 's/^"//' -e 's/"$//' 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文