2017-02-22 152 views
0

我正試圖用ELBV2註冊一個新的EC2實例。我正在嘗試從AWSPowershell模塊,但無法讓它工作。如何使用ELBV2註冊EC2實例?

$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". 

我檢查的文件,並且可以看到,它可以採取口太(可選)。嘗試添加端口,但仍然沒有運氣。

+1

這聽起來像是數據類型錯誤。確保您正在將數據對象轉換爲正確的類型,如文檔所述。你確定你應該直接使用instanceid而不是對象數據嗎?檢查一個樣本:https://www.yobyot.com/aws/how-to-register-and-deregister-ec2-instances-by-name-from-an-elb/2015/01/09/ – Sam

+0

事實上,這是數據類型錯誤,我只是無法弄清楚如何糾正它! 我現在已經排序它 $ Instance = New-Object Amazon.ElasticLoadBalancingV2.Model.TargetDescription 如果您發佈一個答案沿着這些線它是你的... – kafka

回答

1

你沒有正確創建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"