learn-terraform

Course outlines for learning terraform.

View the Project on GitHub robertdebock/learn-terraform

CI/CD for modules

expected time requirements
75 minutes A computer with Terraform installed, terraform knowledge.

Goal: Setup CI/CD for Terraform modules.

Explanation

Testing helps you to keep your modules in a good state and makes it easier to contribute.

Howto

BitBucket

Let’s take an example BitBucket CI/CD file. (It’s not complete, you’ll make it complete later.)

Add a file to the repository containing a Terraform module called bitbucket-pipelines.yml:

---
image: hashicorp/terraform:full

pipelines:
  default:
    - step:
      script:
        - terraform init

And a GitHub actions example. Be aware the the different scenarios (directories in examples need to be listed in the action.

GitHub

There are many actions that you can use. Pick one that suits your situation.

A realistic example can be found here.

Assignment

This may take some time, but it’s well worth the effort as these pipelines will run quite frequently. We’ll compare each others code later to learn from each other.

With CI/CD, always start with the smallest unit/component. This ensures you can deliver value incrementally instead of a “waterfall” approach.

Bonus assignments

Questions

  1. Would you terraform apply the code in CI when testing a module?
  2. If you would terraform apply, how would you make sure terraform destroy runs whatever the output of terraform apply. (hint).

Hints

You may need to place these “secret variables” to the CI system:

For GitLab users, a template can be used.

Solution