'''
import json
替换这里的路径为你的tfstate文件的实际路径
tfstate_path = 'path_to_your_tfstate_file.tfstate'
要寻找的子网名字
subnet_name_to_find = 'dev-s4-vpc-sbnt-cmp1'
读取tfstate文件
with open(tfstate_path) as file:
tfstate = json.load(file)
初始化subnet_id为None,这样我们就可以检查是否找到了子网ID
subnet_id = None
遍历tfstate文件中的资源
for resource in tfstate['resources']:
if resource['type'] == 'aws_subnet' and resource['mode'] == 'managed':
for instance in resource['instances']:
if instance['attributes']['tags']['Name'] == subnet_name_to_find:
subnet_id = instance['attributes']['id']
break
检查是否找到了ID并打印
if subnet_id:
print(f"Subnet ID for '{subnet_name_to_find}': {subnet_id}")
else:
print(f"No subnet ID found for '{subnet_name_to_find}'")