Skip to content
/ tfnew Public

A simple CLI to generate new terraform modules

License

Notifications You must be signed in to change notification settings

fitz7/tfnew

Repository files navigation

tfnew

tfnew is a small cli tool for generating new terraform modules in a fairly opinionated way

Installation

bash

curl -fsSL https://raw.githubusercontent.com/fitz7/tfnew/main/install.sh | bash

go

go install gitub.com/fitz7/tfnew@latest

Usage

init

This step is optional in using tfnew as all the args are also supported by tfnew module.

tfnew init will create a .tfnew.yaml file in the root of your project directory (wherever the .git folder is)

Default behaviour

tfnew init

By default, tfnew init specifies a local backend and a local terraform.tfstate file.

Specifying a backend

currently the only other supported backend is gcs

tfnew init --backend=gcs --backend_gcs_bucket=my-state-bucket --backend_gcs_prefix=my-repo

This will configure subsequent runs in this project to create a terraform backend block configured to use gcs with the bucket and prefix

Important

the prefix defined is not the final prefix. when you create your first module the prefix will be of the form prefix/module_path

module

basic usage

tfnew module modules/new-module

Will generate a new module with a very basic terraform block in the versions.tf file

terraform {
  required_version = ">= 1.0"
}

Create a new root module

A root module will be generated with a backend block to store the root modules state. It will also set the required terraform version to the latest minor version

tfnew module root-module --root
terraform {
  required_version = "~> 1.6"
  backend "gcs" {
    bucket = "my-state-bucket"
    prefix = "root-module"
  }
}

Creating modules with required_providers

required_providers must be referenced by their source and are also generated with their latest minor version

tfnew module root-module-providers --root --required_providers=hashicorp/google,hashicorp/google-beta
terraform {
  required_version = "~> 1.6"
  backend "gcs" {
    bucket = "my-state-bucket"
    prefix = "root-module-providers"
  }
  required_providers {
    google = {
      source  = "hashicorp/google"
      version = "~> 5.8"
    }
    google-beta = {
      source  = "hashicorp/google-beta"
      version = "~> 5.8"
    }
  }
}