Giter Site home page Giter Site logo

bazel-terraform-rules's Introduction

Terraform modules - a Bazel approach

CI

This repository is the result of a short experiment meant to improve the engineering experience when working with Terraform.

Why?

One approach for developing terraform and maximise code reuse is to enforce the use of modules early on. This tends to generally work well but all builds must be reproducible, and it requires version pinning. Terraform modules mono-repositories work well in practice, but as the monorepo grows the terraform init tends to take a while to run. There are a number of tools which can be used to create tests for terraform modules and modules should use them. In my personal experience I have seen (and built) a number of tools to deal with usual monorepo building: detect which things need building, build them and publish the artefacts.

Experiment

This is an experiment aimed at improving the experience of developing terraform modules. What I would like to prove is:

  • low barrier for adding a new module
  • for each added module it should be possible to:
    • run format checks (so that we can ensure the formatting is consistent)
    • run lint checks (to ensure the conventions are enforced)
    • publish it to a central repository
  • modules can depend only on other modules
  • a module should be published with its transitive dependencies
  • it should be possible to write blackbox tests and use the modules
  • should build and test incrementally
  • modules should not depend on terraform test fixtures

Solution

Over the years I have reinvented some tools. Sometimes it's easier to just fall back to previous experience, but for this experiment it felt right to use a familiar build tool some more and try to understand how to author rules.

On this journey this particular blog series https://jayconrod.com/posts/106/ on how to write bazel rules was of great help.

Results

The results can be seen in this repo. All the rules/toolchains/providers are under tools. Let's see how the above criteria is met.

low barrier for adding a new module - this is as easy as adding a BUILD file to the module root containing:

load("@tf_modules//tools:def.bzl", "terraform_module")

terraform_module(
    name = "constants",
)

the module will then get three Bazel targets: format, lint and publish

modules can depend only on other modules - which is something accomplished by adding deps attribute:

load("@tf_modules//tools:def.bzl", "terraform_module")

terraform_module(
    name = "consumer",
    deps = [
        "//modules/constants",
    ],
)

a module should be published with its transitive dependencies - this is accomplished by using the deps above. The publish target will have access to all the module sources (all .tf files) as well as the transitive dependencies sources.

it should be possible to write blackbox tests and use the modules - Terratest - https://github.com/gruntwork-io/terratest is configured and can build/teardown ephemeral infrastructure used for blackbox tests.

should build and test incrementally - this is mainly why I chose Bazel - as long as the rules are correct we will have correct and fast builds.

modules should not depend on terraform test fixtures - test fixtures declared inside examples folder can only be imported from the test submodules. This way none of the published terraform modules won't be able to pick up the test fixtures.

bazel-terraform-rules's People

Contributors

dvulpe avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.