Skip to content

Instantly share code, notes, and snippets.

@HariSekhon
Created June 26, 2024 20:35
Show Gist options
  • Save HariSekhon/469e49f0d58a5f09dee0a0b6b05b0ece to your computer and use it in GitHub Desktop.
Save HariSekhon/469e49f0d58a5f09dee0a0b6b05b0ece to your computer and use it in GitHub Desktop.
terraform.md from HariSekhon/Knowledge-Base repo: https://github.com/HariSekhon/Knowledge-Base

Terraform

Infrastructure-as-Code using HCL manifests to define cloud resources.

Idempotent, queries cloud APIs, detects what is missing or has changed and then applies the necessary changes to reconcile.

Install Terraform

Quick install script is found in the DevOps-Bash-tools repo:

install_terraform.sh

Optionally specify a version argument, otherwise defaults to finding out and installing the latest version.

Terraform Code

See the HariSekhon/Terraform repo for some Terraform code and templates for common files and settings to get you started, such as backend.tf, provider.tf, main.tf etc.

Running Terraform

Download the providers and create or connect to the terraform.tfstate file:

terraform init

Format you code:

terraform fmt

Validate your code:

terraform validate

See the plan of additions/deletions/modifications that Terraform would do:

terraform plan

Apply the changes:

terraform apply

Terraform State

Stored in a terraform.tfstate either locally or more usually in a cloud bucket to be shared among users or from a CI/CD system.

This is just a JSON file so you can read its contents to find out what version of Terraform it is using.

terraform_gcs_backend_version.sh is a convenience script to determine this straight from a GCS bucket.

tfenv

Installs multiple versions of Terraform to ~/.tfenv in order to maintain compatability with different Terraform code bases.

Otherwise using a newer globally installed version of Terraform will upgrade the terraform.tfstate file and break other clients who will be forced to upgrade to the same version in order to run again.

On Mac, install tfenv:

brew instal tfenv

List available versions:

tfenv list-remote

Install specific version to match the existing terraform.tfstate file.

tfenv install <version>

or if you don't care about a specific version and just quickly want the latest:

tfenv install latest

Ensure ~/.tfenv/bin/ is early in your shell $PATH (automatically sourced in [DevOps-Bash-tools] (devops-bash-tools.md) shell).

Then just use terraform like usual.

Once you have more than one version of Terraform installed, switch to another version: another version:

tfenv use <version>

Terragrunt

Important for modularity and performance of Terraform code bases.

See Terragrunt for more details.

Linting & Security

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment