Skip to content

Instantly share code, notes, and snippets.

@serkanh
Created March 16, 2017 15:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save serkanh/1920e581f3f27ee29c90bbc7d9191361 to your computer and use it in GitHub Desktop.
Save serkanh/1920e581f3f27ee29c90bbc7d9191361 to your computer and use it in GitHub Desktop.
create aws load balancer script
#!/bin/bash
set -euox pipefail
echo "Please enter profile to use"
read PROFILE
echo "Please enter region to use"
read REGION
echo "Please enter ELBPORT to use"
read ELBPORT
echo "Please enter INSTANCEPORT to use"
read INSTANCEPORT
echo "Please enter ELB_NAME to use"
read ELB_NAME
if [ -z $ELB_NAME ]; then
echo "ELB name have to be set"
exit 1
fi
ELBPORT=${ELBPORT:-'80'}
INSTANCEPORT=${INSTANCEPORT:-'8080'}
INSTANCEPORT=${INSTANCEPORT:-'3000'}
PROFILE=${PROFILE:-'HA'}
REGION=${REGION:-'us-east-1'}
getVpcNames(){
aws --profile=HA ec2 describe-vpcs --query "Vpcs[*].Tags[].Value" --output text
}
# Gets the public subnets on a given vpc.
getPublicSubnets(){
echo $(aws --profile=$PROFILE ec2 describe-subnets \
--filters "Name=vpc-id,Values=$VPCID" \
--query "Subnets[?Tags != null && Tags[?contains(Value,\`Public\`)==\`true\`]].SubnetId" --output text )
}
# Gets the public subnets on a given vpc.
createElb(){
aws --profile=$PROFILE elb create-load-balancer \
--load-balancer-name $ELB_NAME \
--listeners "Protocol=HTTP,LoadBalancerPort=$ELBPORT,InstanceProtocol=HTTP,InstancePort=$INSTANCEPORT" \
--subnets $PUBLIC_SUBNETS \
--security-groups sg-8c2d57f6
}
#getVpcNames
VPC=$( getVpcNames | sed -e 's/vpc//g' )
echo $VPC
select VPCNAME in $VPC;
do
VPCID=$(aws --profile=HA ec2 describe-vpcs --query "Vpcs[?Tags != null && Tags[?contains(Value,'${VPCNAME}')==\`true\`]].VpcId")
PUBLIC_SUBNETS=$(getPublicSubnets $VPCID)
createElb
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment