Installing
Install Python
brew install python
Install AWS CLI
- AWS CLI (http://aws.amazon.com/cli/ ) is command line tool for working with Amazon Web Service. In this project, we used it for checking the Elastic Load Balancer
(ELB) is existed or not, creating an ELB, adding the Health check
for ELB, retrieving the EC2 instance
information registering EC2 instances
to ELB.
pip install awscli
Create config for aws at ~/.aws/config and add below content
Config file link 1
2
3
4
5
[ profile default]
output = json
region = us-east-1
aws_access_key_id = <access key id>
aws_secret_access_key = <secret access key>
Coding
add-node-2-elb.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
59
#!/bin/bash
#
# ask to add the node to load balancer
node = $1 ;
role = $2 ;
env = $3 ;
lbName = "MyLoadBalancer"
if [ "$node" != "" ] && [ "$role" != "" ] ; then
if [ "$role" == "web" ] ; then
if [ "$env" == "prod" ] ; then
lbName = "MyLoadBalancerProd"
else
lbName = "MyLoadBalancerStaging"
fi
else
printf "Please enter a valid role ('web') \n" ;
fi
# http://docs.aws.amazon.com/cli/latest/reference/elb/describe-load-balancers.html
MyLoadBalancer = $(
aws elb describe-load-balancers \
--load-balancer-name $lbName \
) ;
# the load balancer is node existed, create the ELB first
if [ "$MyLoadBalancer" == '' ] ; then
# http://docs.aws.amazon.com/cli/latest/reference/elb/create-load-balancer.html
aws elb create-load-balancer \
--load-balancer-name $lbName \
--listeners Protocol = HTTP,LoadBalancerPort= 80,InstanceProtocol= HTTP,InstancePort= 80 \
--availability-zones us-east-1a \
--security-groups sg-bac6eddf \
#http://docs.aws.amazon.com/cli/latest/reference/elb/configure-health-check.html
aws elb configure-health-check \
--load-balancer-name $lbName \
--health-check Target = HTTP:80/index.php,Interval= 30,UnhealthyThreshold= 2,HealthyThreshold= 2,Timeout= 3
else
printf "$MyLoadBalancer\n" ;
fi
# http://docs.aws.amazon.com/cli/latest/reference/ec2/describe-instances.html
# http://xmodulo.com/how-to-parse-json-string-via-command-line-on-linux.html
# MAC - brew install jq
nodeId = $( aws ec2 describe-instances --filters "Name=ip-address,Values=$node" | jq ".Reservations[0].Instances[0].InstanceId" )
nodeId = "${nodeId%\"}" ;
# http://docs.aws.amazon.com/cli/latest/reference/elb/register-instances-with-load-balancer.html
aws elb register-instances-with-load-balancer \
--load-balancer-name $lbName \
--instances "${nodeId#\"}"
else
printf "Please enter the node and role adding to ELB.\n" ;
fi
Usage
./add-node-2-elb.sh 123.456.789.910 web staging