### Install Terramate on Windows Source: https://terramate.io/docs/cli/getting-started Provides instructions to download the Terramate binary for Windows from the GitHub releases page. Manual installation steps are required after downloading. ```shell # Download the binary from https://github.com/terramate-io/terramate/releases ``` -------------------------------- ### Install Terramate CLI Manually Source: https://terramate.io/docs/get-started/opentofu Provides instructions to download the Terramate CLI binary directly from the GitHub releases page for manual installation. This method is suitable when package managers are not preferred or available. ```text Download the binary from https://github.com/terramate-io/terramate/releases ``` -------------------------------- ### Check Terramate Version Source: https://terramate.io/docs/cli/getting-started Verifies the Terramate installation by displaying the currently installed version. This command should be run after installation. ```shell $ terramate version ``` -------------------------------- ### Install Terramate on Ubuntu/Debian Source: https://terramate.io/docs/cli/getting-started Installs Terramate on Ubuntu and Debian-based systems by adding the Terramate repository and then installing the package. Requires sudo privileges. ```shell # Add the Terramate repo to your sources echo "deb [trusted=yes] https://repo.terramate.io/apt/ /" \ | sudo tee /etc/apt/sources.list.d/terramate.list apt update apt install terramate ``` -------------------------------- ### Verify Terramate Installation Source: https://terramate.io/docs/getting-started Command to check the currently installed version of the Terramate CLI. ```shell terramate version ``` -------------------------------- ### List All Terramate Stacks Source: https://terramate.io/docs/cli/getting-started Lists all stacks recognized by Terramate after creating the second stack. This confirms that both 'Alice' and 'Bob' stacks are now registered. ```shell $ terramate list stacks/alice stacks/bob ``` -------------------------------- ### Initialize Terramate Project Source: https://terramate.io/docs/getting-started Steps to initialize a new Git repository and create an initial commit, which is required for Terramate's change detection features. ```shell git init -b main terramate-quickstart cd terramate-quickstart git commit --allow-empty -m "Initial empty commit" ``` -------------------------------- ### Initialize and push repository to remote Source: https://terramate.io/docs/getting-started Configures the local git repository to point to a remote GitHub repository and pushes the initial project data. ```sh git remote add origin git@github.com:your-account/terramate-quickstart.git git branch -M main git push -u origin main ``` -------------------------------- ### Install Terramate on Fedora/CentOS Source: https://terramate.io/docs/cli/getting-started Installs Terramate on Fedora and CentOS systems by configuring the Terramate repository and installing the package. Requires sudo privileges. ```shell # Add the Terramate repo to your sources sudo tee /etc/yum.repos.d/terramate.repo <terramate.tm.hcl terramate { config { cloud { organization = "organization-short-name" } } } EOF git add terramate.tm.hcl git commit -m "Add Terramate Cloud configuration" ``` ```hcl cat <terramate.tm.hcl terramate { config { cloud { organization = "organization-short-name" location = "us" } } } EOF git add terramate.tm.hcl git commit -m "Add Terramate Cloud configuration" ``` -------------------------------- ### Authenticate with Terramate Cloud via CLI Source: https://terramate.io/docs/getting-started Initiates the login process for the Terramate CLI to synchronize local data with Terramate Cloud. Supports standard login and GitHub-based authentication. ```sh terramate cloud login terramate cloud login --github ``` -------------------------------- ### Create a Terramate Stack Source: https://terramate.io/docs/cli/getting-started Creates a new Terramate stack with a specified name and description, located in the 'stacks/alice' directory. Terramate automatically assigns an ID if one is not provided. ```shell $ terramate create \ --name "Alice" \ --description "Alice's first stack" \ stacks/alice ``` -------------------------------- ### Create a Second Terramate Stack Source: https://terramate.io/docs/cli/getting-started Creates a second Terramate stack named 'Bob' with a description, located in the 'stacks/bob' directory. This follows the same pattern as creating the first stack. ```shell $ terramate create \ --name "Bob" \ --description "Bob's first stack" \ stacks/bob ``` -------------------------------- ### Push Changes and Trigger Deployment Plan (OpenTofu) Source: https://terramate.io/docs/getting-started Pushes the committed changes to the main branch and then runs an OpenTofu plan on changed stacks to prepare for deployment. The plan is saved to 'deploy.tfplan'. ```shell git push origin main terramate run \ --changed \ -- \ tofu plan -lock-timeout=5m -out deploy.tfplan ``` -------------------------------- ### Trigger and sync deployment to Terramate Cloud Source: https://terramate.io/docs/cli/getting-started Detects changes in stacks, generates a plan, applies the changes, and synchronizes the deployment status to Terramate Cloud. ```bash $ terramate run \ --changed \ -- \ terraform plan -lock-timeout=5m -out deploy.tfplan $ terramate run \ --changed \ --sync-deployment \ --terraform-plan-file=deploy.tfplan \ -- \ terraform apply -input=false -auto-approve -lock-timeout=5m deploy.tfplan ``` ```bash $ terramate run \ --changed \ -- \ tofu plan -lock-timeout=5m -out deploy.tfplan $ terramate run \ --changed \ --sync-deployment \ --tofu-plan-file=deploy.tfplan \ -- \ tofu apply -input=false -auto-approve -lock-timeout=5m deploy.tfplan ``` -------------------------------- ### Define and track Terraform/OpenTofu null resources Source: https://terramate.io/docs/getting-started Creates a null resource configuration file within a Terramate stack and commits it to version control. This demonstrates how to manage infrastructure definitions without requiring cloud credentials. ```hcl cat <stacks/bob/null.tf resource "null_resource" "quickstart" { } EOF git add stacks/bob/null.tf git commit -m "Add a null resource" ``` -------------------------------- ### Create Initial Git Commit Source: https://terramate.io/docs/cli/getting-started Adds an initial, empty commit to the Git repository. This is necessary for Terramate's change detection, which relies on comparing at least two commits. ```shell $ git commit --allow-empty -m "Initial empty commit" ``` -------------------------------- ### Modify Stack and Commit Changes (Terraform/OpenTofu) Source: https://terramate.io/docs/getting-started Adds a null resource to the 'bob' stack, stages the change, and commits it to the Git repository. This prepares for triggering a new deployment. ```shell cat <>stacks/bob/null.tf resource "null_resource" "quickstart2" { } EOF git add stacks/bob/null.tf git commit -m "Add another null resource" ``` -------------------------------- ### Configure Git ignore for infrastructure files Source: https://terramate.io/docs/cli/getting-started Creates a .gitignore file to prevent temporary Terraform/OpenTofu state and lock files from being committed to the version control system. ```sh cat <.gitignore .terraform .terraform.lock.hcl *.tfstate terraform.tfstate terraform.tfstate.backup *.tfplan EOF ``` -------------------------------- ### Terramate Run OpenTofu Plan Source: https://terramate.io/docs/getting-started Executes the `tofu plan` command across all stacks managed by Terramate. This generates an execution plan, showing what actions OpenTofu will take to achieve the desired state. ```bash $ terramate run tofu plan ``` -------------------------------- ### Push Changes and Trigger Deployment Plan (Terraform) Source: https://terramate.io/docs/getting-started Pushes the committed changes to the main branch and then runs a Terraform plan on changed stacks to prepare for deployment. The plan is saved to 'deploy.tfplan'. ```shell git push origin main terramate run \ --changed \ -- \ terraform plan -lock-timeout=5m -out deploy.tfplan ``` -------------------------------- ### Configure Terramate Cloud integration Source: https://terramate.io/docs/getting-started Sets up the Terramate Cloud configuration file to link a project to a specific organization. Supports regional variations for EU and US data residency. ```hcl cat <terramate.tm.hcl terramate { config { cloud { organization = "organization-short-name" } } } EOF git add terramate.tm.hcl git commit -m "Add Terramate Cloud configuration" ``` ```hcl cat <terramate.tm.hcl terramate { config { cloud { organization = "organization-short-name" location = "us" } } } EOF git add terramate.tm.hcl git commit -m "Add Terramate Cloud configuration" ``` -------------------------------- ### Add Terraform Null Resource Source: https://terramate.io/docs/cli/getting-started Demonstrates adding a Terraform null resource to a stack using plain Terraform configuration. This resource is for demonstration and does not create actual cloud resources. It requires basic Terraform knowledge. ```shell $ cat <stacks/bob/null.tf resource "null_resource" "quickstart" { } EOF $ git add stacks/bob/null.tf $ git commit -m "Add a null resource" ``` -------------------------------- ### Sync Stacks to Terramate Cloud (OpenTofu) Source: https://terramate.io/docs/getting-started Runs an OpenTofu plan in all stacks to detect drift and syncs the status to Terramate Cloud. This command adds stacks to the inventory without marking them as drifted. ```shell terramate run \ --sync-drift-status \ --tofu-plan-file=drift.tfplan \ --continue-on-error \ -- \ tofu plan -detailed-exitcode -out drift.tfplan ``` -------------------------------- ### Initialize Terraform/OpenTofu in Changed Stacks Source: https://terramate.io/docs/cli/getting-started Re-initializes Terraform or OpenTofu in stacks that have detected changes. This step is crucial for downloading necessary providers, such as the null provider. It leverages Terramate's change detection to optimize execution. ```shell $ terramate run --changed terraform init ``` ```shell $ terramate run --changed tofu init ``` -------------------------------- ### Generate Terraform/OpenTofu backend configuration Source: https://terramate.io/docs/cli/getting-started Creates a Terramate HCL configuration file to automate the generation of backend.tf files across all stacks. This ensures consistent backend settings for Terraform or OpenTofu projects. ```sh cat <stacks/backend.tm.hcl generate_hcl "backend.tf" { content { terraform { backend "local" {} } } } EOF ``` -------------------------------- ### Terramate Run Terraform Plan Source: https://terramate.io/docs/getting-started Executes the `terraform plan` command across all stacks managed by Terramate. This generates an execution plan, showing what actions Terraform will take to achieve the desired state. ```bash $ terramate run terraform plan ``` -------------------------------- ### Execute infrastructure lifecycle commands with change detection Source: https://terramate.io/docs/getting-started Uses the Terramate CLI to run initialization, planning, and application commands only on stacks that have detected changes. This optimizes execution time and limits the blast radius of operations. ```sh terramate run --changed terraform init terramate run --changed terraform plan terramate run --changed terraform apply -auto-approve ``` ```sh terramate run --changed tofu init terramate run --changed tofu plan terramate run --changed tofu apply -auto-approve ``` -------------------------------- ### Install Terramate on macOS Source: https://terramate.io/docs/cli/getting-started Installs Terramate using Homebrew on macOS. Ensure Homebrew is installed before running this command. ```shell brew install terramate ``` -------------------------------- ### Install OpenTofu Source: https://terramate.io/docs/cli/automation/github-actions/preview-workflow This is a commented-out step to install the OpenTofu CLI. It uses the 'opentofu/setup-opentofu' action and specifies the desired OpenTofu version. ```yaml # - name: Install OpenTofu # uses: opentofu/setup-opentofu@v1 # with: # tofu_version: 1.10.3 # tofu_wrapper: false ``` -------------------------------- ### Install OpenTofu Action Source: https://terramate.io/docs/cli/automation/github-actions/deployment-workflow Installs the OpenTofu CLI using the opentofu/setup-opentofu GitHub Action. This is an alternative to Terraform if you are using OpenTofu. ```yaml # Uncomment this if using OpenTofu # - name: Install OpenTofu # uses: opentofu/setup-opentofu@v1 # with: # tofu_version: 1.10.3 # tofu_wrapper: false ``` -------------------------------- ### Install Terraform Action Source: https://terramate.io/docs/cli/automation/github-actions/deployment-workflow Installs the Terraform CLI using the setup-terraform GitHub Action. This step is necessary if your infrastructure uses Terraform. ```yaml # Comment this out if not using Terraform - name: Install Terraform uses: hashicorp/setup-terraform@v3 with: terraform_version: 1.12.2 terraform_wrapper: false ``` -------------------------------- ### Install Terraform Source: https://terramate.io/docs/cli/automation/github-actions/preview-workflow This step installs the Terraform CLI using the 'hashicorp/setup-terraform' action. It specifies the Terraform version and disables the Terraform wrapper. ```yaml - name: Install Terraform uses: hashicorp/setup-terraform@v3 with: terraform_version: 1.12.2 terraform_wrapper: false ``` -------------------------------- ### Sync Stacks to Terramate Cloud with Terragrunt Source: https://terramate.io/docs/get-started/terragrunt This command syncs your local stacks to Terramate Cloud by running a Terragrunt plan and detecting drift. It sends the plan results to Terramate Cloud, which can trigger alerts if changes are detected. This command requires Terragrunt to be installed and configured. ```bash terramate run \ --continue-on-error \ --cloud-sync-drift-status \ --terraform-plan-file=drift.tfplan \ --terragrunt \ -- terragrunt plan -out drift.tfplan -detailed-exitcode -lock=false ``` -------------------------------- ### Example: Get Element at Specific Index (Shell) Source: https://terramate.io/docs/cli/reference/functions/tm_element This example demonstrates how to use `tm_element` to retrieve an element at a specific index from a list. The index is zero-based, so index 1 corresponds to the second element. ```sh tm_element(["a", "b", "c"], 1) b ``` -------------------------------- ### Configure Change Detection Source: https://terramate.io/docs/get-started/terragrunt Customizes Terragrunt change detection behavior within the terramate.tm.hcl configuration file. ```hcl terramate { config { change_detection { terragrunt { enabled = "off" } } } } ``` -------------------------------- ### Common GitLab CI Setup (.common.yml) Source: https://terramate.io/docs/cli/automation/gitlab-ci This file contains common setup scripts and configurations for GitLab CI jobs. It includes installing tools like Terraform and Terramate, setting up authentication tokens, and configuring Git behavior. ```yaml .setup: script: - apk add unzip git bash curl jq - wget https://releases.hashicorp.com/terraform/1.5.7/terraform_1.5.7_linux_amd64.zip -O /tmp/terraform.zip - unzip /tmp/terraform.zip -d /tmp - mv /tmp/terraform /bin - wget https://github.com/terramate-io/terramate/releases/download/v0.9.1/terramate_0.9.1_linux_x86_64.tar.gz -O /tmp/terramate.tar.gz - tar xzf /tmp/terramate.tar.gz -C /tmp - mv /tmp/terramate /bin .id_tokens: id_tokens: GITLAB_OIDC_TOKEN: aud: https://gitlab.com TM_GITLAB_ID_TOKEN: aud: api.terramate.io .auth: script: - echo ${GITLAB_OIDC_TOKEN} > `pwd`/.ci_job_jwt_file - gcloud iam workload-identity-pools create-cred-config projects//locations/global/workloadIdentityPools/gitlab-ci/providers/gitlab-ci --service-account="" --output-file=`pwd`/.gcp_temp_cred.json --credential-source-file=`pwd`/.ci_job_jwt_file - gcloud auth login --cred-file=`pwd`/.gcp_temp_cred.json - export GOOGLE_APPLICATION_CREDENTIALS=`pwd`/.gcp_temp_cred.json .common: variables: GIT_STRATEGY: clone # clone entire repo instead of using fetch GIT_DEPTH: 0 # avoid shallow clone to give terramate all the info it needs ``` -------------------------------- ### Example: Get Last Element using `tm_length` (Shell) Source: https://terramate.io/docs/cli/reference/functions/tm_element This example shows how to retrieve the last element of a list by combining `tm_element` with `tm_length`. The index is calculated as the total length of the list minus one, accounting for the zero-based indexing. ```sh tm_element(["a", "b", "c"], tm_length(["a", "b", "c"])-1) c ``` -------------------------------- ### Setup Terragrunt Source: https://terramate.io/docs/cli/automation/github-actions/preview-workflow This step sets up the Terragrunt CLI using the 'autero1/action-terragrunt' action. It specifies the Terragrunt version and uses the GitHub token for authentication. ```yaml - name: Setup Terragrunt uses: autero1/action-terragrunt@v3 with: terragrunt-version: 0.83.2 token: ${{ github.token }} ``` -------------------------------- ### Setup Terragrunt Action Source: https://terramate.io/docs/cli/automation/github-actions/deployment-workflow Sets up the Terragrunt CLI using the autero1/action-terragrunt GitHub Action. This action makes Terragrunt available in the workflow. ```yaml - name: Setup Terragrunt uses: autero1/action-terragrunt@v3 with: terragrunt-version: 0.83.2 token: ${{ github.token }} ``` -------------------------------- ### Initialize OpenTofu Source: https://terramate.io/docs/cli/automation/github-actions/drift-check-workflow Initializes the OpenTofu environment using Terramate. This step ensures all necessary providers and modules are downloaded. ```yaml run: | terramate run \ --parallel 1 \ -- \ tofu init -lock-timeout=5m ``` -------------------------------- ### Modify stack configuration for deployment Source: https://terramate.io/docs/cli/getting-started Appends a new null resource to a stack configuration file and commits the change to Git to trigger a deployment workflow. ```bash $ cat <>stacks/bob/null.tf resource "null_resource" "quickstart2" { } EOF $ git add stacks/bob/null.tf $ git commit -m "Add another null resource" $ git push origin main ``` -------------------------------- ### Expand '~' in Path - Shell Script Source: https://terramate.io/docs/cli/reference/functions/tm_pathexpand This example demonstrates how the tm_pathexpand function expands a path starting with '~' to the user's home directory. If the path does not start with '~', it is returned unmodified. This function is useful for locating files relative to the user's home directory. ```shell tm_pathexpand("~/.ssh/id_rsa") /home/steve/.ssh/id_rsa tm_pathexpand("/etc/resolv.conf") /etc/resolv.conf ``` -------------------------------- ### Install Terramate Source: https://terramate.io/docs/cli/automation/github-actions/preview-workflow This step installs the Terramate CLI using the official Terramate GitHub Action. It specifies the version of Terramate to be installed. ```yaml - name: Install Terramate uses: terramate-io/terramate-action@v3 with: version: "0.14.0" ``` -------------------------------- ### Run Terragrunt Commands with Terramate Source: https://terramate.io/docs/get-started/terragrunt Executes Terragrunt commands across stacks using Terramate orchestration, including parallel execution and change-based filtering. ```sh terramate list ``` ```sh terramate run -- terragrunt init ``` ```sh terramate run --parallel 5 -- terragrunt plan -out plan.tfplan ``` ```sh terramate run --changed -- terragrunt apply -auto-approve plan.tfplan ``` -------------------------------- ### Terramate List Changed Stacks Source: https://terramate.io/docs/getting-started Lists stacks that have changed based on Git integration. This command helps identify modified stacks after a commit, useful for targeted operations. ```bash $ terramate list --changed stacks/bob ``` -------------------------------- ### Install Terramate Action Source: https://terramate.io/docs/cli/automation/github-actions/drift-check-workflow Installs the Terramate CLI using the official GitHub Action. Specify the desired version for consistency. ```yaml uses: terramate-io/terramate-action@v3 with: version: "0.14.0" ``` -------------------------------- ### Install Terraform Action Source: https://terramate.io/docs/cli/automation/github-actions/preview-workflow Installs a specific version of Terraform using the official HashiCorp setup-terraform Action. It configures Terraform to be available in the workflow environment. ```yaml uses: hashicorp/setup-terraform@v3 with: terraform_version: 1.12.2 terraform_wrapper: false ``` -------------------------------- ### Initialize Terragrunt Source: https://terramate.io/docs/cli/automation/github-actions/drift-check-workflow Initializes the Terragrunt environment using Terramate. This step downloads Terragrunt configurations and dependencies. ```yaml run: | terramate run \ --parallel 1 \ -- \ terragrunt init -lock-timeout=5m ``` -------------------------------- ### Parent Stacks Example (Terraform/OpenTofu) Source: https://terramate.io/docs/cli/stacks Demonstrates a directory structure for parent stacks within a 'dev' environment. Each subdirectory represents a parent stack managing a specific infrastructure service (db, k8s, vpc). Terramate can execute these in parallel if they are not nested. ```shell .\nde└── dev\n ├── config.tm.hcl\n ├── db\n │ ├── backend.tf\n │ ├── main.tf\n │ ├── stack.tm.hcl\n │ └── terraform.tf\n ├── k8s\n │ ├── backend.tf\n │ ├── main.tf\n │ ├── stack.tm.hcl\n │ └── terraform.tf\n └── vpc\n ├── backend.tf\n ├── main.tf\n ├── stack.tm.hcl\n └── terraform.tf ```