![](/img/trans.png)
[英]What is the best way to get the private IP addresses of other ec2's in an autoscaling group while on one of the ec2 instances?
[英]How to get EC2 ID and Private IP from EC2 Autoscaling Group using AWS CDK
如何獲取使用 AutoscalingGroup (AWS CDK Python) 部署的 EC2 實例的實例 ID 和私有 IP?
AutoscalingGroup 構造是這樣的:
from aws_cdk import (
core,
aws_ec2,
aws_autoscaling
)
autoscaling_group = aws_autoscaling.AutoScalingGroup(
self,
id="AutoscalingGroup",
instance_type=aws_ec2.InstanceType('m5.xlarge'),
machine_image=aws_ec2.MachineImage.latest_amazon_linux(),
vpc=Myvpc,
vpc_subnets=aws_ec2.SubnetSelection(subnet_type=aws_ec2.SubnetType.PUBLIC),
associate_public_ip_address=True,
desired_capacity=1,
key_name='MySSHKey'
)
非常感謝。
您可以使用boto3
檢索它們。
這是一個僅針對正在運行的實例獲取它們的示例:
ec2_res = boto3.resource('ec2')
instances = ec2_res.instances.filter(
Filters=[
{'Name': 'instance-state-name', 'Values': ['running']}
]
)
for instance in instances:
print(instance.id, instance.instance_type, instance.private_ip_address)
您可以在此處查看文檔以獲取可用參數,並在此處查看 boto3 調用。
如果要過濾特定名稱,則必須簽入實例的標簽:
for instance in instances:
for tag in instance.tags:
if (tag.get('Key') == 'Name') and (tag.get('Value') == '<The name of your instance>'):
print(instance.id, instance.instance_type, instance.private_ip_address)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.