簡體   English   中英

子參數參數的Cloudformation嵌套堆棧模板ValidationError

[英]Cloudformation nested stack template ValidationError for child-to-child parameter

我制作了一個嵌套的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:在嵌套堆棧之間傳遞值

您不能直接從另一個模板中的一個模板(通過Ref )引用資源,即使它們是父子模板或同級模板。 資源是特定於堆棧的,除非它們通過“ Output部分顯式導出。

因此,您有2個選擇:

選項1:通過“ Output部分從子模板1導出您關心的BatchScatterGatherSubmissionActivity值,然后將這些值導入子模板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

缺點之一是您不能“ DependsOn”。 而且,從字面上看,子模板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" }
    }
  }
}

在這種情況下,直到子堆棧1准備好並輸出其值時才創建子堆棧2,因此隱含了“ DependsOn”。 但是在這種情況下,子模板2不依賴於子模板1。相反,它取決於滿足其輸入參數的任何內容(可以是父堆棧,也可以是其他內容)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM