Created
December 12, 2016 20:57
-
-
Save nandotorres/8886d5ebf1d76276d2d1660fd793df0f to your computer and use it in GitHub Desktop.
Run EC2 instance based on AMI created by packer
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 | |
EC2_NAME_SUFIX=`date "+%Y%m%d%H%M"` | |
# Query AWS if exists an AMI with TAG SHA1 = $CI_BUILD_REF | |
EXISTING_AMI=$(aws ec2 describe-images \ | |
--filter="Name=tag-key,Values=SHA1,Name=tag-value,Values=$CI_BUILD_REF") | |
# Extract the AMI ID | |
AMI_ID=$(echo $EXISTING_AMI |jq ".Images[] .ImageId" |sed -e 's/^"//' -e 's/"$//') | |
if [[ ! -z "$AMI_ID" ]] | |
then | |
echo "Run EC2 instance for AMI $AMI_ID" | |
INSTANCE_OUTPUT=$(aws ec2 run-instances \ | |
--image-id $AMI_ID \ | |
--count 1 \ | |
--instance-type t2.micro \ | |
--key-name FFTorres-US-EAST-1 \ | |
--security-group-ids sg-47862b3a \ | |
--subnet-id subnet-a71ad09b) | |
INSTANCE_ID=$(echo $INSTANCE_OUTPUT |jq ".Instances[0] .InstanceId" |sed -e 's/^"//' -e 's/"$//') | |
echo "Tagging instance $INSTANCE_ID with Name=TechTalk-TODO-App and SHA1=$CI_BUILD_REF" | |
aws ec2 create-tags --resources $INSTANCE_ID \ | |
--tags Key=Name,Value=TeckTalk-TODO-App-$EC2_NAME_SUFIX Key=SHA1,Value=$CI_BUILD_REF | |
else | |
echo "There is not an AMI tagged for SHA1 $CI_BUILD_REF" | |
exit 1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment