Created
October 11, 2014 15:45
-
-
Save kubihie/c7c1fe3db2c65ef621ad to your computer and use it in GitHub Desktop.
userdata.txt:https://gist.github.com/kubihie/7c77b1f6dd10476eaa50
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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.' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment