Created
October 11, 2014 16:21
-
-
Save kubihie/1e75b1e9ef84fa28309c 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]]' | |
#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.' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment