Velocity for complex CloudFormation templates

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.

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")

Before:

After:

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!

Similar Posts You Might Enjoy

Velocity für komplexe CloudFormation-Templates

Cloudformation als Beschreibungssprache Mit AWS CloudFormation lassen sich (fast) alle AWS Dienst in einer Konfigurationssprache beschreiben. Das ermöglicht Scripten von AWS-Infrastruktur. So sind die AWS Ressourcen gut dokumentiert. Die Templates können auch in Konfigurationssystemen abgelegt und versioniert werden, z.B. CodeCommit. CloudFormation hat also viele Vorteile. Ein Nachteil ist jedoch, dass die CloudFormation Templates schnell sehr groß werden. Was tun? Zum Glück gibt es verschiedene Ansätze, der Komplexität Herr zu werden. - by Gernot Glawe

Clouds - einfaches Management von CloudFormation Templates

Infrastruktur als Code ist eine (Heraus)Forderung von DevOps. Die Infrastrukturbeschreibungssprache von AWS heißt CloudFormation. Hat man sich erstmal eingearbeitet, greift man immer häufiger zum Texteditor statt zur Console, um AWS Ressourcen zu erstellen. Und eh man es sich versieht, hat man eine unüberschaubare Anzahl von Cloudformation Templates. Und dann steht man vor der Aufgabe, diese zu verwalten. Hier wollen wir ein paar Möglichkeiten dafür vorstellen. - by Gernot Glawe

Einleitung zu tRick

Wer träumt nicht davon das Infrastructure as Code Framwork zu benutzen, das die eigenen Anforderungen bestens erfüllt? Aber wie findet man nun dieses eine richtige Framework für seinen Anwendungsfall, den “Alleskönner” oder den “Spezialisten”? Auch wir bei tecRacer haben immer wieder mit unterschiedlichsten Infrastructure as Code Frameworks zu tun. Bei unseren Projekten im AWS Umfeld steht uns eine große Zahl an möglichen Tools zur Verfügung. Aber worin genau besteht der Vorteil von Infrastructure as Code Frameworks gegenüber einer manuellen Bereitstellung von Infrastruktur-Resourcen? - by Marco Tesch