-
Notifications
You must be signed in to change notification settings - Fork 2
/
tf_instance.sh
executable file
·57 lines (50 loc) · 1.56 KB
/
tf_instance.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/usr/bin/env bash
# Use AWS CLI to get the most recent version of an AMI that
# matches certain criteria. Has obvious uses. Made possible via
# --query, --output text, and the fact that RFC3339 datetime
# fields are easily sortable.
set -e
print_usage() {
echo "USAGE: $0 launch|list|terminate [ubuntu|aws|aws2]"
}
SIZE=t2.micro
LOCAL_KEY=~/.ssh/id_rsa.pub
#SG_NAME=ssh-only-sg
# Pull the region name from the AWS_DEFAULT_REGION if set (else terrafrom.tf defaults to us-east-1)
if [ ! -z $AWS_DEFAULT_REGION ]; then
REGION_PARM=" -var region=$AWS_DEFAULT_REGION "
fi
# Pull the aws named profile name from the AWS_PROFILE if set (else terrafrom.tf defaults to default)
if [ ! -z $AWS_PROFILE ]; then
PROFILE_PARM=" -var profile=$AWS_PROFILE "
fi
case $1 in
launch )
# Check to make sure the local ssh key exists
if [ ! -f $LOCAL_KEY ]; then
# If not, create it
ssh-keygen -f id_rsa -t rsa -N ''
fi
# Get our public IP address
MY_IP="`curl -s https://ipinfo.io/ip`/32"
# Initialize terraform
terraform init
# Run the apply
terraform apply -var "os=$2" -var "allowed_cidr=$MY_IP" $REGION_PARM $PROFILE_PARM -auto-approve
;;
list )
# List the output vars
terraform output
;;
terminate )
# Destroy the resources
terraform destroy $PROFILE_PARM -auto-approve
# Clean up the state files
rm -rf .terraform
rm terraform.tfstate*
;;
* )
print_usage
exit 1
;;
esac