CloudFormation - 在子嵌套堆栈中访问父堆栈的输出

CloudFormation - Access Output of Parent Stack in Child Nested stack

我有一个主 Cloudformation 模板,它调用两个子模板。我有我的第一个模板 运行 和资源的 Outputs 部分中捕获的输出。我在嵌套的第二个模板中使用 ChildStack01 输出值进行了很多尝试,但我不确定为什么会得到 Template format error: Unresolved resource dependencies [XYZ] in the Resources block of the template。这是我的主模板。

{
"AWSTemplateFormatVersion": "2010-09-09",
"Resources": {
    "LambdaStack": {
        "Type": "AWS::CloudFormation::Stack",
        "Properties": {
            "TemplateURL": "https://s3.amazonaws.com/bucket1/cloudformation/Test1.json",
            "TimeoutInMinutes": "60"
        }
    },
    "PermissionsStack": {
        "Type": "AWS::CloudFormation::Stack",
        "Properties": {
            "TemplateURL": "https://s3.amazonaws.com/bucket1/cloudformation/Test2.json",
            "Parameters": {
                "LambdaTest": {
                    "Fn::GetAtt": ["LambdaStack", "Outputs.LambdaTest"]
                }
            },
            "TimeoutInMinutes": "60"
        }
    }
}
}

这是我的Test1.json模板

{
"Resources": {
    "LambdaTestRes": {
        "Type": "AWS::Lambda::Function",
        "Properties": {
            "Description": "Testing AWS cloud formation",
            "FunctionName": "LambdaTest",
            "Handler": "lambda_handler.lambda_handler",
            "MemorySize": 128,
            "Role": "arn:aws:iam::3423435234235:role/lambda_role",
            "Runtime": "python2.7",
            "Timeout": 300,
            "Code": {
                "S3Bucket": "bucket1",
                "S3Key": "cloudformation/XYZ.zip"
            }
        }
    }
},
"Outputs": {
    "LambdaTest": {
        "Value": {
            "Fn::GetAtt": ["LambdaTestRes", "Arn"]
        }
    }
}
}

这是我的 Test2.json,它必须使用 Test1.json 的输出。

{
"Resources": {
    "LambdaPermissionLambdaTest": {
        "Type": "AWS::Lambda::Permission",
        "Properties": {
            "Action": "lambda:invokeFunction",
            "FunctionName": {
                "Ref": "LambdaTest"
            },
            "Principal": "apigateway.amazonaws.com",
            "SourceArn": {
                "Fn::Join": ["", ["arn:aws:execute-api:", {
                    "Ref": "AWS::Region"
                }, ":", {
                    "Ref": "AWS::AccountId"
                }, ":", {
                    "Ref": "TestAPI"
                }, "/*"]]
            }
        }
    }
},
"Parameters": {
   "LambdaTest": {
       "Type": "String"
   }
}
}

错误来自 test1.json(LambdaStack)。

Logical ID
An identifier for the current output. The logical ID must be alphanumeric (a-z, A-Z, 0-9) and unique within the template.

您似乎有两个同名的逻辑 ID "LambdaTest",一个在资源部分,另一个在输出部分。

您在 Test2.json 中有两个未解决的 Ref 资源依赖关系,一个到 LambdaTest,一个到 TestAPI

对于 LambdaTest,您似乎正试图将其作为父堆栈的参数传递,但您尚未将其指定为子 Test2.json 模板中的输入参数.在 Test2.jsonParameters 部分添加一个条目,如下所示:

"Parameters": {
  "LambdaTest": {
    "Type": "String"
  }
},

关于 TestAPI,这个引用似乎没有出现在您的模板中的其他任何地方,因此您应该直接将其指定为固定字符串,或者在您的 Test2.json 中添加另一个输入参数堆栈(见上文),然后从父堆栈提供它。

仅仅有输出是不够的,你需要导出那个输出。 看这里:http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/using-cfn-stack-exports.html 所以你需要这样的东西:

"Outputs": {
    "LambdaTest": {
        "Value": {
            "Fn::GetAtt": ["LambdaTestRes", "Arn"]
        }
        "Export": {
            "Name": "LambdaTest"
        }
    }
}