-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlaunch.sh
executable file
·60 lines (39 loc) · 3.92 KB
/
launch.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
58
#!/bin/bash
# Arguement commands are as followed: ami-d05e75b8 3 t2.micro sg-4a82be2d subnet-08d9f951 itmo444springVMWare mp1 mp1-elb mp1-launch-config mp1-auto-scaling-group mp1-scaleup-policy mp1-scaledown-policy
#./cleanup.sh
declare -a instancecheck
mapfile -t instancecheck < <(aws ec2 run-instances --image-id $1 --count $2 --instance-type $3 --security-group-ids $4 --subnet-id $5 --key-name $6 --iam-instance-profile Name=$7 --associate-public-ip-address --user-data file://environment-setup/install-env.sh --output table | grep InstanceId | sed "s/|//g" | tr -d ' ' | sed "s/InstanceId//g")
echo ${instancecheck[@]}
aws ec2 wait instance-running --instance-ids ${instancecheck[@]}
echo "All instances are now running"
aws elb create-load-balancer --load-balancer-name $8 --listeners "Protocol=HTTP,LoadBalancerPort=80,InstanceProtocol=HTTP,InstancePort=80" --security-groups $4 --subnets $5 --output=text
echo -e "\nFinished launching ELB and waiting 25 seconds"
echo -e "\n"
for i in {0..25};do echo -ne '.';sleep 1;done
echo -e "\n"
aws elb register-instances-with-load-balancer --load-balancer-name $8 --instances ${instancecheck[@]}
aws elb configure-health-check --load-balancer-name $8 --health-check Target=HTTP:80/index.html,Interval=30,UnhealthyThreshold=2,HealthyThreshold=2,Timeout=3
aws elb create-lb-cookie-stickiness-policy --load-balancer-name $8 --policy-name MPFINAL-SP --cookie-expiration-period 300
aws elb set-load-balancer-policies-of-listener --load-balancer-name $8 --load-balancer-port 80 --policy-names MPFINAL-SP
aws elb describe-load-balancers --load-balancer-name $8
aws autoscaling create-launch-configuration --launch-configuration-name $9 --image-id $1 --key-name $6 --security-groups $4 --instance-type $3 --user-data file://environment-setup/install-env.sh --iam-instance-profile $7 --associate-public-ip-address
aws autoscaling create-auto-scaling-group --auto-scaling-group-name $10 --launch-configuration-name $9 --load-balancer-names $8 --health-check-type ELB --min-size 3 --max-size 6 --desired-capacity 3 --default-cooldown 600 --health-check-grace-period 120 --vpc-zone-identifier $5
PolicyARN1=(`aws autoscaling put-scaling-policy --policy-name $11 --auto-scaling-group-name $10 --scaling-adjustment 1 --adjustment-type ChangeInCapacity`);
PolicyARN2=(`aws autoscaling put-scaling-policy --policy-name $12 --auto-scaling-group-name $10 --scaling-adjustment -1 --adjustment-type ChangeInCapacity`);
aws cloudwatch put-metric-alarm --alarm-name AddCapacity --metric-name CPUUtilization --namespace AWS/EC2 --statistic Average --period 120 --threshold 30 --comparison-operator GreaterThanOrEqualToThreshold --dimensions "Name=AutoScalingGroupName,Value=$10" --evaluation-periods 2 --alarm-actions $PolicyARN1 --unit Percent
aws cloudwatch put-metric-alarm --alarm-name RemoveCapacity --metric-name CPUUtilization --namespace AWS/EC2 --statistic Average --period 120 --threshold 10 --comparison-operator LessThanOrEqualToThreshold --dimensions "Name=AutoScalingGroupName,Value=$10" --evaluation-periods 2 --alarm-actions $PolicyARN2 --unit Percent
mapfile -t dbInstanceARR < <(aws rds describe-db-instances --output json | grep "\"DBInstanceIdentifier" | sed "s/[\"\:\, ]//g" | sed "s/DBInstanceIdentifier//g")
echo ${#dbInstanceARR[@]}
LENGTH=${#dbInstanceARR[@]}
for (( i=0; i<=${LENGTH}; i++));
do
if [[ ${dbInstanceARR[i]} == "mp1jphdb" ]]
then
echo "db exists"
else
aws rds create-db-instance --db-name mp1jphdb --db-instance-identifier mp1jphdb --db-instance-class db.t1.micro --engine MySQL --master-username jhedlund --master-user-password letmeinplease --allocated-storage 20 --vpc-security-group-ids $4 --db-subnet-group-name db-mp1-subnet
aws rds wait db-instance-available --db-instance-identifier mp1jphdb
aws rds create-db-instance-read-replica --db-instance-indentifier mpfinal-replica --source-db-instance-identifier mp1jphdb
fi
done
echo "All done!"