Skip to content

About

A worker that can run in a container/CICD pipeline to execute terraform from a yaml configuration

Resources

Stars

11 stars

Watchers

2 watching

Forks

Repository files navigation

terraform-worker

terraform-worker runs a pipeline of terraform root modules, called definitions, from one configuration file. It generates the provider, backend and remote state blocks for each definition, passes outputs from one definition to the next, and plans, applies or destroys the whole set in order. Each definition keeps its own state, so large deployments stay split into small, fast states that share common providers and variables.

./worker.yaml

terraform:
  providers:
    aws:
      requirements:
        version: "5.31.0"
      vars:
        region: {{ aws_region }}

  global_vars:
    terraform_vars:
      environment: dev

  definitions:
    network:
      path: /definitions/aws/network
    database:
      path: /definitions/aws/rds
      remote_vars:
        subnet_ids: network.outputs.private_subnet_ids
uv tool install terraform-worker
worker --aws-profile default --backend s3 --backend-bucket my-state terraform example --plan --apply

The database definition reads the network's output as local.subnet_ids, and {{ aws_region }} is filled in from --aws-region. On a new deployment, apply network first (--limit network), since planning database reads its state.

Documentation

Assuming a Role

The first step in assuming a role is to create the role to be assumed as documented in Creating a role to delegate permissions to an IAM user and then granting permissions to assume the role as documented in Granting a user permissions to switch roles .

To have the worker assume a role once the role and permissions are configured the --aws-role-arn and --aws-external-id flags need to be provided to the worker along with the credentials for the trusted account. Since neither the role ARN nor the ExternalId are secret, this allows running under another set of credentials without providing any additional secrets.

Development

The project uses uv to manage its environment and dependencies.

 % make init-dev        # or: uv sync
 % make lint test

uv sync creates .venv and installs the dev dependency group by default. Run tools through uv run, for example uv run pytest -k my_test.

Documentation

User docs are Markdown under docs/. The pages in docs/reference/ are generated from the CLI options and the configuration models; after changing either, run make docs and commit the result. CI fails when they are out of date.

Releasing

The worker is released from v<semver> tags and the console from console-v<semver> tags. A pushed tag publishes to PyPI and GHCR and creates a GitHub release. See Releases for installing, image tags, rebuilds and the release steps.

Background

The terraform worker was a weekend project to run terraform against a series of definitions (modules). The idea was the configuration vars, provider configuration, remote state, and variables from remote state would all be dynamically generated. The purpose was for building kubernetes deployments, and allowing all of the configuration information to be stored as either yamnl files in github, or have the worker configuration generated by an API which stored all of the deployment configurations in a database.

About

A worker that can run in a container/CICD pipeline to execute terraform from a yaml configuration

Resources

Stars

11 stars

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages