无法在AWS Target组上注册目标

时间:2019-03-08 20:40:29

标签: amazon-web-services powershell aws-alb

我正在尝试使用Powershell设置自动DNS部署。我已经编写了一个Powershell脚本,用于创建TargetGroup,将实例注册到TG,创建ALB并向其添加侦听器。一旦完成,它将创建R53 RecordSet并创建A记录到ALB DNS。 在将实例注册到TargetGroup时遇到问题。 这是该部分的代码段:

$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)

它抛出以下错误:

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

我检查了类似的帖子here。和相应的职位,到目前为止没有任何帮助。我想知道是否有人可以指导我我在做什么错?

我尝试逐行运行每一行,碰巧将实例注册到TargetGroup,只是脚本失败。 实例为t2.micro,它们处于运行状态。

1 个答案:

答案 0 :(得分:0)

根据https://docs.aws.amazon.com/sdkfornet/v3/apidocs/items/ElasticLoadBalancingV2/TTargetDescription.html- Amazon.ElasticLoadBalancingV2.Model.TargetDescription涉及“有关目标的信息”- 也就是说,您应该分配一个实例ID。另外,如果您仔细查看以下属性:

AvailabilityZone System.String

Id System.String

Port System.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"
}