Templates

Shaun edited this page Jun 16, 2017 · 9 revisions

Go has an excellent and expandable text/template library which is utilised by Qaz for additional logic in creating templates. To read more on Go Template see Here. All features of Go Template are supported in Qaz.

Templating in Qaz

We'll run through some basics to get started.

asciicast

Templating In-Action

To demonstrate the process of creating templates with Qaz we'll start with a simple VPC Cloudformation block.

Single Mappings

config.yml

project: test
stacks:
  vpc:
    cf:
      cidr: 10.10.0.0/16

template.yml

Resources:
  {{ .name }}:
    Type: "AWS::EC2::VPC"
    Properties:
      CidrBlock: {{ .vpc.cidr }}

Outputs:

$ ls
config.yml  template.yml

$ qaz generate -t vpc::template.yml

Resources:
  vpc:
    Type: "AWS::EC2::VPC"
    Properties:
      CidrBlock: 10.10.0.0/16

The template values wrapped by {{ }} were swapped for their respective values defined in config. Note that {{ .name }} will always resolve to the name of the stack calling the template, this allows Qaz to use the same template for multiple stacks and generate dynamic content based on the stack calling the template.

If-Statements

Sometimes it may become necessary to add specific content to a template based on a particular config value. In this example we'll demonstrate an if-statement in Go Template logic by using the same template to generate dynamic content for 2 different stacks.

config.yml

project: test
stacks:
  oneVPC:
    cf:
      cidr: 10.10.0.0/16

  twoVPC:
    cf:
      cidr: 192.168.0.0/16

template.yml

Resources:
  {{ .name }}:
    Type: "AWS::EC2::VPC"
    Properties:
    {{ if eq .name "oneVPC" }}
      CidrBlock: {{ .oneVPC.cidr }}
    {{ else }}
      CidrBlock: {{ .twoVPC.cidr }}
    {{ end }}

{{ if eq .name "oneVPC" }} _simply says: if stack name equals oneVPC.

Outputs:

$ ls
config.yml template.yml

$ ## let's generate the onvVPC stack
$ qaz generate -t oneVPC::template.yml

Resources:
  oneVPC:
    Type: "AWS::EC2::VPC"
    Properties:
      CidrBlock: 10.10.0.0/16

$ ## now for twoVPC
$ qaz generate -t twoVPC::template.yml

Resources:
  twoVPC:
    Type: "AWS::EC2::VPC"
    Properties:

      CidrBlock: 192.168.0.0/16

Loops

Looping over items in config is something you'll probably be doing a lot. This example demonstrates a loop using Go Template.

Let's say I want a template that generates VPCs based on the amount of CIDR blocks I specify in my config.

config.yml

project: test
stacks:
  vpc:
    cf:
      cidrs:
        - 10.10.0.0/16
        - 10.20.0.0/16
        - 10.30.0.0/16

template.yml

Resources:
{{ range $index, $cidr := .vpc.cidrs }}
  {{ $.name }}00{{ $index }}:
    Type: "AWS::EC2::VPC"
    Properties:
      CidrBlock: {{ $cidr }}
{{ end }}

Important: The value, {{ $.name }} is the same as {{ .name }}. Due to how scoping works with Go's template library, Config values need to be referenced in this way when looping.

{{ range $index, $cidr := .vpc.cidrs }} is a loop over the .vpv.cidrs yaml list.

Outputs:

$  qaz-test qaz generate -t vpc::template.yml
Resources:

  vpc000:
    Type: "AWS::EC2::VPC"
    Properties:
      CidrBlock: 10.10.0.0/16

  vpc001:
    Type: "AWS::EC2::VPC"
    Properties:
      CidrBlock: 10.20.0.0/16

  vpc002:
    Type: "AWS::EC2::VPC"
    Properties:
      CidrBlock: 10.30.0.0/16

Templates do not have to be Cloudformation. By using the Go template library Qaz is able to operate at a string manipulation level, meaning, any file can be templated and written using Qaz.

For more information on the cool stuff you can do using Go Templates See Here

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.