aws-cliでEC2インスタンスを起動する

  • このエントリーをはてなブックマークに追加

aws-cliのコマンド群は、オプションへ値を渡すやり方がバラバラで普通に文字列でvalueを渡せばOKだったりKey=hoge,value=fugaで渡さなければならなかったりjsonで渡さなければならなかったりしていて、実際に動かそうとすると試行錯誤をするはめになるのでサンプルを参考として残しておきます。

公式ドキュメント(http://docs.aws.amazon.com/cli/latest/reference/ec2/run-instances.html)

 

 

ec2 run-instances(EIPを付与する場合)

userdata.txtは「AmazonLinuxでcloud-initを使う」のものを使っています。

#!/bin/bash

IMAGE_ID='--image-id=ami-35072834'
USER_DATA='--user-data=file://userdata.txt'
INSTANCE_TYPE='--instance-type=t2.micro'
COUNT='--count=1'

KEY_NAME='--key-name=[[Key Pair Name]]'
SECURITY_GROUP_IDS='--security-group-ids=[[Security Group ID(sg-xxxxxxxx)]]'
MONITORING='--monitoring=Enabled=[[true/false]]'
SUBNET_ID='--subnet-id=[[Subnet ID(subnet-xxxxxxxx)]]'
IAM_INSTANCE_PROFILE='--iam-instance-profile=Arn=[[Instance Profile ARN(arn:aws:iam:******:instance-profile/xxxxxxxxxxxxxxx)]]'
TAGS='--tags=Key=[[Name]],Value=[[Value]]'
#EIP
EIPALLOCATIONID='--allocation-id=i[[EIP Allocation ID(eipalloc-xxxxxxxx)]]'

#Launch Instance
RET=$(aws ec2 run-instances "${IMAGE_ID}" "${KEY_NAME}" "${SECURITY_GROUP_IDS}" "${USER_DATA}" "${INSTANCE_TYPE}" "${MONITORING}" "${SUBNET_ID}" "${IAM_INSTANCE_PROFILE}" "${COUNT}" 2>&1)

if [ $? -ne 0 ]; then
 echo "Run-instances is failed."
 exit 1
fi
echo "Instance launched."
#echo ${RET}

#Getting instance-id
INSTANCEID=$(echo ${RET} | jq ".Instances[].InstanceId" | sed -e "s/\"//g")

if [ ! "${INSTANCEID}" ]; then
 echo "Getting instance-id is failed."
 exit 1
fi
echo "EC2 instance-id=${INSTANCEID}"

#Create tags
RET=$(aws ec2 create-tags --resources "${INSTANCEID}" "${TAGS}" 2>&1)
if [ $? -ne 0 ]; then
 echo "Create tags is failed."
 exit 1
fi
#echo ${RET}

#Setting EIP
for i in $(seq 1 20)
do
 sleep 5
 RET=$(aws ec2 associate-address --instance-id ${INSTANCEID} ${EIPALLOCATIONID} 2>&1)
# echo ${RET}
 echo "${RET}" | grep 'true' > /dev/null
 if [ $? -eq 0 ]; then
  break
 fi
 echo "Retry $i..."
 if [ $i -ge 20 ]; then
  echo 'Setting EIP failed.'
  exit 1
 fi
done

echo 'End.'

   

ec2 run-instances(自動でPublicIPを付与する場合)

#!/bin/bash

IMAGE_ID='--image-id=ami-35072834'
USER_DATA='--user-data=file://userdata.txt'
INSTANCE_TYPE='--instance-type=t2.micro'
COUNT='--count=1'

KEY_NAME='--key-name=[[Key Pair Name]]'
SECURITY_GROUP_IDS='--security-group-ids=[[Security Group ID(sg-xxxxxxxx)]]'
MONITORING='--monitoring=Enabled=[[true/false]]'
SUBNET_ID='--subnet-id=[[Subnet ID(subnet-xxxxxxxx)]]'
IAM_INSTANCE_PROFILE='--iam-instance-profile=Arn=[[Instance Profile ARN(arn:aws:iam:******:instance-profile/xxxxxxxxxxxxxxx)]]'
TAGS='--tags=Key=[[Name]],Value=[[Value]]'
#Auto Public IP
NETWORK_INTERFACES='--network-interfaces=[ { "DeviceIndex": 0, "AssociatePublicIpAddress": true } ]'

#Launch Instance
RET=$(aws ec2 run-instances "${IMAGE_ID}" "${KEY_NAME}" "${SECURITY_GROUP_IDS}" "${USER_DATA}" "${INSTANCE_TYPE}" "${MONITORING}" "${SUBNET_ID}" "${IAM_INSTANCE_PROFILE}" "${COUNT}" "${NETWORK_INTERFACES}" 2>&1)

if [ $? -ne 0 ]; then
 echo "Run-instances is failed."
 exit 1
fi
echo "Instance launched."
#echo ${RET}

#Getting instance-id
INSTANCEID=$(echo ${RET} | jq ".Instances[].InstanceId" | sed -e "s/\"//g")

if [ ! "${INSTANCEID}" ]; then
 echo "Getting instance-id is failed."
 exit 1
fi
echo "EC2 instance-id=${INSTANCEID}"

#Create tags
RET=$(aws ec2 create-tags --resources "${INSTANCEID}" "${TAGS}" 2>&1)
if [ $? -ne 0 ]; then
 echo "Create tags is failed."
 exit 1
fi
#echo ${RET}

#Getting Auto Public IP
for i in $(seq 1 20)
do
 sleep 5
 RET=$(aws ec2 describe-instances --instance-ids ${INSTANCEID})
# echo ${RET}
 PUBLICIP=$(echo "${RET}" | jq ".Reservations[].Instances[].NetworkInterfaces[].Association.PublicIp" | sed -e "s/\"//g")
 if [ "${PUBLICIP}" ]; then
  break
 fi
 echo "Retry $i..."
 if [ $i -ge 20 ]; then
  echo 'Getting Auto Public IP failed.'
  exit 1
 fi
done
echo "EC2 Public IP=${PUBLICIP}."

echo 'End.'

 

aws-cliのバージョンが上がると変わっていくのが辛いです。。