Skip to content

Pipeline Templates

Pipeline Templates

In this next section, you'll build out our pipeline file to create our PaaS infrastructure by utilizing a shared pipeline template, which will execute our Terraform from TFE.

  1. Build out the sections for the azure-pipelines.yaml
  2. Deploy our modules using the pipeline onto the dev environment

Pipeline Templates

You have used pipelines in the past by defining what each step will do within the main pipeline file itself either via built-in tasks that are provided by azure, or via custom scripts that you can define in-line. Pipeline templates are user defined custom scripts that can be reused in other pipelines to accomplish similar tasks. You will be using the pipeline templates from this repository here

Briefly examining the azure-pipelines.yaml file, there are a few things to note before you begin building out our pipeline.

On the line that you have the resources block. This will create a resource that is accessible in your pipeline called templates, which gives your pipeline access to all of the pipeline templates available in that repository.

You can set this up like you have shown below.

resources:
  repositories:
    - repository: templates
      type: git
      name: templates-iac-pipeline-deployment
      ref: refs/tags/1.1.0

The parameters blocks lets us define default values to pass into the pipeline templates just like the built-in azure provided tasks. Here, you'll be setting the default value to the parameter authorizeDestructionOfResources to be false so in the case that any configuration change tries to destroy any infrastructure, you will prevent the pipeline from doing so as shown below.

parameters:
- name: authorizeDestructionOfResources
  displayName: Authorize Destruction of Resources
  type: boolean
  default: false

Building out the Pipeline

In Azure Pipelines, there are different types of stages that can run in a pipeline. Some examples are stage, job, and the one you'll be using in this file is the template type, which allows you to use a template within your pipeline.

Terraform Validate

To start building out the pipeline, you'll first want to validate that our Terraform configuration is correct by running terraform validate via the pipeline.

stages:
 - template:  terraform-templates/validate.yml@templates
   parameters:
     environments:
       - "dev"
       - "model"

This template definition can be found here

Terraform Plan

After you've successfully validated your terraform configuration, you can run a terraform plan to see all the resources that will be created. This command will not actually create any infrastructure.

 - template:  terraform-templates/plan.yml@templates
   parameters:
     authorizeDestructionOfResources: ${{ parameters.authorizeDestructionOfResources }}
     environments:
       - "dev"
       - "model"

This template definition can be found here

Terraform Apply

Now that you have confirmed all of the infrastructure that will be created in the plan and it looks correct, you can run a terraform apply to actually provision the infrastructure shown in the plan.

 - template:  terraform-templates/apply.yml@templates
   parameters:
     authorizeDestructionOfResources: ${{ parameters.authorizeDestructionOfResources }}
     environments:
       - "dev"
       - "model"

This template definition can be found here

Testing

Note: We did not include any IaC Pipeline Integration Tests for this workshop for reasons explained below, as well as for the sake of time in the lab as integration tests take a while to run.

There are two types of tests that can be done for this repo, testing the infrastructure itself, and testing that the pipeline is correct. For the first, the infrastructure which comes from Cloud Platform's modules is being tested and iterated on so every module that is used has already been tested. An example of this can be seen here for Cloud Platform's Azure App Service Module.

For the second type of test for the pipeline itself, these pipeline templates have been utilized numerous times in pipelines to execute Terraform code. There are plans to validate pipeline files themselves but the work for this is currently TBD.

In the last section, we will show you how to utilize Pipeline Templates in order to write a pipeline that executes your Terraform code on TFE and deploys infrastructure onto multiple environments.