如何获取使用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'
)非常感谢。
发布于 2021-01-13 00:05:10
您可以使用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)您可以在文档here中查看可用参数,并查看boto3调用的here。
如果您想要根据特定名称进行过滤,则必须签入实例的标签:
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)https://stackoverflow.com/questions/64299718
复制相似问题