-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcreate-ec2-machine-database.sh
More file actions
executable file
·61 lines (51 loc) · 1.9 KB
/
create-ec2-machine-database.sh
File metadata and controls
executable file
·61 lines (51 loc) · 1.9 KB
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
58
59
60
61
#!/bin/bash -e
# Parameters:
# - $1 : Instance's name
# - $2 : Path to instance profile
# Result:
# - Launch an EC2 instance named $1, with the specs/profile provided in the file located at $2
# - Output the public IP address of the instance
if [ "$#" -ne 2 ]; then
echo "Must provide 2 arguments as (a) instance tag=Name and (b) path to instance profile script"
echo "e.g. ./create-ec2-machine-database.sh HackO-Developer-Database ec2-profile-database-developer.sh"
exit 1
fi
# Source EC2 specs from a separate file
# The ./ec2-profile.sh should contain the following variables
# DEVICENAME='/dev/sdb'
# IMAGEID='ami-7f43f307'
# INSTANCETYPE='t2.micro'
# KEYNAME='hackoregon-2018-database-dev-env'
# REGION='us-west-2'
# SECURITYGROUPIDS='sg-28154957'
# SUBNETID='subnet-8794fddf'
# VOLUMESIZE='8'
EC2PROFILE=$2
INSTANCE_ID=
INSTANCE_ID_FILE='./tmp_instance_id'
INSTANCE_NAME=$1
TAG_SPECS='ResourceType=instance,Tags=[{Key=Name,Value='$INSTANCE_NAME'}]'
source $EC2PROFILE
echo "Launching the ec2 "\"$INSTANCE_NAME\"" instance..."
aws ec2 run-instances \
--image-id $IMAGEID \
--count 1 \
--instance-type $INSTANCETYPE \
--key-name $KEYNAME \
--security-group-ids $SECURITYGROUPIDS \
--subnet-id $SUBNETID\
--region $REGION \
--block-device-mappings "[{\"DeviceName\":\"/dev/sdb\",\"Ebs\":{\"VolumeSize\":8,\"VolumeType\":\"gp2\",\"DeleteOnTermination\":true}}]" \
--tag-specifications $TAG_SPECS \
--query 'Instances[0].InstanceId' \
> $INSTANCE_ID_FILE
echo "New instance'id: "
cat $INSTANCE_ID_FILE
echo "Getting the public ip address of the new instance..."
INSTANCE_ID=$( cat $INSTANCE_ID_FILE )
INSTANCE_ID=$( echo $INSTANCE_ID | cut -c 2- | rev | cut -c 2- | rev ) # trim off the surrounding double quotes
echo "instance id= "$INSTANCE_ID
aws ec2 describe-instances \
--instance-ids $INSTANCE_ID \
--query 'Reservations[0].Instances[0].PublicIpAddress'
rm -f $INSTANCE_ID_FILE