Last active
October 8, 2015 12:17
-
-
Save instaBOT/54cba4a6d9759f499da2 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env bash | |
cd | |
set -e | |
#################### | |
# parameters | |
#################### | |
# sg-167cec72: security group for dev environment in EC2 | |
vpc_security_group_id="sg-167cec72" | |
# subnet-35760642: subnet for VPC 'vpc-89b6fbec' in 'us-west-2a' | |
# subnet-ef7d2f8a: subnet for VPC 'vpc-89b6fbec' in 'us-west-2b' | |
# subnet-a9901af0: subnet for VPC 'vpc-89b6fbec' in 'us-west-2c' | |
subnet_id="subnet-a9901af0" | |
# t2.micro: the cheapest type | |
instance_type="t2.micro" | |
# dev-env-key: the one for the dev environment | |
key_name="dev-env-key" | |
# ami-4cbc5b7f: our custom image named "ubuntu-14.04-base-5" | |
ami_id="ami-4cbc5b7f" | |
if [ "$1" != "" ]; then | |
ami_id=$1 | |
fi | |
#################### | |
# create instance | |
#################### | |
echo "creating instance from AMI $ami_id ..." | |
instance_id=$(\ | |
aws ec2 run-instances --image-id $ami_id --count 1 --instance-type $instance_type \ | |
--key-name $key_name --security-group-ids $vpc_security_group_id --subnet-id $subnet_id \ | |
--associate-public-ip-address \ | |
--query 'Instances[0].InstanceId' --output text) | |
echo "instance id: $instance_id" | |
# e.g. i-02c38cc4 | |
echo "waiting for its public DNS..." | |
aws ec2 wait instance-running --instance-ids $instance_id | |
instance_public_dns=$(\ | |
aws ec2 describe-instances --instance-ids $instance_id \ | |
--query 'Reservations[0].Instances[0].PublicDnsName' --output text) | |
echo "instance public DNS: $instance_public_dns" | |
# e.g. ec2-52-26-149-233.us-west-2.compute.amazonaws.com | |
# add tag env=dev for the instance just created | |
aws ec2 create-tags --resources $instance_id --tags Key=env,Value=dev | |
echo "" | |
echo "command for connecting to instance (sometimes you have to wait a little before it works):" | |
echo " ssh -i ~/.ssh/$key_name.pem ubuntu@$instance_public_dns" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment