查找附加到ELB的实例的方法

发布于 2024-09-19 03:17:10 字数 62 浏览 6 评论 0原文

我正在使用 Amazon AWS ELB 命令行工具。有没有办法找到附加到特定弹性负载均衡器(ELB)的实例?

I am using the Amazon AWS ELB command line tools. Is there a way of finding out the instances attached to a particular Elastic Load Balancer (ELB)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

茶花眉 2024-09-26 03:17: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 工具可能有类似这些的东西?

哈!

2013/12/18: To update this and since the links are dead!

I installed the new AWS cli tools:

$ pip install awscli

Then ran:

$ 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]:

This data is saved into ~/.aws/config.

Then I can find instances connected to a loadbalancer like so:

$ 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"
            }
        }
    ]
}

The data is in LoadBalancerDescriptions.Instances.

My loadbalancer is called my-name — this is the name you selected when you created it.

Old answer below!

I'm not familiar with the cli tool, but I used the API.

I'd check these two requests:

The cli tool probably has something to resemble these?

HTH!

浊酒尽余欢 2024-09-26 03:17:10

假设您有 aws-clijq 安装后,您可以使用以下命令获取关联的 ec2 实例 id:

aws elb describe-load-balancers --load-balancer-name my-elb \
  | jq -r '.LoadBalancerDescriptions[].Instances[].InstanceId'

这将返回与该 ELB 关联的 ec2 id 。

旁注:我建议您设置 aws cli 配置文件< /a> 所以你不必摆弄环境变量和区域参数(尽可能多)。

Assuming you have aws-cli and jq installed, you can use the following command to get associated ec2 instance ids:

aws elb describe-load-balancers --load-balancer-name my-elb \
  | jq -r '.LoadBalancerDescriptions[].Instances[].InstanceId'

This will return the ec2 ids associated with that ELB.

Side note: I recommend you setup aws cli profiles so you don't have to fiddle with environment variables and region params (as much).

初懵 2024-09-26 03:17:10

因为我喜欢可以通过最少的搜索/替换和复制粘贴使用的答案

先决条件:配置了 aws-cli

pip install awscli
aws configure

配置:您的 ELB 名称

$ELB_NAME = "Your-elb-name"

在终端中复制粘贴

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

将输出公共 IP 列表。您还可以在 for ID in $(...) 的括号内执行查询来获取实例 ID

想要不同的东西吗?

请随意查看 的结构

aws elb describe-load-balancers --load-balancer-name $ELB_NAME
aws ec2 describe-instances --instance-ids $INSTANCE_ID

并相应地更改查询!

Because I love answers that can be used with a minimum of search/replace and copy paste

Prerequisites : aws-cli configured

pip install awscli
aws configure

Configure : your ELB name

$ELB_NAME = "Your-elb-name"

Copy-n-Paste in terminal

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

Will output a list of Public IPs. You could also just execute the query inside the parenthesis of the for ID in $(...) to just get the instance IDs

Want something different ?

Feel free to have a look at the structure of

aws elb describe-load-balancers --load-balancer-name $ELB_NAME
aws ec2 describe-instances --instance-ids $INSTANCE_ID

and change the query accordingly!

毁我热情 2024-09-26 03:17:10

如果有人通过搜索到达这里,了解为什么 elb-describe-lbs 命令在启动并运行 ELB 时不返回任何内容,我意识到我需要添加 EC2_REGION=eu-west -1 到我的环境变量(或使用 elb-describe-lbs --region 命令)

If anyone arrives here from a search as to why the elb-describe-lbs command returns nothing when they have ELBs up and running, what I realised was I needed to add EC2_REGION=eu-west-1 to my environment variables (or use elb-describe-lbs --region command)

清引 2024-09-26 03:17:10

如果您想查看所有 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"
        ]
    }
]

If you want to see all your ELB's and the instances attached use JMESPath like this:

aws elb describe-load-balancers --query "LoadBalancerDescriptions[*].{ID:LoadBalancerName,InstanceId:Instances[*].InstanceId}[*]. {ELB:ID,InstanceId:InstanceId[*]}" --output=json

Result

[
    {
        "ELB": "my_name",
        "InstanceId": [
            "i-0cc72"
        ]
    },
    {
        "ELB": "my_name2",
        "InstanceId": [
            "i-02ff5f",
            "i-09e467"
        ]
    }
]

If you know the name of the ELB and want to see what is attached use JMESPath like this:

aws elb describe-load-balancers --load-balancer-name "my_name" --query "LoadBalancerDescriptions[].{ID:LoadBalancerName,InstanceId:Instances[].InstanceId}[].{ELB:ID,InstanceId:InstanceId[]}" --output=json

Result:

[
    {
        "ELB": "my_name",
        "InstanceId": [
            "i-02ff5f72",
            "i-09e46743"
        ]
    }
]
小镇女孩 2024-09-26 03:17:10

将 INSTANCEID 替换为实际实例 ID

aws elb describe-load-balancers --query "LoadBalancerDescriptions[*].{ID:LoadBalancerName,InstanceId:Instances[?InstanceId=='INSTANCEID'].InstanceId}[*].{ ID:ID,InstanceId:InstanceId[0]}" --output=text | grep 实例ID | awk '{print $1}'

replace INSTANCEID with actual instance 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}'

请持续率性 2024-09-26 03:17:10

在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 是一个数组,数组中的每个元素都是一个具有属性 Instances 的对象,该属性具有实例 ID。

In node.js you can do this by using 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 is an array and each element in the array is an object with the property Instances that has the instance id.

时光是把杀猪刀 2024-09-26 03:17:10

您可以循环遍历所有负载均衡器实例 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
猫<< “EOF”> ~/.aws/config

[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

You can loop trough all you load balancer instance ids as follows:

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')

You can loop trough your load balancers names as follows :

    # 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')

Provided that you have configured your aws cli :
src: http://docs.aws.amazon.com/cli/latest/topic/config-vars.html
cat << "EOF" > ~/.aws/config

[profile dev]
output = text
region = us-east-1
[profile dev]
output = text
region = us-east-1
[default]
output = text
region = Global

EOF 

And configured your security credentials:

    # 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
π浅易 2024-09-26 03:17:10

aws elb 描述负载均衡器 --负载均衡器名称“LB_NAME”| grep "实例ID" | awk '{print $2}' | sed 's/\"//g'

aws elb describe-load-balancers --load-balancer-name "LB_NAME" | grep "InstanceId" | awk '{print $2}' | sed 's/\"//g'

寄意 2024-09-26 03:17:10

首先执行 elb-describe-lbs 来获取负载均衡器及其名称的列表。

然后执行 elb-describe-instance-health 获取该负载均衡器背后的实例列表。 LB_NAME 是 elb-describe-lbs 输出中第二列的值。

First do elb-describe-lbs to get a list of your load balancers and their names.

Then do elb-describe-instance-health <LB_NAME> to get a list of instances behind that load balancer. LB_NAME is the value of the 2nd column in the output of elb-describe-lbs.

请帮我爱他 2024-09-26 03:17:10

您可以将 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 列以获取其他详细信息。

You can use AWS command line tools with some bash piping:

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 }'

This will give you the public DNS name for every instance attached to the ELB, you can change the awk columns respectively to get other details.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文