Search code examples
amazon-web-servicespowershellaws-alb

Unable to register targets on AWS Target group


I am trying to setup an automated DNS deployment using powershell. I have written a powershell script that creates TargetGroup, registers instances to the TG, creates an ALB and adds a listener to it. Once, that is done, it creates R53 RecordSet and creates an A record to the ALB DNS. I am having issues in having the instances registered to the TargetGroup. This is my code snippet to that section:

$searchFor1 =@( @{name = 'tag:Name'; values = $target1})
$searchFor2 =@( @{name = 'tag:Name'; values = $target2})

$id1 = (Get-EC2Instance -Filter $searchFor1).Instances | select InstanceId
$id2 = (Get-EC2Instance -Filter $searchFor2).Instances | select InstanceId

# Create Target Group

$tg = New-ELB2TargetGroup -TargetType "instance" -HealthyThresholdCount 4 -Name $custname -Port $siteport -Protocol "HTTP" -UnhealthyThresholdCount 4 -VpcId $vpcid
Start-Sleep -s 120
$addid1 = New-Object Amazon.ElasticLoadBalancingV2.Model.TargetDescription
$addid2 = New-Object Amazon.ElasticLoadBalancingV2.Model.TargetDescription
$addid1.Id = $id1.InstanceId
$addid2.Id = $id2.InstanceId
$addid1.Port = $siteport
$addid2.Port = $siteport
$tgarn = (Get-ELB2TargetGroup -Name $custname).TargetGroupArn
Register-ELB2Target -TargetGroupArn $tgarn -Target @($addid1)
Register-ELB2Target -TargetGroupArn $tgarn -Target @($addid2)

It throws below error:

Register-ELB2Target : An instance ID must be specified
At C:\scripts\Distinct-DNS-Deployment.ps1:107 char:1
+ Register-ELB2Target -TargetGroupArn $tgarn -Target @($addid1)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (Amazon.PowerShe...LB2TargetCmdlet:RegisterELB2TargetCmdlet) [Register
   -ELB2Target], InvalidOperationException
    + FullyQualifiedErrorId : Amazon.ElasticLoadBalancingV2.AmazonElasticLoadBalancingV2Exception,Amazon.PowerShell.Cm
   dlets.ELB2.RegisterELB2TargetCmdlet

I have checked a similar post here. And the corresponding posts, so far nothing helped. I am wondering if anyone can guide me what am I doing wrong?

I have tried to run each line one by one and that happens to register the instance to the TargetGroup, just the script fails. Instances are t2.micro and they are in running state.


Solution

  • As per the https://docs.aws.amazon.com/sdkfornet/v3/apidocs/items/ElasticLoadBalancingV2/TTargetDescription.html - the Amazon.ElasticLoadBalancingV2.Model.TargetDescription is about 'Information about a target' - which means, you should be assigning a single instance id Also, if you take a close look at the properties:

    AvailabilityZone System.String

    Id System.String

    Port System.Int32

    The result of your instance search may or may not be single output - you should keep them in loop to create each target via TargetDescription

    $Instances = (Get-EC2Instance -Filter @{Name="tag:auto-delete";Value="no"}).instances |select instanceid
    
    $theVpc = get-ec2vpc -VpcId vpc-4565e5c4
    $name = "new-tg"
    $port = "80"
    $protocol = "HTTP"
    
    $tg = New-ELB2TargetGroup -TargetType "instance" -HealthyThresholdCount 4 -Name $name -Port $port -Protocol "HTTP" -UnhealthyThresholdCount 4 -VpcId $theVpc.VpcId
    $tgarn = (Get-ELB2TargetGroup -Name $name).TargetGroupArn
    
    If($instances -ne $null){
        foreach ($instance in $instances ){
            $addid1 = New-Object Amazon.ElasticLoadBalancingV2.Model.TargetDescription
            $addid1.Id = $Instance.instanceid
            $addid1.Port = $port
            Register-ELB2Target -TargetGroupArn $tgarn -Target @($addid1)
            Remove-Variable addid1
        }
    }
    else {
        echo "There were no instances with the matching filter"
    }