问题描述
我有一个 configtemplate
,它描述了我在环境中想要的东西。
例如:
I have a configtemplate
which describes things I want in my environment.For example:
OptionSettings:
- Namespace: aws:autoscaling:asg
OptionName: MinSize
Value: 1
我在我的环境(模板名称)中使用了这个:
I use this in my environment (templatename):
appEnvironment:
Type: AWS::ElasticBeanstalk::Environment
Properties:
EnvironmentName: xx
ApplicationName: xx
TemplateName: !Ref appConfiguration
VersionLabel: xx
效果很好。但是现在我需要添加一个新的 optionsetting
,其中包含环境的URL:
It works well. But now I need to add a new optionsetting
which contains the URL of the environment:
- Namespace: aws:elasticbeanstalk:application:environment
OptionName: URL
Value: !GetAtt appEnvironment.EndpointURL
但这无法正常工作,因为现在我依赖于环境
和环境
依赖 template
。
But this does not work because now I have a dependency on environment
and environment
has a dependency on template
.
有人可以向我解释如何在一个模板中正确处理吗?我无法想象没有其他人面临这个问题。
Can someone explain to me how to handle this properly in one template? I can't imagine there aren't others who are facing this problem.
谢谢
推荐答案
这可能比您想了解的要多,但是我使用构建系统来处理类似的事情,希望对您有所帮助。它使我的Cloudformation模板更加整洁,我强烈建议您使用它。我的构建系统由Nunjucks& Gulp
This is probably more than you want to get into, but I use build system to handle things like this and hopefully this will help someone. It makes my Cloudformation templates much cleaner, and I highly recommend it. My build system consists of Nunjucks & Gulp
那能让我做什么?首先,我可以例如将变量/数据添加到.json文件。让我们举一个简单的例子,dev和prod具有两个不同的端点。我将创建这两个文件:
What does that allow me to do? First, I can for example add variables/data to .json files. Lets take a simple example, dev and prod having two different endpoints. I would create these two files:
data-dev.json
{
"url": "my-dev-endpoint.com"
}
data-prod.json
{
"url": "my-prod-endpoint.com"
}
configtemplate
- Namespace: aws:elasticbeanstalk:application:environment
OptionName: URL
Value: {{ url }}
{{url}}
是一个Nujucks变量;如果您对Nunjucks不熟悉,则可以使用任何模板系统(把手,小胡子等),尽管Nunjucks是我经验中功能最全面的。现在我们需要做的就是创建一个构建脚本,该脚本将从data-dev.json或data-prod.json中获取我们的 url
值,并替换 {{url}}
与我们的.json文件中的值一起在我们的 configtemplate
中。
{{ url }}
is a Nujucks variable; if you aren't familiar with Nunjucks, you could use any templating system (Handlebars, Mustache, etc.) although Nunjucks is the most fully featured in my experience. Now all we need to do is create a build script which will get our url
value from either data-dev.json or data-prod.json, and replace {{ url }}
in our configtemplate
with the value from our .json file.
这是Gulp出现的地方。如果您不熟悉Gulp,这应该可以帮助您入门。它很容易学习,一天之内就能使事情顺利进行:
This is where Gulp comes in. This should get you started if you aren't familiar with Gulp; it's quite simple to learn, in a day or so you should be able to get things going:
gulpfile.js
const gulp = require('gulp');
const nunjucksRender = require('gulp-nunjucks-render');
const data = require('gulp-data');
const fs = require('fs');
gulp.task('default', function(){
return gulp.src(['src'])
.pipe(data(function() {
return JSON.parse(fs.readFileSync('./dev-data.json'))
}))
.pipe(nunjucksRender({path: ['src/nunjucks']}))
.pipe(gulp.dest('dist'));
});
gulp.src(['src'])
定义源 configtemplate
文件的位置;在此示例中,它是一个名为src的文件夹。 ./ data-dev.json
是我们的数据文件,因此该gulp任务会将 {{url}}
替换为 my-dev-endpoint.com
(您将为data-prod.json写另一个gulp任务)。 nunjucksRender({path:['src / nunjucks']})
是文件夹的路径,在这种情况下可以为空(有关您可以放在此文件夹中)。 gulp.dest('dist')
定义要输出到的位置,在本例中为/ dist文件夹。
gulp.src(['src'])
defines where your source configtemplate
file is; in this example, it's a folder named src. ./data-dev.json
is our data file, so this gulp task will replace {{ url }}
with my-dev-endpoint.com
(you'll write another gulp task to for data-prod.json). nunjucksRender({path: ['src/nunjucks']})
is a path to a folder, in this case it can be empty (more info below on what you could put in this folder). gulp.dest('dist')
defines the location of where you want the output to go, in this case a /dist folder.
如果您有时间解决这个问题,从长远来看,这是值得的。 Nunjucks是一个模板系统,听起来很像。除了像本例中那样注入数据外,您还可以为常用代码创建模板。在CF中,我经常使用此块(请注意,我使用.json而不是yaml;该细节无关紧要):
If you have the time to get into this, it is well worth it in the long run. Nunjucks is a templating system, which is what it sounds like; more than injecting data like we did in this example, you can create templates for oft used code. In CF I use this chunk quite often (note that I use .json not yaml; that detail is irrelevant):
{
"Namespace": "the-namespace",
"OptionName": "the-option",
"Value": "the-value"
}
我没有写很多遍,而是将其放在模板 my-template.nunjucks
;按照上面的示例,此模板将放在一个文件夹中: src / nunjucks
。然后,每当我需要CF模板中的这段代码时,我都可以执行此操作:
Instead of writing that dozens of times, I put it in a template my-template.nunjucks
; following the example above, this template would go in a folder: src/nunjucks
. Then whenever I need this chunk of code in a CF template, I can just do this one liner:
{%include my-template。 nunjucks%}
configtemplate 源文件:
- Namespace: aws:elasticbeanstalk:application:environment
OptionName: URL
Value: {{ url }}
{% include "my-template.nunjucks" %}
将输出
- Namespace: aws:elasticbeanstalk:application:environment
OptionName: URL
Value: my-dev-endpoint.com
{
"Namespace": "the-namespace",
"OptionName": "the-option",
"Value": "the-value"
}
(显然,像这样混合yaml和.json是没有意义的,这只是一个示例,展示了如何使用Nunjucks来 include 文件)
(obviously mixing yaml and .json like this makes no sense, it's just an example to show how Nunjucks can be used to include files)
这篇关于AWS Cloudformation:如何管理configtemplate和环境之间的关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!