如何在ELBV2中注册EC2实例? [英] How to register an EC2 Instance with an ELBV2?
问题描述
我正在尝试向ELBV2注册新的EC2实例.我正在尝试使用AWSPowershell模块,但无法使其正常工作.
I'm trying to register a new EC2 instance with an ELBV2. I'm trying from the AWSPowershell Module but cannot get it to work.
$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".
我已经检查了文档,可以看到它也可以占用一个端口(可选).曾尝试添加端口,但仍然没有运气.
I've checked the documentation, and can see that it can take a port too (optional). Have tried adding the port, but still no luck.
推荐答案
您没有正确创建TargetDescription对象,它应该类似于:
You're not creating the TargetDescription object correctly, it should be something like:
$thisInstance = New-Object Amazon.ElasticLoadBalancingV2.Model.TargetDescription
$thisInstance.Id = $instanceId
$thisInstance.Port = 80
对于到达这里寻求简单答案的任何其他人,我们使用以下脚本作为实例,以便在部署新软件包时向ALB注册:
For anyone else who arrives here looking for an easy answer, we use the following script for instances to register themselves with the ALB when new packages are deployed:
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"
这篇关于如何在ELBV2中注册EC2实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!