我通过从简单的bash脚本调用ec2-run-instances启动了一个EC2实例,并希望对该实例执行进一步的操作(例如,关联弹性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获得它,那就更好了。但是afaik没有EC2命令来返回最近启动的实例的id。)
发布于 2010-04-15 19:29:06
好吧,至少像这样的东西应该是有效的:
instance_id=$(ec2-run-instances ami-dd8ea5a9 [...] | awk '/INSTANCE/{print $2}') 无可否认,我有点懒,认为这样做比重新学习一些AWK基础知识更快……:-)
编辑:按照丹尼斯的建议简化了AWK的用法。此外,为了清晰起见,使用$()而不是,并且去掉了中间变量。
发布于 2011-10-13 22:34:18
作为正确答案的补充,下面是一个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 <stephane+amazon@bortzmeyer.org>
# 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}发布于 2018-10-30 20:31:06
作为ec2-run-instances的替代方案,您可以创建一个ec2实例,并通过awscli InstanceId逐行获取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)https://stackoverflow.com/questions/2644742
复制相似问题