AWS CloudFormation:在嵌套堆栈之间传递值 [英] AWS CloudFormation: Passing Values between Nested Stacks

查看:100
本文介绍了AWS CloudFormation:在嵌套堆栈之间传递值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更多AWS问题!好的,所以这个主意是一个主模板调用所有嵌套堆栈。在这里的帮助下,我弄清楚了如何将参数从主服务器传递到嵌套堆栈。现在,我试图找出如何将值从嵌套堆栈传递到嵌套堆栈。我认为这应该通过进出口来完成,但我认为我不太正确。我不确定是我的进口商品还是出口商品错了。

More AWS questions! Ok, so the idea is one master template calls all the nested stacks. With help from here I figured out how to pass parameters from the master to the nested stacks. Now I am trying to figure out how to pass values from nested stacks to nested stacks. I believe this should be done via Exports and Imports, but I don't think I have this quite right. I'm not sure if it's my imports or exports that are wrong.

我得到的错误是:

No export named TestStack1-VpcStackID found. Rollback requested by user.

Master:

{
    "AWSTemplateFormatVersion" : "2010-09-09",
    "Description" : "Master template",
    "Parameters" : {
        "availabilityZone" : {
            "Default" : "us-east-1d",
            "Description" : "Enter AvailabilityZone.",
            "Type" : "String"
        },
        "VpcCidrBlock" : {
            "Default" : "10.0.0.0/16",
            "Description" : "VPC CIDR Block.",
            "Type" : "String"
        },
        "PublicSubnetCidrBlock" : {
            "Default" : "10.0.0.0/24",
            "Description" : "Public subnet CIDR block.",
            "Type" : "String"
        }
    },
    "Resources" : {
        "VpcStack" : {
            "Type" : "AWS::CloudFormation::Stack",
            "Properties" : {
                "Parameters" : {
                    "VpcCidrBlock" : {
                        "Ref" : "VpcCidrBlock"
                    }
                },
                "TemplateURL" : "https://s3.amazonaws.com/url/templates/vpcStack.json",
                "TimeoutInMinutes" : "5"
            }
        },
        "PublicRouteStack" : {
            "Type" : "AWS::CloudFormation::Stack",
            "Properties" : {
                "Parameters" : {
                    "PublicSubnetCidrBlock" : {
                        "Ref" : "PublicSubnetCidrBlock"
                    },
                    "VpcStack" : {
                        "Fn::ImportValue" : {
                            "Fn::Sub" : "${AWS::StackName}-VpcStackID"
                        }
                    }
                },
                "TemplateURL" : "https://s3.amazonaws.com/url/templates/publicRouteStack.json",
                "TimeoutInMinutes" : "5"
            }
        }
    }
}

VpcStack (嵌套-我认为我的输出不正确):

VpcStack (Nested - I don't think I'm outputting right):

{
    "AWSTemplateFormatVersion" : "2010-09-09",
    "Description" : "VPC template",
    "Parameters" : {
        "VpcCidrBlock" : {
            "Description" : "Vpc CIDR Block.",
            "Type" : "String"
        }
    },
    "Resources" : {
        "VpcStack" : {
            "Type" : "AWS::EC2::VPC",
            "Properties" : {
                "EnableDnsSupport" : "true",
                "EnableDnsHostnames" : "true",
                "CidrBlock" : {
                    "Ref" : "VpcCidrBlock"
                },
                "Tags" : [
                    {
                        "Key" : "Application",
                        "Value" : {
                            "Ref" : "AWS::StackName"
                        }
                    }
                ]
            }
        }
    },
    "Outputs" : {
        "VpcStack" : {
            "Description" : "VPC Stack ID.",
            "Value" : {
                "Ref" : "VpcStack"
            },
            "Export" : {
                "Name" : {
                    "Fn::Sub" : "${AWS::StackName}-VpcStackID"
                }
            }
        }
    }
}

PublicStubnetStack (我认为这是失败的地方):

PublicStubnetStack (I think this is where it is failing):

