cloudformation堆栈部署完成后返回FilesystemID

问题描述

我正在尝试使用 boto3 在 lambda 中使用 cloudformation 模板创建 efs 文件系统。并且有兴趣使用describe_stack从堆栈返回作为Filesystemid的输出。但是我得到空值作为回报。请指出我哪里出错了。

错误是:

Response
null

代码是:

import boto3
import time
import json
import botocore

datetime = time.strftime("%Y%m%d%H%M%s")
stackname = 'My-EFS'
region = "ap-south-1"
client = boto3.client('cloudformation')
s = boto3.Session(region_name=region)


def lambda_handler(event,context):

    response = client.create_stack(
      StackName= stackname,TemplateURL='https://cloudnaeem.s3.amazonaws.com/efs.yaml',)
    waiter = client.get_waiter('stack_create_complete')
    res = waiter.wait(
            StackName=stackname,)
     
    stack = client.describe_stacks(StackName=stackname)
    FileSystem_id=None
    
    for v in stack["Stacks"][0]["Outputs"]:
        if v["OutputKey"] == "FileSystemId":
            FileSystem_id = v["OutputValue"]
    
    return FileSystem_id

模板输出为:

Outputs:
  EFS:
    Description: The created EFS 
    Value: !Ref EFSFileSystem

解决方法

您的输出名为 EFS,但您正在寻找 FileSystemId。您的代码应该是这样的:

    for v in stack["Stacks"][0]["Outputs"]:
        if v["OutputKey"] == "EFS":
            FileSystem_id = v["OutputValue"]