
If you are not familiar with AWS Application Load Balancers, check out my article FreeKB - Amazon Web Services (AWS) - Getting Started with Application Load Balancer (ALB).
This assumes you have already configured the aws command line tool. If not, check out my article on Getting Started with the AWS CLI
Let's say you have two or more EC2 instances that are producing web apps. An application load balancer can be used to load balance the requests for the web app.
A Target Group contains:
- One or more EC2 instances using the EC2 instances ID
- One or more EC2 instances via IP address
- One or more EC2 instances via Lambda
- One or more Application Load Balancers (in other words, a group of Application Load Balancers)
The aws elbv2 describe-target-groups command can be used to list your Target Groups and to get the Amazon Resource Number (ARN) of the Target Group.
~]# aws elbv2 describe-target-groups
{
"TargetGroups": [
{
"TargetGroupArn": "arn:aws:elasticloadbalancing:us-east-1:123456789012:targetgroup/my-target-group/af5707bbd67c69ab",
"TargetGroupName": "my-target-group",
"Protocol": "HTTP",
"Port": 80,
"VpcId": "vpc-123abd654dsf356s1",
"HealthCheckProtocol": "HTTP",
"HealthCheckPort": "traffic-port",
"HealthCheckEnabled": true,
"HealthCheckIntervalSeconds": 30,
"HealthCheckTimeoutSeconds": 5,
"HealthyThresholdCount": 5,
"UnhealthyThresholdCount": 2,
"HealthCheckPath": "/",
"Matcher": {
"HttpCode": "200"
},
"LoadBalancerArns": [],
"TargetType": "instance",
"ProtocolVersion": "HTTP1",
"IpAddressType": "ipv4"
}
]
}
Let's say you are configuring an Application Load Balancer to listen on HTTP port 80 and let's say app "a" on each EC2 instance is listening on port 12345 and app "b" in each EC2 instance is listening on port 67890.
The aws elbv2 register-targets command can be used to register EC2 instances in the Target Group.
aws elbv2 register-targets \
--target-group-arn arn:aws:elasticloadbalancing:us-east-1:123456789012:targetgroup/my-target-group/af5707bbd67c69ab \
--targets Id=i-a0908ab089f9008ab,Port=12345 Id=i-c099080ab09dc0909,Port=12345
Did you find this article helpful?
If so, consider buying me a coffee over at