Bootstrap FreeKB - Amazon Web Services (AWS) - Register Targets in a Target Group using the AWS CLI
Amazon Web Services (AWS) - Register Targets in a Target Group using the AWS CLI


An Elastic Load Balancer (ELB) is typically used to load balance requests across two (or more) different EC2 instances. 

 

This assumes you have already configured the aws command line tool. If not, check out my article on Getting Started with the AWS CLI.

There are a few different types of load balancers.

  • Application Load Balancers (e.g. you have a web app that you want to load balance)
  • Network Load Balancers (e.g. you have SQL databases that you want to load balance)
  • Gateway Load Balancers
  • Classic Load Balancers (deprecated)

When creating an Application Load Balancer, you will need a Target Group. A Target Group is:

  • 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, an group of Application Load Balancers)

If you have not yet created a Target Group, check out my article on Creating a Target Group using the AWS CLI.The aws elbv2 describe-target-groups command can be used to list the Target Groups that have been created. 

Let's say you've created a Target Group named my-target-group that will contain one or more EC2 instances using the EC2 instances ID.

~]$ aws elbv2 describe-target-groups
{
    "TargetGroups": [
        {
            "TargetGroupArn": "arn:aws:elasticloadbalancing:us-east-1:713512344252:targetgroup/my-target-group/b12348d3a5abcd1d",
            "TargetGroupName": "my-target-group",
            "Protocol": "HTTP",
            "Port": 80,
            "VpcId": "vpc-0141234fa33abcd01",
            "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"
        }
    ]
}

 

Optionally, --names can be used to return target groups matching names.

aws elbv2 describe-target-groups --names my-target-group

 

The aws ec2 describe-instances command can be used to the IDs of the EC2 instances you want to register in the Target Group.

 ~]$ aws ec2 describe-instances --filters "Name=tag:Name,Values=foo-instance"
{
    "Reservations": [
        {
            "Groups": [],
            "Instances": [
                {
                    "AmiLaunchIndex": 0,
                    "ImageId": "ami-0b0dcb5067f052a63",
                    "InstanceId": "i-0f9a8d82897695a66",

 

The aws elbv2 register-targets command can then be used the Register the EC2 instances in the Target Group. The EC2 instance being registered must be running.

aws elbv2 register-targets 
--target-group-arn arn:aws:elasticloadbalancing:us-east-1:713512344252:targetgroup/my-target-group/b12348d3a5abcd1d
--targets Id=i-0f9a8d82897695a66 Id=i-0182deb20171e5945

 

The aws elbv2 describe-target-health command can then be used to see that the EC2 instances have been registered in the Target Group.

~]$ aws elbv2 describe-target-health --target-group-arn arn:aws:elasticloadbalancing:us-east-1:713512344252:targetgroup/my-target-group/b12348d3a5abcd1d
{
    "TargetHealthDescriptions": [
        {
            "Target": {
                "Id": "i-0182deb20171e5945",
                "Port": 80
            },
            "HealthCheckPort": "80",
            "TargetHealth": {
                "State": "unused",
                "Reason": "Target.NotInUse",
                "Description": "Target group is not configured to receive traffic from the load balancer"
            }
        },
        {
            "Target": {
                "Id": "i-0f9a8d82897695a66",
                "Port": 80
            },
            "HealthCheckPort": "80",
            "TargetHealth": {
                "State": "unused",
                "Reason": "Target.NotInUse",
                "Description": "Target group is not configured to receive traffic from the load balancer"
            }
        }
    ]
}

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 5453aa in the box below so that we can be sure you are a human.