首页 > 解决方案 > 如何使用 Python boto3 检索 VPC ID 和子网 ID?

问题描述

我希望看到一个可以在部署堆栈后获取 VPC id 和子网 ID 的 python 脚本。

要获取的信息:

需要获取的信息

我曾尝试使用 boto3,但它似乎不起作用。很感谢任何形式的帮助。

标签: pythonamazon-web-servicesamazon-cloudformationboto3amazon-eks

解决方案


正如 jordanm 提到的,您可以使用describe_stacks()来获取该堆栈创建的所有资源

import boto3

def lambda_handler(event, context):
    client = boto3.client('cloudformation')
    
    response = client.describe_stacks(
        StackName="eks-sample-vpc"
    )
    return print(response)

或者你可以使用describe_stack_resource()

response = client.describe_stack_resource(
    StackName="eks-sample-vpc",
    LogicalResourceId="Subnet01" #Logical ID in you template
)

推荐阅读