-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcreate-servers.sh
40 lines (35 loc) · 1.32 KB
/
create-servers.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
#!/bin/bash
NAMES=$@
INSTANCE_TYPE=""
IMAGE_ID=ami-03265a0778a880afb
SECURITY_GROUP_ID=sg-0b34d8689bd628e3f
DOMAIN_NAME=joindevops.online
HOSTED_ZONE_ID=Z0308214GYCUYHGJHT8R
# if mysql or mongodb instance_type should be t3.medium , for all others it is t2.micro
for i in $@
do
if [[ $i == "mongodb" || $i == "mysql" ]]
then
INSTANCE_TYPE="t3.medium"
else
INSTANCE_TYPE="t2.micro"
fi
echo "creating $i instance"
IP_ADDRESS=$(aws ec2 run-instances --image-id $IMAGE_ID --instance-type $INSTANCE_TYPE --security-group-ids $SECURITY_GROUP_ID --tag-specifications "ResourceType=instance,Tags=[{Key=Name,Value=$i}]" | jq -r '.Instances[0].PrivateIpAddress')
echo "created $i instance: $IP_ADDRESS"
aws route53 change-resource-record-sets --hosted-zone-id $HOSTED_ZONE_ID --change-batch '
{
"Changes": [{
"Action": "CREATE",
"ResourceRecordSet": {
"Name": "'$i.$DOMAIN_NAME'",
"Type": "A",
"TTL": 300,
"ResourceRecords": [{ "Value": "'$IP_ADDRESS'"}]
}}]
}
'
done
# imporvement
# check instance is already created or not
# check route53 record is already exist, if exist update, otherwise create route53 record