-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathstart.sh
More file actions
executable file
·244 lines (193 loc) · 8.03 KB
/
start.sh
File metadata and controls
executable file
·244 lines (193 loc) · 8.03 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#!/bin/bash
set -e
# TODO: This script can be simplified by replacing the json_get function with the --query option of aws cli (http://docs.aws.amazon.com/cli/latest/userguide/controlling-output.html#controlling-output-filter)
export TAG_NAME="example-script"
export SCRIPT_DIR=$(realpath $(dirname $0))
function json_get {
python -c "import json, sys; data=json.load(sys.stdin); print data$1;"
}
function tag {
aws ec2 create-tags --tags "Key=Name,Value=$TAG_NAME" --resources "$1"
}
function get_ids {
ALL_TAGS=$(aws ec2 describe-tags --filters "Name=value,Values=$TAG_NAME" "Name=resource-type,Values=$1")
echo "$ALL_TAGS" | python -c "import json, sys; data=json.load(sys.stdin); print ' '.join([tag['ResourceId'] for tag in data['Tags']])"
}
function create_or_get {
IDs=$(get_ids $1)
NUMBER_OF_IDs=$(echo "$IDs" | wc -w)
if [[ $NUMBER_OF_IDs -eq 0 ]]
then
result=$($3 | json_get "$2")
tag "$result"
echo "$result"
elif [[ $NUMBER_OF_IDs -eq 1 ]]
then
echo $IDs
else
echo "Error: More than one $1 with tag $TAG_NAME found." 2>&1
exit -1
fi
}
if [ ! -e ~/.aws/credentials ]
then
echo "AWS is not configured." >&2
echo "Run 'aws configure' to set the aws credentials." >&2
exit -1
fi
#########################################################################
echo -n "Creating VPC... "
#########################################################################
VPC_ID=$(create_or_get "vpc" "['Vpc']['VpcId']" \
"aws ec2 create-vpc --cidr-block 10.0.0.0/24")
# Allow instances to resolve their own hostname via dns,
# because some instances do not have the hostname set in /etc/hosts
# and some tools need to resolve the hostname.
aws ec2 modify-vpc-attribute --vpc-id ${VPC_ID} --enable-dns-hostnames > /dev/null
echo "$VPC_ID"
#########################################################################
echo -n "Creating subnet... "
#########################################################################
SUBNET_ID=$(create_or_get "subnet" "['Subnet']['SubnetId']" \
"aws ec2 create-subnet --vpc-id "$VPC_ID" --cidr-block 10.0.0.0/24")
echo "$SUBNET_ID"
#########################################################################
echo -n "Configuring security group: Allowing SSH and HTTP(S)... "
#########################################################################
SECURITY_GROUP_JSON=$(aws ec2 describe-security-groups --filters "Name=vpc-id,Values=$VPC_ID")
SECURITY_GROUP_ID=$(echo "$SECURITY_GROUP_JSON" | json_get "['SecurityGroups'][0]['GroupId']")
tag "$SECURITY_GROUP_ID"
if [[ "$SECURITY_GROUP_JSON" != *"\"ToPort\": 22,"* ]]
then
aws ec2 authorize-security-group-ingress \
--group-id "$SECURITY_GROUP_ID" \
--protocol tcp \
--port 22 \
--cidr 0.0.0.0/0
fi
if [[ "$SECURITY_GROUP_JSON" != *"\"ToPort\": 80,"* ]]
then
aws ec2 authorize-security-group-ingress \
--group-id "$SECURITY_GROUP_ID" \
--protocol tcp \
--port 80 \
--cidr 0.0.0.0/0
fi
if [[ "$SECURITY_GROUP_JSON" != *"\"ToPort\": 443,"* ]]
then
aws ec2 authorize-security-group-ingress \
--group-id "$SECURITY_GROUP_ID" \
--protocol tcp \
--port 443 \
--cidr 0.0.0.0/0
fi
echo "$SECURITY_GROUP_ID"
#########################################################################
echo -n "Creating Internet gateway... "
#########################################################################
INTERNET_GATEWAY_ID=$(create_or_get "internet-gateway" "['InternetGateway']['InternetGatewayId']" \
"aws ec2 create-internet-gateway")
echo "$INTERNET_GATEWAY_ID"
#########################################################################
echo -n "Attaching Internet gateway to VPC... "
#########################################################################
CURRENT_INTERNET_GATEWAY=$(aws ec2 describe-internet-gateways --internet-gateway-ids "$INTERNET_GATEWAY_ID")
if [[ "$CURRENT_INTERNET_GATEWAY" != *"\"VpcId\": \"$VPC_ID\""* ]]
then
aws ec2 attach-internet-gateway \
--internet-gateway-id "$INTERNET_GATEWAY_ID" \
--vpc-id "$VPC_ID"
fi
echo done
#########################################################################
echo -n "Retreiving route table id... "
#########################################################################
ROUTE_TABLE_ID=$(aws ec2 describe-route-tables \
--filter "Name=vpc-id,Values=$VPC_ID" \
| json_get "['RouteTables'][0]['RouteTableId']")
tag "$ROUTE_TABLE_ID"
echo "$ROUTE_TABLE_ID"
#########################################################################
echo -n "Creating default route... "
#########################################################################
aws ec2 create-route \
--route-table-id "$ROUTE_TABLE_ID" \
--destination-cidr-block "0.0.0.0/0" \
--gateway-id "$INTERNET_GATEWAY_ID" > /dev/null
echo done
#########################################################################
echo -n "Creating SSH key pair... "
#########################################################################
set +e
aws ec2 describe-key-pairs --key-name "$TAG_NAME" > /dev/null 2>&1
if [ $? -ne 0 ]
then
aws ec2 create-key-pair --key-name "$TAG_NAME" | json_get "['KeyMaterial']" > "${TAG_NAME}.pem"
chmod 400 "${TAG_NAME}.pem"
fi
set -e
echo done
#########################################################################
echo -n "Creating EC2 instance... "
#########################################################################
ALL_IDs=$(get_ids instance)
NON_TERMINATED=()
for INSTANCE_ID in $ALL_IDs
do
DESCRIBE_RESULT=$(aws ec2 describe-instances --filter "Name=instance-id,Values=$INSTANCE_ID")
if [[ $DESCRIBE_RESULT == *"\"InstanceId\": \"$INSTANCE_ID\""* ]]
then
INSTANCE_STATE=$(echo "$DESCRIBE_RESULT" | json_get "['Reservations'][0]['Instances'][0]['State']['Name']")
if [ $INSTANCE_STATE != "terminated" ] && [ $INSTANCE_STATE != "shutting-down" ]
then
NON_TERMINATED+=($INSTANCE_ID)
fi
fi
done
NUMBER_OF_NON_TERMINATED=${#NON_TERMINATED[@]}
if [[ $NUMBER_OF_NON_TERMINATED -eq 0 ]]
then
INSTANCE_ID=$(aws ec2 run-instances \
--image-id ami-b43503a9 \
--count 1 \
--instance-type t2.micro \
--instance-initiated-shutdown-behavior terminate \
--subnet-id $SUBNET_ID \
--security-group-ids $SECURITY_GROUP_ID \
--associate-public-ip-address \
--key-name "$TAG_NAME" \
--user-data file://${SCRIPT_DIR}/init-instance.script \
| json_get "['Instances'][0]['InstanceId']")
tag "$INSTANCE_ID"
elif [[ $NUMBER_OF_NON_TERMINATED -eq 1 ]]
then
INSTANCE_ID=${NON_TERMINATED[0]}
else
echo "Error: More than one instance with tag $TAG_NAME found." 2>&1
exit -1
fi
echo "$INSTANCE_ID"
#########################################################################
echo "Waiting for instance. This may take a few seconds... "
#########################################################################
INSTANCE_STATE="pending"
while [ "$INSTANCE_STATE" == "pending" ]
do
DESCRIBE_RESULT=$(aws ec2 describe-instances --filter "Name=instance-id,Values=$INSTANCE_ID")
INSTANCE_STATE=$(echo "$DESCRIBE_RESULT" | json_get "['Reservations'][0]['Instances'][0]['State']['Name']")
echo "... state is $INSTANCE_STATE."
if [[ "$INSTANCE_STATE" == "pending" ]]
then
sleep 5
fi
done
PUBLIC_IP=$(echo "$DESCRIBE_RESULT" | json_get "['Reservations'][0]['Instances'][0]['PublicIpAddress']")
echo Started EC2 instance with IP address "$PUBLIC_IP"
if [ -e "${TAG_NAME}.pem" ]
then
echo "As soon as the SSH daemon is started, you can log in with 'ssh -i ${TAG_NAME}.pem ec2-user@${PUBLIC_IP}'."
echo "If you see 'Connection refused': No worries, it will take a few minutes until the SSH daemon is started. Try again."
echo "The nginx Web server is installed in the background after the first boot."
echo "When you are logged in with SSH on ${PUBLIC_IP}, you can view the progress with 'tail -f /var/log/init-instance.log'."
echo "Once the Web server is up, you can access it on http(s)://${PUBLIC_IP}"
fi