ものぐさシリーズ、AMIを表示して選択したものからインスタンスを作成します。
vim create-test-instance.sh
#!/bin/sh
tmpfile=`mktemp`
/usr/bin/aws ec2 describe-images --filters "Name=tag-key,Values=Name" "Name=tag-key,Values=Backup-Type" "Name=tag-value,Values=${INSTANCE_NAME}" --query 'Images[].{Name:Name, ID:ImageId}' | awk '{print $2 ":" $1}' > $tmpfile
if [ ! -f $tmpfile ]; then
echo "image not found"
exit 1
fi
if [ ! -s $tmpfile ]; then
echo "image not found"
exit 1
fi
COUNT=1
for line in `cat $tmpfile`
do
echo "${COUNT} $line"
COUNT=$(( COUNT + 1 ))
done
read -p "please choose image number : " NUMBER_LOADSETTING
AMI=`sed -n -e ${NUMBER_LOADSETTING}p $tmpfile | awk -F":" '{print $2}'`
if [ "${AMI}" = "" ]; then
echo "wrong number selected"
exit 1
fi
echo $AMI""
rm -f $tmpfile
NAME={instanceName}
result=`/path/to/file/launch_from_ami.sh ${AMI} ${NAME}`
echo "instance $result created"
echo "please wait for publicip assign"
sleep 15
publicip=`/usr/bin/aws ec2 describe-instances --instance-ids $result --query 'Reservations[].Instances[].PublicIpAddress'`
echo "PublicIP: $publicip"
vim launch_from_ami.sh
#/bin/sh
AMI=$1
instanceName=$2
awsprofile=$3
if [ "$awsprofile" = "" ]; then
awsprofile=default
fi
SUBNET="{subnet}"
KEYPAIR="{keypair}"
GROUP="{securitygroup}"
if [ "$AMI" = "" ]; then
echo "usage: $0 amiID tagName"
exit 1
fi
TYPE="t2.micro"
result_id=`aws ec2 run-instances --profile ${awsprofile} --image-id ${AMI} --instance-type ${TYPE} --subnet-id ${SUBNET} --key-name ${KEYPAIR} --security-group-ids ${GROUP} | grep INSTANCE | awk '{ print $7 }'`
#echo "instance_id: $result_id created !"
if [ "$result_id" = "" ]; then
echo "instance_id not found ..."
exit 1
fi
TAGS=[{\"Key\":\"Name\",\"Value\":\"${instanceName}\"}]
if [ "$instanceName" != "" ]; then
aws ec2 create-tags --profile ${awsprofile} --resources $result_id --tags $TAGS
fi
echo $result_id


