[英]Writing reusable CloudFormation snippets with AWS::Include and Nested Stacks
I have been using nested stacks in CloudFormation
for several months and they are very useful. 我在
CloudFormation
使用嵌套堆栈已有几个月了,它们非常有用。 So I thought I should spend sometime to make each nested stack reusable to other teams in the org. 因此,我认为我应该花一些时间使每个嵌套堆栈可重用于组织中的其他团队。
I saw the use case of AWS::Include in several places like here and here and it makes good sense to me. 我看到了AWS ::: Use的用例出现在这里和这里的几个地方,这对我来说很有意义。
One approach I have in mind is one snippet for each resource, like an AWS::EC2::Subnet
or AWS::EC2::InternetGateway
which can be included zero or more times into a vpc.json
template, which itself can be used as a nested stack in a larger application. 我想到的一种方法是为每种资源提供一个代码段 ,例如
AWS::EC2::Subnet
或AWS::EC2::InternetGateway
,可以将其零次或多次包含在vpc.json
模板中,该模板本身可以使用作为较大应用程序中的嵌套堆栈。
The snippet does not take any parameters, but can reference a parameter that exists in the parent template. 该代码段不带任何参数,但可以引用父模板中存在的参数。
At first glance this doesn't seem enough to me. 乍一看,这在我看来还不够。 Consider this example:
考虑以下示例:
"PublicSubnet": {
"Type": "AWS::EC2::Subnet",
"Properties": {
"VpcId": {"Ref": "VPC"},
"AvailabilityZone": {
"Fn::Select" : [ "0", { "Fn::GetAZs" : {"Ref": "AWS::Region"} }]
},
"CidrBlock": {
"Fn::FindInMap": ["AZSubnetMap", {
"Fn::Select" : [ "0", { "Fn::GetAZs" : {"Ref": "AWS::Region"} }]},
"PublicSubnet"]},
"MapPublicIpOnLaunch": "true",
"Tags": [..]
}
}
How can I avoid hard coding that "0"
for the AZ in a Subnet snippet for example? 例如,如何避免在子网代码段中将AZ硬编码为
"0"
?
Unfortunately, AWS doesn't provide a way to dynamically update the template as per the requirement. 不幸的是,AWS没有提供根据要求动态更新模板的方法。
I have solved a similar problem using Mustache Templates using Java Library Handle Bars . 我已经使用Java库处理栏使用Moustache模板解决了类似的问题。 Using this library you can generate template on the fly based on the requirements.
使用此库,您可以根据需求即时生成模板。
Hope this helps. 希望这可以帮助。
You will have to use two AWS::Include files located in: 您将必须使用以下两个AWS :: Include文件:
And call them from your MAIN Template: 并从您的MAIN模板中调用它们:
Fn::Transform:
Name: AWS::Include
Parameters:
Location : "s3://yourname/PublicSubnetA.yaml"
Fn::Transform:
Name: AWS::Include
Parameters:
Location : "s3://yourname/PublicSubnetB.yaml"
I 'am trying to find way to send additional parameters or override parameter to AWS::include, as you see it has Parameters: Location: 我正在尝试寻找将其他参数或替代参数发送到AWS :: include的方法,因为您看到它具有Parameters:Location:
Why ti's not understanding more parameters and not just Location, I would be glad to have something like this: 为什么ti不了解更多的参数而不仅仅是位置,我很高兴有这样的东西:
Fn::Transform:
Name: AWS::Include
Parameters:
MySubnetIndex: 0
Location : "s3://yourname/PublicSubnetB.yaml"
I tried this way to send additional parameters: 我尝试过这种方式发送其他参数:
Fn::Transform:
Name: AWS::Include
Parameters:
Location : "s3://my.test/create-ec2.yaml"
EC2Size :
Type: String
Default: "t2.micro"
And got interesting error: 并得到了有趣的错误:
The value of parameter EC2Size under transform Include must resolve to a string, number, boolean or a list of any of these 转换包含下的参数EC2Size的值必须解析为字符串,数字,布尔值或任何这些的列表
Looks like it understand what this is additional parameters, probably it need to be configured little bit different. 看起来它了解这是什么附加参数,可能需要对其进行一些配置。 I was not able to find way to fix this error yet.
我还找不到解决此错误的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.