kaf*_*fka 1 powershell amazon-ec2 amazon-web-services
我正在尝试向ELBV2注册新的EC2实例。我正在尝试使用AWSPowershell模块,但无法正常工作。
$InstanceId = (Invoke-WebRequest 'http://169.254.169.254/latest/meta-data/instance-id').Content
Register-ELB2Target -TargetGroupArn 'arn:etc...' -Target $InstanceID
Run Code Online (Sandbox Code Playgroud)
错误是:
Register-ELB2Target : Cannot bind parameter 'Target'. Cannot convert the "i-redacted" value of type "System.String" to type
"Amazon.ElasticLoadBalancingV2.Model.TargetDescription".
Run Code Online (Sandbox Code Playgroud)
我检查了文档,可以看到它也可以占用一个端口(可选)。曾尝试添加端口,但仍然没有运气。
您没有正确创建TargetDescription对象,它应该类似于:
$thisInstance = New-Object Amazon.ElasticLoadBalancingV2.Model.TargetDescription
$thisInstance.Id = $instanceId
$thisInstance.Port = 80
Run Code Online (Sandbox Code Playgroud)
对于到达这里寻求简单答案的任何人,我们使用以下脚本作为实例,以在部署新软件包时向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"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
835 次 |
| 最近记录: |