1

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.

4

1 回答 1

0

根据https://docs.aws.amazon.com/sdkfornet/v3/apidocs/items/ElasticLoadBalancingV2/TTargetDescription.html - Amazon.ElasticLoadBalancingV2.Model.TargetDescription 是关于“关于目标的信息” - 这意味着,您应该分配一个实例ID 另外,如果您仔细查看属性:

可用区系统.String

Id System.String

端口系统.Int32

您的实例搜索的结果可能是也可能不是单个输出 - 您应该将它们保持在循环中以通过 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"
}
于 2019-03-10T09:57:11.827 回答