用于子对象参数

时间:2018-03-08 22:05:09

标签: amazon-web-services nested stack amazon-cloudformation

我制作了一个嵌套的cloudformation父模板,然后引用了4个子模板。当我尝试通过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 的子模板,其中包含BatchScatterGatherSubmissionActivity,然后是另一个引用它的子模板 EC2InstanceResourcesStack 。我确保为后一个子模板添加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

以下是父(嵌套)模板的相关部分,其中上述输出传递到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

对于我从子项导出参数并通过父模板将参数传递给另一个的方法,我按照答案中提供的方法进行了操作:AWS CloudFormation: Passing Values between Nested Stacks

1 个答案:

答案 0 :(得分:2)

您无法直接从另一个模板中的一个模板引用资源(通过Ref),即使它们是父子模板或兄弟模板也是如此。除非通过Output部分明确导出资源,否则资源是特定于堆栈的。

所以你有两个选择:

选项1:通过BatchScatterGatherSubmissionActivity部分从子模板1导出您关心Output的值,然后在子模板2中导入这些值

例如:

在"来源"模板:

"Outputs" : {
  "MyValue" : {
    "Value" : {
      "Ref" : "BatchScatterGatherSubmissionActivity"
    },
    "Export" : {
      "Name" : "MyExportedValue"
    }
  }
}

然后导入"消费"模板:

{ "Fn::ImportValue" : "MyExportedValue" }

更多信息:https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/using-cfn-stack-exports.html

一个缺点是你不能"取决于"。另外,儿童模板2完全取决于儿童模板1。

选项2:从子模板输出您关心BatchScatterGatherSubmissionActivity的值(即直到父级),然后从父级传递这些值对另一个孩子。

因此在子模板1中,您将从子堆栈中输出值:

"Outputs" : {
  "MyValue" : {
    "Value" : {
      "Ref" : "BatchScatterGatherSubmissionActivity"
    }
  }
}

在子模板2中,您可以通过"参数"向模板添加参数。部分,然后引用该参数。

"Parameters" : {
  "MyParam" : {
    "Type" : "String",
    "Description" : "The value from elsewhere"
  }
}

然后

{ "Ref" : "MyParam" }

最后,通过将子模板1的输出传递给子模板2的参数,将2个子堆栈挂钩在父级中:

"ChildStack2": {
  "Type" : "AWS::CloudFormation::Stack",
  "Properties" : {
    "Parameters" : {
      "MyParam" : { "Fn::GetAtt" : "Outputs.MyValue" }
    }
  }
}

在这种情况下,子堆栈2不会被创建,直到子堆栈1准备就绪并输出它的值,因此存在隐含的" DependsOn"。但在这种情况下,子模板2不依赖于子模板1.相反,它取决于任何满足它的输入参数(可以是父堆栈或其他东西)。