如何使用 Python boto3 检索 VPC ID 和子网 ID?

问题描述

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

获取的信息:

Information to be fetched

我曾尝试使用 boto3,但它似乎不起作用。 非常感谢任何帮助。

解决方法

正如 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
)