{
    "AWSTemplateFormatVersion" : "2010-09-09",
    "Description" : "Public Subnet Stack",
    "Parameters" : {
        "PublicSubnetCidrBlock" : {
            "Default" : "10.0.0.0/24",
            "Description" : "Public subnet CIDR block.",
            "Type" : "String"
        },
        "VpcStack" : {
            "Description" : "VPC Stack.",
            "Type" : "String"
        }
    },
    "Resources" : {
        "PublicSubnet" : {
            "Type" : "AWS::EC2::Subnet",
            "Properties" : {
                "VpcId" : {
                    "Ref" : "VpcStack"
                },
                "CidrBlock" : {
                    "Ref" : "PublicSubnetCidrBlock"
                },
                "Tags" : [
                    {
                        "Key" : "Application",
                        "Value" : {
                            "Ref" : "AWS::StackName"
                        }
                    },
                    {
                        " Key" : "Network",
                        "Value" : "Public"
                    }
                ]
            }
        }
    },
    "Outputs" : {
        "PublicSubnet" : {
            "Description" : "Public Subnet ID.",
            "Value" : {
                "Ref" : "PublicSubnet"
            },
            "Export" : {
                "Name" : {
                    "Fn::Sub" : "${AWS::StackName}-PublicSubnetID"
                }
            }
        }
    }
}

抱歉,发布了这么多,我很新

Sorry to be posting so many, I am very new to AWS, and am trying to pick it up quickly.

推荐答案

问题

您的问题是您将值导出为

Your problem is that you are exporting the value as

"Export" : {
  "Name" : {
    "Fn::Sub" : "${AWS::StackName}-VpcStackID"
  }
}

您正在使用 $ {AWS :: StackName} 变量代替将当前堆栈名称添加到您的导出变量名称中。请注意,这是嵌套堆栈的堆栈名称

You are using the ${AWS::StackName} variable which will substitute the current stack name into your export variable name. Note that this is the stack name of your nested stack.

在包装模板中,您尝试将值导入为:

Whereas, in your wrapper template, you are attempting to import the value as:

"Fn::ImportValue" : {
  "Fn::Sub" : "${AWS::StackName}-VpcStackID"
}

同样,您要替换变量 $ {AWS :: StackName} 表示当前堆栈,在这种情况下,是您的包装堆栈

Again, you are substituting the variable ${AWS::StackName} for the current stack, which in this case is your wrapper stack.

请注意,当您使用嵌套堆栈时,实际上是在创建一个新堆栈,因此堆栈名称会根据您所使用的模板而变化。

Note that when you use nested stacks, you are actually creating a new stack, so the stack names change depending on which template you are in.

分辨率

请勿对变量使用导入/导出。

Do not use import/export for your variables.

在嵌套模板中,从输出中删除 Export 元素。您不需要它们。只需使用堆栈参数将值从包装堆栈传递到嵌套堆栈,然后使用堆栈输出将值从嵌套堆栈备份传递回包装堆栈。

In your nested templates, drop the Export element from your outputs. You don't need them. Simply use stack parameters to pass values from your wrapper stack to nested stacks, and use stack outputs to pass values back up from nested stacks to wrapper stacks.

在包装堆栈中,请使用 VpcStack 的输出,像这样:

In your wrapper stack, use the output from VpcStack like this:

"PublicRouteStack" : {
        "Type" : "AWS::CloudFormation::Stack",
        "Properties" : {
            "Parameters" : {
                "PublicSubnetCidrBlock" : {
                    "Ref" : "PublicSubnetCidrBlock"
                },
                "VpcStack" : {
                    "Fn::GetAtt" : [ "VpcStack", "Outputs.VpcStack" ]
                }
            },
            "TemplateURL" : "https://s3.amazonaws.com/url/templates/publicRouteStack.json",
            "TimeoutInMinutes" : "5"
        }
    }

请注意,在这种情况下,我使用的是输出名为 VpcStack 通过 Fn :: GetAtt 函数从 VpcStack 嵌套堆栈中获取。

Note that in this case, I am using the output named VpcStack from your VpcStack nested stack via the Fn::GetAtt function.

PS。为了清楚起见,您应该更改一些名称。尽量避免在各处重复使用相同的名称。它有助于弄清楚事情。

PS. You should change some of your names simply for clarity. Try to avoid reusing the same name all over the place. It helps make things clear.

这篇关于AWS CloudFormation:在嵌套堆栈之间传递值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