Velocity for complex CloudFormation templates
This content is more than 4 years old and the cloud moves fast so some information may be slightly out of date.
Cloudformation as a description language
With AWS CloudFormation, (almost) all AWS service can be described in a configuration language. This enables scripting of AWS infrastructure. Thus, AWS resources are well documented. The templates can also be stored and versioned in configuration systems, e.g. CodeCommit. So CloudFormation has many advantages. However, one disadvantage is that CloudFormation templates quickly become very large. What to do? Fortunately, there are several approaches to deal with the complexity.
Here we want to present a simple approach of a different kind: Using the Velocity template engine.
Velocity is kept very simple as a template language.
Commands are hashed #
, objects are referenced with $
.
For easy structuring of large Cloudformation templates, the large files can be split into many small files.
These are then inserted into the master template.
#include("datei.cf")
causes an insertion as text,
#parse("datei.cf")
causes an insertion as velocity template. This means that the inserted file is also interpreted by the velocity template engine. So with parse also multiple nestings can be realized.
Velocity Template Example
As implementation of the template engine we do not take java, but the “lighter” node implementation as npm package velocity. ( velocity. ) So you need:
-
Eine Installation von Node.js
-
Den Node Package Manager npm, der in der Node Installation enthalten sein sollte
-
Das Packet Velocity. Dies installiert man auf der Kommandozeile mit “npm install velocity”
-
Eine große Cloudformation Template Datei. Im Beispiel nehmen wir das Beispiel “Word Press Basic Instance” von aws.
-
An installation of Node.js
-
The Node Package Manager npm, which should be included in the Node installation.
-
The package velocity. This is installed on the command line with
npm install velocity
. -
A large Cloudformation template file. In the example, we’ll take the “Word Press Basic Instance” example from aws.
At the beginning we have one big file with 365 lines. This quickly becomes confusing. This can be done easier. I copy the original file “bigfile.template” into a new file “smallfile.template”. I replace the lines 74 to 129 with a single include line
#include("mappings.vm")
I put the previously copied content (lines 74-129) into a new file “mappings.vm”. To get a better structure, I save the file in a new subdirectory “include”:
he extension “vm” stands for velocity macros. So you get an idea how to structure large files by subdirectories. Useful subdirectories can be:
- Security Groups
- Subnet
- Instances
- Routing Tables
- …
How do I put the files back together into one big file now? I create a configuration file for node-velocity and call the template engine:
Simple configuration of node-velocity
In the configuration file we only have to tell the Velocity Engine that we have a subdirectory to search for include files:
module.exports = {
root: \['.','./include'\]
}
More directories can be added in the array root. Generating the whole file is then a single command on the command line
velocity -t smallfile.template -c velocity-config.js -o bigfile-new.template
Parameters:
-t The template to interpret, i.e. the input.
-c The configuration file
-o The output
The generated file is identical to the old big file:
Have fun trying it out, velocity offers even more possibilities!