child-to-child 参数的 Cloudformation 嵌套堆栈模板 ValidationError
Cloudformation nested stack template ValidationError for child-to-child parameter
我制作了一个嵌套的 cloudformation parent 模板,然后引用了 4 个 child 模板。当我尝试通过 CLI 命令 aws cloudformation create-stack...
启动堆栈时,出现错误:
An error occurred (ValidationError) when calling the CreateStack
operation: Template error: instance of Fn::GetAtt references undefined
resource BatchScatterGatherSubmissionActivity
这是因为我有一个名为 StepFunctionResourcesStack 的 child 模板,其中包含 BatchScatterGatherSubmissionActivity
和另一个 child 模板 EC2InstanceResourcesStack 引用它。我确保为后一个 child 模板添加一个 DependsOn
子句,但我仍然得到错误。
这是 StepFunctionResourcesStack:
AWSTemplateFormatVersion: '2010-09-09'
Description: step functions resources stack.
Parameters:
StackUID:
Type: String
Resources:
StatesExecutionRole:
Type: "AWS::IAM::Role"
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Principal:
Service:
- !Sub states.${AWS::Region}.amazonaws.com
Action: "sts:AssumeRole"
Path: "/"
Policies:
- PolicyName: StatesExecutionPolicy
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- "lambda:InvokeFunction"
Resource: "*"
MyStateMachine:
Type: "AWS::StepFunctions::StateMachine"
Properties:
#
# The StateMachine definition is substituted in with Jinja2
#
DefinitionString: !Sub
- |
{{ machine_json | indent(10) }}
- {% for item in machine_args -%}
{{ item }}
{% endfor %}
RoleArn: !GetAtt [ StatesExecutionRole, Arn ]
# several of these:
BatchScatterGatherSubmissionActivity:
Type: "AWS::StepFunctions::Activity"
Properties:
Name:
Fn::Join: [ "-", [ "BatchScatterGatherSubmissionActivity", Ref: StackUID] ]
BatchScatterGatherPollingActivity:
Type: "AWS::StepFunctions::Activity"
Properties:
Name:
Fn::Join: [ "-", [ "BatchScatterGatherPollingActivity", Ref: StackUID] ]
BatchGatherActivity:
Type: "AWS::StepFunctions::Activity"
Properties:
Name:
Fn::Join: [ "-", [ "BatchGatherActivity", Ref: StackUID] ]
BatchTriodenovoActivity:
Type: "AWS::StepFunctions::Activity"
Properties:
Name:
Fn::Join: [ "-", [ "BatchTriodenovoActivity", Ref: StackUID] ]
BatchHandoffActivity:
Type: "AWS::StepFunctions::Activity"
Properties:
Name:
Fn::Join: [ "-", [ "BatchHandoffActivity", Ref: StackUID] ]
Outputs:
# These get used in the instance_resources child stack.
BatchScatterGatherSubmissionActivity:
Value: !Ref BatchScatterGatherSubmissionActivity
BatchScatterGatherPollingActivity:
Value: !Ref BatchScatterGatherPollingActivity
BatchGatherActivity:
Value: !Ref BatchGatherActivity
BatchTriodenovoActivity:
Value: !Ref BatchTriodenovoActivity
BatchHandoffActivity:
Value: !Ref BatchHandoffActivity
这里是 parent(嵌套)模板 的相关部分,其中上述输出被传递到 EC2InstanceResourcesStack:
StepFunctionResourcesStack:
Type: AWS::CloudFormation::Stack
Properties:
Parameters:
StackUID:
Ref: StackUID
TemplateURL: https://s3.amazonaws.com/CFNTemplate/step_functions_resources.stack.yaml
Timeout: "100"
EC2InstanceResourcesStack:
Type: AWS::CloudFormation::Stack
Properties:
Parameters:
BatchScatterGatherSubmissionActivity:
Fn::GetAtt: [ "BatchScatterGatherSubmissionActivity", "Outputs.StepFunctionResourcesStack" ]
BatchScatterGatherPollingActivity:
Fn::GetAtt: [ "BatchScatterGatherPollingActivity", "Outputs.StepFunctionResourcesStack" ]
BatchGatherActivity:
Fn::GetAtt: [ "BatchGatherActivity", "Outputs.StepFunctionResourcesStack" ]
BatchTriodenovoActivity:
Fn::GetAtt: [ "BatchTriodenovoActivity", "Outputs.StepFunctionResourcesStack" ]
BatchHandoffActivity:
Fn::GetAtt: [ "BatchHandoffActivity", "Outputs.StepFunctionResourcesStack" ]
Subnet: !Ref Subnet
GPCESSHKeyPair: !Ref GPCESSHKeyPair
GPCESubnetAZ1: !Ref GPCESubnetAZ1
ActivityAndHandoffAnsibleBucketName: !Ref ActivityAndHandoffAnsibleBucketName
ActivityAndHandoffAnsibleKeyName: !Ref ActivityAndHandoffAnsibleKeyName
ActivityAndHandoffDaemonBucketName: !Ref ActivityAndHandoffDaemonBucketName
ActivityAndHandoffDaemonKeyName: !Ref ActivityAndHandoffDaemonKeyName
ActivityAndHandoffDaemonRequirementsBucketName: !Ref ActivityAndHandoffDaemonRequirementsBucketName
ActivityAndHandoffDaemonRequirementsKeyName: !Ref ActivityAndHandoffDaemonRequirementsKeyName
Rkstr8PkgBucketName: !Ref Rkstr8PkgBucketName
Rkstr8PkgKeyName: !Ref Rkstr8PkgKeyName
TemplateURL: https://s3.amazonaws.com/CFNTemplate/instance_resources.stack.yaml
Timeout: "100"
DependsOn: StepFunctionResourcesStack
对于我从 child 导出参数并通过 parent 模板将它们传递给另一个的方法,我遵循了此处答案中提供的方法:
您不能直接从一个模板引用另一模板中的资源(通过 Ref
),即使它们是 parent-child 或同级模板。这些资源是特定于堆栈的,除非它们通过 Output
部分显式导出。
所以你有两个选择:
选项 1: 通过 Output
部分从 child 模板 1 导出您关心的 BatchScatterGatherSubmissionActivity
的值,然后将这些值导入 child 模板 2.
例如:
在 "source" 模板中:
"Outputs" : {
"MyValue" : {
"Value" : {
"Ref" : "BatchScatterGatherSubmissionActivity"
},
"Export" : {
"Name" : "MyExportedValue"
}
}
}
然后在您的 "consuming" 模板中导入值:
{ "Fn::ImportValue" : "MyExportedValue" }
更多信息:https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/using-cfn-stack-exports.html
一个缺点是你不能"DependsOn"。此外,从字面上看,child 模板 2 依赖于 child 模板 1.
选项 2: 从 child 模板中输出您关心 BatchScatterGatherSubmissionActivity
的值(即直到 parent), 然后将这些值从 parent 向下传递到另一个 child.
因此在 child 模板 1 中,您将从 child 堆栈输出值:
"Outputs" : {
"MyValue" : {
"Value" : {
"Ref" : "BatchScatterGatherSubmissionActivity"
}
}
}
在 child 模板 2 中,您可以通过 "Parameters" 部分向模板添加一个参数,然后引用该参数。
"Parameters" : {
"MyParam" : {
"Type" : "String",
"Description" : "The value from elsewhere"
}
}
然后
{ "Ref" : "MyParam" }
最后,通过将 child 模板 1 的输出传递到 child 模板 2 的参数中,将 2 child 堆栈连接到 parent 中:
"ChildStack2": {
"Type" : "AWS::CloudFormation::Stack",
"Properties" : {
"Parameters" : {
"MyParam" : { "Fn::GetAtt" : "Outputs.MyValue" }
}
}
}
在这种情况下,child 堆栈 2 将不会创建,直到 child 堆栈 1 准备好并输出它的值,所以有一个隐含的 "DependsOn"。但在这种情况下,child 模板 2 不依赖于 child 模板 1。相反,它依赖于满足其输入参数的任何东西(可以是 parent 堆栈,或其他东西).
我制作了一个嵌套的 cloudformation parent 模板,然后引用了 4 个 child 模板。当我尝试通过 CLI 命令 aws cloudformation create-stack...
启动堆栈时,出现错误:
An error occurred (ValidationError) when calling the CreateStack
operation: Template error: instance of Fn::GetAtt references undefined
resource BatchScatterGatherSubmissionActivity
这是因为我有一个名为 StepFunctionResourcesStack 的 child 模板,其中包含 BatchScatterGatherSubmissionActivity
和另一个 child 模板 EC2InstanceResourcesStack 引用它。我确保为后一个 child 模板添加一个 DependsOn
子句,但我仍然得到错误。
这是 StepFunctionResourcesStack:
AWSTemplateFormatVersion: '2010-09-09'
Description: step functions resources stack.
Parameters:
StackUID:
Type: String
Resources:
StatesExecutionRole:
Type: "AWS::IAM::Role"
Properties:
AssumeRolePolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: "Allow"
Principal:
Service:
- !Sub states.${AWS::Region}.amazonaws.com
Action: "sts:AssumeRole"
Path: "/"
Policies:
- PolicyName: StatesExecutionPolicy
PolicyDocument:
Version: "2012-10-17"
Statement:
- Effect: Allow
Action:
- "lambda:InvokeFunction"
Resource: "*"
MyStateMachine:
Type: "AWS::StepFunctions::StateMachine"
Properties:
#
# The StateMachine definition is substituted in with Jinja2
#
DefinitionString: !Sub
- |
{{ machine_json | indent(10) }}
- {% for item in machine_args -%}
{{ item }}
{% endfor %}
RoleArn: !GetAtt [ StatesExecutionRole, Arn ]
# several of these:
BatchScatterGatherSubmissionActivity:
Type: "AWS::StepFunctions::Activity"
Properties:
Name:
Fn::Join: [ "-", [ "BatchScatterGatherSubmissionActivity", Ref: StackUID] ]
BatchScatterGatherPollingActivity:
Type: "AWS::StepFunctions::Activity"
Properties:
Name:
Fn::Join: [ "-", [ "BatchScatterGatherPollingActivity", Ref: StackUID] ]
BatchGatherActivity:
Type: "AWS::StepFunctions::Activity"
Properties:
Name:
Fn::Join: [ "-", [ "BatchGatherActivity", Ref: StackUID] ]
BatchTriodenovoActivity:
Type: "AWS::StepFunctions::Activity"
Properties:
Name:
Fn::Join: [ "-", [ "BatchTriodenovoActivity", Ref: StackUID] ]
BatchHandoffActivity:
Type: "AWS::StepFunctions::Activity"
Properties:
Name:
Fn::Join: [ "-", [ "BatchHandoffActivity", Ref: StackUID] ]
Outputs:
# These get used in the instance_resources child stack.
BatchScatterGatherSubmissionActivity:
Value: !Ref BatchScatterGatherSubmissionActivity
BatchScatterGatherPollingActivity:
Value: !Ref BatchScatterGatherPollingActivity
BatchGatherActivity:
Value: !Ref BatchGatherActivity
BatchTriodenovoActivity:
Value: !Ref BatchTriodenovoActivity
BatchHandoffActivity:
Value: !Ref BatchHandoffActivity
这里是 parent(嵌套)模板 的相关部分,其中上述输出被传递到 EC2InstanceResourcesStack:
StepFunctionResourcesStack:
Type: AWS::CloudFormation::Stack
Properties:
Parameters:
StackUID:
Ref: StackUID
TemplateURL: https://s3.amazonaws.com/CFNTemplate/step_functions_resources.stack.yaml
Timeout: "100"
EC2InstanceResourcesStack:
Type: AWS::CloudFormation::Stack
Properties:
Parameters:
BatchScatterGatherSubmissionActivity:
Fn::GetAtt: [ "BatchScatterGatherSubmissionActivity", "Outputs.StepFunctionResourcesStack" ]
BatchScatterGatherPollingActivity:
Fn::GetAtt: [ "BatchScatterGatherPollingActivity", "Outputs.StepFunctionResourcesStack" ]
BatchGatherActivity:
Fn::GetAtt: [ "BatchGatherActivity", "Outputs.StepFunctionResourcesStack" ]
BatchTriodenovoActivity:
Fn::GetAtt: [ "BatchTriodenovoActivity", "Outputs.StepFunctionResourcesStack" ]
BatchHandoffActivity:
Fn::GetAtt: [ "BatchHandoffActivity", "Outputs.StepFunctionResourcesStack" ]
Subnet: !Ref Subnet
GPCESSHKeyPair: !Ref GPCESSHKeyPair
GPCESubnetAZ1: !Ref GPCESubnetAZ1
ActivityAndHandoffAnsibleBucketName: !Ref ActivityAndHandoffAnsibleBucketName
ActivityAndHandoffAnsibleKeyName: !Ref ActivityAndHandoffAnsibleKeyName
ActivityAndHandoffDaemonBucketName: !Ref ActivityAndHandoffDaemonBucketName
ActivityAndHandoffDaemonKeyName: !Ref ActivityAndHandoffDaemonKeyName
ActivityAndHandoffDaemonRequirementsBucketName: !Ref ActivityAndHandoffDaemonRequirementsBucketName
ActivityAndHandoffDaemonRequirementsKeyName: !Ref ActivityAndHandoffDaemonRequirementsKeyName
Rkstr8PkgBucketName: !Ref Rkstr8PkgBucketName
Rkstr8PkgKeyName: !Ref Rkstr8PkgKeyName
TemplateURL: https://s3.amazonaws.com/CFNTemplate/instance_resources.stack.yaml
Timeout: "100"
DependsOn: StepFunctionResourcesStack
对于我从 child 导出参数并通过 parent 模板将它们传递给另一个的方法,我遵循了此处答案中提供的方法:
您不能直接从一个模板引用另一模板中的资源(通过 Ref
),即使它们是 parent-child 或同级模板。这些资源是特定于堆栈的,除非它们通过 Output
部分显式导出。
所以你有两个选择:
选项 1: 通过 Output
部分从 child 模板 1 导出您关心的 BatchScatterGatherSubmissionActivity
的值,然后将这些值导入 child 模板 2.
例如:
在 "source" 模板中:
"Outputs" : {
"MyValue" : {
"Value" : {
"Ref" : "BatchScatterGatherSubmissionActivity"
},
"Export" : {
"Name" : "MyExportedValue"
}
}
}
然后在您的 "consuming" 模板中导入值:
{ "Fn::ImportValue" : "MyExportedValue" }
更多信息:https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/using-cfn-stack-exports.html
一个缺点是你不能"DependsOn"。此外,从字面上看,child 模板 2 依赖于 child 模板 1.
选项 2: 从 child 模板中输出您关心 BatchScatterGatherSubmissionActivity
的值(即直到 parent), 然后将这些值从 parent 向下传递到另一个 child.
因此在 child 模板 1 中,您将从 child 堆栈输出值:
"Outputs" : {
"MyValue" : {
"Value" : {
"Ref" : "BatchScatterGatherSubmissionActivity"
}
}
}
在 child 模板 2 中,您可以通过 "Parameters" 部分向模板添加一个参数,然后引用该参数。
"Parameters" : {
"MyParam" : {
"Type" : "String",
"Description" : "The value from elsewhere"
}
}
然后
{ "Ref" : "MyParam" }
最后,通过将 child 模板 1 的输出传递到 child 模板 2 的参数中,将 2 child 堆栈连接到 parent 中:
"ChildStack2": {
"Type" : "AWS::CloudFormation::Stack",
"Properties" : {
"Parameters" : {
"MyParam" : { "Fn::GetAtt" : "Outputs.MyValue" }
}
}
}
在这种情况下,child 堆栈 2 将不会创建,直到 child 堆栈 1 准备好并输出它的值,所以有一个隐含的 "DependsOn"。但在这种情况下,child 模板 2 不依赖于 child 模板 1。相反,它依赖于满足其输入参数的任何东西(可以是 parent 堆栈,或其他东西).