【问题标题】:aws cloud formation nested stack exporting/ Importing valuesaws cloudformation 嵌套堆栈导出/导入值
【发布时间】:2018-05-11 11:55:58
【问题描述】:

我正在为自动化 aws 基础架构创建云形成嵌套模板。基本上我的父配置如下

"Resources": {
    "VPCStack": {
      "Type": "AWS::CloudFormation::Stack",
      "Properties": {
        "Parameters": {
                    "ClassB": {                      

                           "Ref": "ClassB"                     

                    }                
                },
          "TemplateURL": "https://s3.us-east-2.amazonaws.com/xxx/vpc.json",
          "TimeoutInMinutes": "5"
      }
    },
    "NatStack": {
      "Type": "AWS::CloudFormation::Stack",
      "Properties": {
        "Parameters": {
                    "ParentVPCStack": { "Ref": "AWS::StackName" },
                    "SubnetZone": {"Ref": "SubnetZone"}           
                },
          "TemplateURL": "https://s3.us-east-2.amazonaws.com/xxx/nat-gateway.json",
          "TimeoutInMinutes": "5"
      },
      "DependsOn": "VPCStack"
    }
  }

以上将触发嵌套 CF,我在 VPC 中有如下导出输出

"Outputs": {
                "StackName": {
            "Description": "Stack name.",
            "Value": {
                "Fn::Sub": "${AWS::StackName}"
            }
        },
        "AZs": {
            "Description": "AZs",
            "Value": 2,
            "Export": {
                "Name": {
                    "Fn::Sub": "${AWS::StackName}-AZs"
                }
            }
        },
        "AZA": {
            "Description": "AZ of A",
            "Value": {
                "Fn::Select": [
                    0,
                    {
                        "Fn::GetAZs": ""
                    }
                ]
            },
            "Export": {
                "Name": {
                    "Fn::Sub": "${AWS::StackName}-AZA"
                }
            }
        },
        "AZB": {
            "Description": "AZ of B",
            "Value": {
                "Fn::Select": [
                    1,
                    {
                        "Fn::GetAZs": ""
                    }
                ]
            },
            "Export": {
                "Name": {
                    "Fn::Sub": "${AWS::StackName}-AZB"
                }
            }
        },
        "ClassB": {
            "Description": "Class B.",
            "Value": {
                "Ref": "ClassB"
            },
            "Export": {
                "Name": {
                    "Fn::Sub": "${AWS::StackName}-ClassB"
                }
            }
        },
        "VPC": {
            "Description": "VPC.",
            "Value": {
                "Ref": "VPC"
            },
            "Export": {
                "Name": {
                    "Fn::Sub": "${AWS::StackName}-VPC"
                }
            }
        },
        "SubnetsPublic": {
            "Description": "Subnets public.",
            "Value": {
                "Fn::Join": [
                    ",",
                    [
                        {
                            "Ref": "SubnetAPublic"
                        },
                        {
                            "Ref": "SubnetBPublic"
                        }
                    ]
                ]
            },
            "Export": {
                "Name": {
                    "Fn::Sub": "${AWS::StackName}-SubnetsPublic"
                }
            }
        },
        "SubnetsPrivate": {
            "Description": "Subnets private.",
            "Value": {
                "Fn::Join": [
                    ",",
                    [
                        {
                            "Ref": "SubnetAPrivate"
                        },
                        {
                            "Ref": "SubnetBPrivate"
                        }
                    ]
                ]
            },
            "Export": {
                "Name": {
                    "Fn::Sub": "${AWS::StackName}-SubnetsPrivate"
                }
            }
        },
        "RouteTablePrivate": {
            "Description": "Route table private (deprecated in v4, will be removed in v5).",
            "Value": {
                "Ref": "RouteTablePrivate"
            },
            "Export": {
                "Name": {
                    "Fn::Sub": "${AWS::StackName}-RouteTablePrivate"
                }
            }
        }

然后我有 nat 网关,VPC 创建也没有问题,但是在创建 nat 网关时出现此错误 找不到名为 ${stack-name}-RouteTableAPrivate 的导出

我的主 CF 中有这些输出

"Outputs": {

   "StackRef": {"Value": { "Ref" : "VPCStack"}},

     "Output1FromNestedmyVPCStack" : {

          "Value": { "Fn::GetAtt": [ "VPCStack", "Outputs.RouteTableAPrivate" ] }

      }

   }
}

我该如何解决这个问题

【问题讨论】:

    标签: amazon-web-services amazon-cloudformation


    【解决方案1】:

    您的导出值在 RouteTable 和 Private 之间没有 A,而您的引用值则 (RouteTableAPrivate)

    【讨论】:

    • 这里。 "RouteTableAPrivate": { "Description": "Route table A private.", "Value": { "Ref": "RouteTablePrivate" }, "Export": { "Name": { "Fn::Sub": "$ {AWS::StackName}-RouteTableAPrivate" } }
    • 我从这里导出并从 nat gateway cf 导入
    • "Route": { "Type": "AWS::EC2::Route", "Properties": { "RouteTableId": { "Fn::ImportValue": { "Fn::Sub ": "${ParentVPCStack}-RouteTable${SubnetZone}Private" } }
    • json 模板很难阅读,所以不要在此处粘贴更多 json,而是编辑您的问题
    【解决方案2】:

    我怀疑这是因为您的嵌套堆栈中有“${AWS::StackName}-RouteTablePrivate”。

    嵌套堆栈会生成您事先不知道的名称。

    因此,只需将其命名为您的资源“RouteTablePrivate”。这应该都是基于我的假设。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-21
      • 1970-01-01
      • 2020-09-17
      • 2017-11-25
      • 2019-10-28
      • 1970-01-01
      相关资源
      最近更新 更多