我正在尝试使用ELBV2注册一个新的EC2实例。我正在从AWSPowershell模块尝试,但无法使其工作。
$InstanceId = (Invoke-WebRequest 'http://169.254.169.254/latest/meta-data/instance-id').Content
Register-ELB2Target -TargetGroupArn 'arn:etc...' -Target $InstanceID错误是:
Register-ELB2Target : Cannot bind parameter 'Target'. Cannot convert the "i-redacted" value of type "System.String" to type
"Amazon.ElasticLoadBalancingV2.Model.TargetDescription".我查看了文档,可以看到它也可以使用端口(可选)。我已经尝试添加端口,但仍然没有成功。
发布于 2017-11-27 14:11:32
您没有正确创建TargetDescription对象,它应该如下所示:
$thisInstance = New-Object Amazon.ElasticLoadBalancingV2.Model.TargetDescription
$thisInstance.Id = $instanceId
$thisInstance.Port = 80对于其他来这里寻找简单答案的人,我们使用以下脚本在部署新包时将自己注册到ALB:
Set-DefaultAWSRegion "ap-southeast-2"
# work out the id and load balancer for this instance
$instanceId = (wget -UseBasicParsing "http://169.254.169.254/latest/meta-data/instance-id").Content
$targetGroupArn = (Get-EC2Tag -Filter @{Name="resource-id"; Values=$instanceId}, @{Name="key";Values="<name of key>"}).Value # instances have the alb arn tagged in when created by an autoscaling group
$thisInstance = New-Object Amazon.ElasticLoadBalancingV2.Model.TargetDescription
$thisInstance.Id = $instanceId
$thisInstance.Port = 80
write-host "Adding $instanceId to $targetGroupArn"
# register instace with ALB
Register-ELB2Target -TargetGroupArn $targetGroupArn -Targets @( $thisInstance ) -Force
write-host "Done"发布于 2019-08-22 21:24:24
基本上,它与@CamM的答案相同,但语法更短:
Register-ELB2Target -TargetGroupArn $targetGroupArn -Target @{ Id = $instanceId; Port = 80 }https://stackoverflow.com/questions/42395691
复制相似问题