我正在使用Amazon AWS ELB命令行工具。有没有办法找出附加到特定弹性负载均衡器(ELB)的实例?
答案 0 :(得分:10)
2013/12/18:要更新此内容,因为链接已关闭!
我安装了新的AWS cli工具:
$ pip install awscli
然后跑了:
$ aws configure
AWS Access Key ID [None]: my-key
AWS Secret Access Key [None]: my-secret
Default region name [None]: us-east-1
Default output format [None]:
此数据已保存到~/.aws/config
。
然后我可以找到连接到负载均衡器的实例,如下所示:
$ aws elb describe-load-balancers --load-balancer-name "my-name"
{
"LoadBalancerDescriptions": [
{
"Subnets": [],
"CanonicalHostedZoneNameID": "ID",
"CanonicalHostedZoneName": "my-name-foo.us-east-1.elb.amazonaws.com",
"ListenerDescriptions": [
{
"Listener": {
"InstancePort": 80,
"LoadBalancerPort": 80,
"Protocol": "HTTP",
"InstanceProtocol": "HTTP"
},
"PolicyNames": []
},
{
"Listener": {
"InstancePort": 80,
"SSLCertificateId": "arn:aws:iam::x:server-certificate/x-ssl-prod",
"LoadBalancerPort": 443,
"Protocol": "HTTPS",
"InstanceProtocol": "HTTP"
},
"PolicyNames": [
"AWSConsole-SSLNegotiationPolicy-api-production"
]
}
],
"HealthCheck": {
"HealthyThreshold": 10,
"Interval": 30,
"Target": "HTTP:80/healthy.php",
"Timeout": 5,
"UnhealthyThreshold": 2
},
"BackendServerDescriptions": [],
"Instances": [
{
"InstanceId": "i-FIRST-INSTANCEID"
},
{
"InstanceId": "i-SECOND-INSTANCEID"
}
],
"DNSName": "my-name-foo.us-east-1.elb.amazonaws.com",
"SecurityGroups": [],
"Policies": {
"LBCookieStickinessPolicies": [],
"AppCookieStickinessPolicies": [],
"OtherPolicies": [
"AWSConsole-SSLNegotiationPolicy-my-name"
]
},
"LoadBalancerName": "my-name",
"CreatedTime": "2013-08-05T16:55:22.630Z",
"AvailabilityZones": [
"us-east-1d"
],
"Scheme": "internet-facing",
"SourceSecurityGroup": {
"OwnerAlias": "amazon-elb",
"GroupName": "amazon-elb-sg"
}
}
]
}
数据位于LoadBalancerDescriptions.Instances
。
我的负载均衡器名为my-name
- 这是您在创建时选择的名称。
下面的旧答案!
我不熟悉cli工具,但我使用了API。
我会检查这两个请求:
cli工具可能有类似的东西吗?
HTH!
答案 1 :(得分:7)
假设您安装了aws-cli和jq,可以使用以下命令获取关联的ec2实例ID:
aws elb describe-load-balancers --load-balancer-name my-elb \
| jq -r '.LoadBalancerDescriptions[].Instances[].InstanceId'
这将返回与该ELB关联的ec2 ID。
旁注:我建议您设置aws cli profiles,这样就不必使用环境变量和区域参数(同样多)。
答案 2 :(得分:6)
因为我喜欢可以使用最少的搜索/替换和复制粘贴的答案
> dt <- data.table(matrix(rnorm(25), 5,5))
> names(dt) <- c("X/Y_1", "Y/Z_1", "X/Y_2", "Y/Z_2", "X/Y_3")
> dt
X/Y_1 Y/Z_1 X/Y_2 Y/Z_2 X/Y_3
1: 1.5972490 -0.01763484 1.10745607 -0.1416583 -0.4632829
2: 0.6629621 -0.82719204 -1.68214956 0.6145526 -0.8169235
3: -0.7491393 -0.05290791 0.63935066 1.0665537 -1.9107424
4: -0.6804972 -0.40107880 -0.01030063 1.4566075 -0.6866042
5: 0.2505391 -0.29091850 -1.95926987 0.8733446 1.3909565
pip install awscli
aws configure
$ELB_NAME = "Your-elb-name"
将输出公共IP列表。您也可以只在for ID in $(aws elb describe-load-balancers --load-balancer-name $ELB_NAME \
--query LoadBalancerDescriptions[].Instances[].InstanceId \
--output=text);
do
aws ec2 describe-instances --instance-ids $ID \
--query Reservations[].Instances[].PublicIpAddress \
--output text
done
的括号内执行查询以获取实例ID
随意查看
的结构for ID in $(...)
并相应地更改查询!
答案 3 :(得分:2)
如果有人从搜索到达这里为什么elb-describe-lbs
命令在ELB启动并运行时没有返回任何内容,我意识到我需要将EC2_REGION=eu-west-1
添加到我的环境变量(或使用elb-describe-lbs --region
命令)
答案 4 :(得分:1)
如果您想要查看所有ELB和附加的实例,请使用JMESPath,如下所示:
aws elb describe-load-balancers --query "LoadBalancerDescriptions[*].{ID:LoadBalancerName,InstanceId:Instances[*].InstanceId}[*]. {ELB:ID,InstanceId:InstanceId[*]}" --output=json
结果
[
{
"ELB": "my_name",
"InstanceId": [
"i-0cc72"
]
},
{
"ELB": "my_name2",
"InstanceId": [
"i-02ff5f",
"i-09e467"
]
}
]
如果您知道ELB的名称并希望查看附加内容,请使用JMESPath,如下所示:
aws elb describe-load-balancers --load-balancer-name "my_name" --query "LoadBalancerDescriptions[].{ID:LoadBalancerName,InstanceId:Instances[].InstanceId}[].{ELB:ID,InstanceId:InstanceId[]}" --output=json
结果:
[
{
"ELB": "my_name",
"InstanceId": [
"i-02ff5f72",
"i-09e46743"
]
}
]
答案 5 :(得分:0)
将INSTANCEID替换为实际的实例ID
aws elb describe-load-balancers --query "LoadBalancerDescriptions[*].{ID:LoadBalancerName,InstanceId:Instances[?InstanceId=='INSTANCEID'].InstanceId}[*].{ID:ID,InstanceId:InstanceId[0]}" --output=text | grep INSTANCEID | awk '{print $1}'
答案 6 :(得分:0)
在node.js中,您可以使用aws-sdk
。
var AWS = require('aws-sdk')
var options = {
accessKeyId: 'accessKeyId',
secretAccessKey: 'secretAccessKey',
region: 'region'
}
var elb = new AWS.ELB(options)
elb.describeLoadBalancers({LoadBalancerNames: ['elbName']}, function(err, data) {
if (err) {
console.log('err: ', err)
}
else {
console.log('data: ', data.LoadBalancerDescriptions)
}
})
data.LoadBalancerDescriptions
是一个数组,数组中的每个元素都是一个具有实例id的属性Instances
的对象。
答案 7 :(得分:0)
您可以循环播放所有负载均衡器实例ID,如下所示:
while read -r lb ; do echo -e "\n\n start lb: $lb " ; \
echo run cmd on lb: $lb ; echo " stop lb: $lb" ; \
done < <(aws elb describe-load-balancers --query \
'LoadBalancerDescriptions[].Instances[].InstanceId' \
--profile dev|perl -nle 's/\s+/\n/g;print')
您可以循环使用负载均衡器名称,如下所示:
# how-to loop trough all your load balancer names
while read -r lb ; do \
echo -e "\n\n start lb: $lb " ; \
echo run cmd on lb: $lb ; \
echo " stop lb: $lb" ; \
done < <(aws elb describe-load-balancers --query \
'LoadBalancerDescriptions[].LoadBalancerName' \
--profile rnd|perl -nle 's/\s+/\n/g;print')
如果您配置了aws cli: src:http://docs.aws.amazon.com/cli/latest/topic/config-vars.html 猫&lt;&lt; &#34; EOF&#34; &GT; 〜/ .aws /配置
[profile dev]
output = text
region = us-east-1
[profile dev]
output = text
region = us-east-1
[default]
output = text
region = Global
EOF
并配置了您的安全凭据:
# in aws admin console :
# Services => iam => users => <<your_username>> => Security Credentials => Access Keys
# configure the aws cli
cat << "EOF" > ~/.aws/credentials
[dev]
aws_access_key_id = <<your_aws_access_key_id_in_the_dev_environment>>
aws_secret_access_key = <<your_aws_secret_access_key_in_dev_env>>
[dev]
aws_access_key_id = <<your_aws_access_key_id_in_the_dev_environment>>
aws_secret_access_key = <<your_aws_secret_access_key_in_dev_env>>
[default]
aws_access_key_id = <<your_aws_access_key_id_in_the_dev_environment>>
aws_secret_access_key = <<your_aws_secret_access_key_in_dev_env>>
EOF
答案 8 :(得分:0)
aws elb describe-load-balancer --load-balancer-name“ LB_NAME” | grep“ InstanceId” | awk'{print $ 2}'| sed's / \“ // g'
答案 9 :(得分:-1)
首先执行elb-describe-lbs
以获取负载均衡器及其名称的列表。
然后执行elb-describe-instance-health <LB_NAME>
以获取该负载均衡器后面的实例列表。 LB_NAME是elb-describe-lbs
输出中第二列的值。
答案 10 :(得分:-2)
您可以将AWS命令行工具与一些bash管道配合使用:
elb-describe-instance-health loadbalancer_name --region eu-west-1 | awk '{ print $2 }' | xargs ec2-describe-instances --region eu-west-1 | grep ^INSTANCE | awk '{ print $4 }'
这将为您附加到ELB的每个实例提供公共DNS名称,您可以分别更改awk列以获取其他详细信息。