2017-02-22 5 views
0

新しいEC2インスタンスをELBV2で登録しようとしています。私はAWSPowershell Moduleから試していますが、動作させることはできません。EC2インスタンスをELBV2に登録する方法は?

$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" 
関連する問題