macOS - Installing Terraform with Autocompletion

A short tutorial for macOS on Installing Terraform with Zsh autocompletion.

macOS - Installing Terraform with Autocompletion
Photo by Kvistholt Photography / Unsplash

Install

brew tap hashicorp/tap
First, install the HashiCorp tap, a repository of all Homebrew packages.
brew install hashicorp/tap/terraform
Now, install Terraform with hashicorp/tap/terraform.
terraform -version

Terraform v1.2.5
on darwin_arm64
Verify that the installation worked by opening a new terminal session and listing Terraform's available subcommands.

Enable tab completion

touch ~/.zshrc
terraform -install-autocomplete
Once the autocomplete support is installed, you will need to restart your shell.

Quick test

mkdir learn-terraform-docker-container
cd learn-terraform-docker-container
Create a local folder and change directories

Create a main.tf with the following configuration.

terraform {
  required_providers {
    docker = {
      source  = "kreuzwerker/docker"
      version = "~> 2.13.0"
    }
  }
}

provider "docker" {}

resource "docker_image" "nginx" {
  name         = "nginx:latest"
  keep_locally = false
}

resource "docker_container" "nginx" {
  image = docker_image.nginx.latest
  name  = "tutorial"
  ports {
    internal = 80
    external = 8000
  }
}
terraform init
Initialize the project, which downloads a plugin that allows Terraform to interact with Docker.
terraform apply
Provision the NGINX server container with apply. When Terraform asks you to confirm type yes and press ENTER.

Verify the existence of the NGINX container by visiting localhost:8000 in your web browser or running docker ps to see the container.

docker ps
CONTAINER ID        IMAGE                     COMMAND                  CREATED             STATUS              PORTS                    NAMES
445d5e111111        e7913s34dehk              "nginx -g 'daemon of…"   20 seconds ago      Up 30 seconds       0.0.0.0:8000->80/tcp     tutorial
terraform destroy	
To stop the container, run terraform destroy.

Subscribe to Devtooler

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe