Search for a command to run...
Install Terraform 1.16 from the official HashiCorp repositories, configure providers and backends, write your first stack and keep state safe with locking.
Terraform is HashiCorp's Infrastructure as Code (IaC) tool. You describe your infrastructure in configuration files, and Terraform plans the changes and applies them against cloud and on-prem providers.
Terraform 1.16.6 · the official apt.releases.hashicorp.com and
rpm.releases.hashicorp.com repositories · native terraform test ·
terraform providers lock for reproducible provider versions
Since Terraform 1.6, HashiCorp distributes the binary under the Business Source License — free to use, but it converts to a commercial licence. If you need a fully open-source licence, use OpenTofu, a community fork of Terraform 1.5 with the same CLI and configuration format.
releases.hashicorp.com and registry.terraform.io# Add the HashiCorp signing key
wget -O - https://apt.releases.hashicorp.com/gpg \
| sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
# Add the repository
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(grep -oP '(?<=UBUNTU_CODENAME=).*' /etc/os-release || lsb_release -cs) main" \
| sudo tee /etc/apt/sources.list.d/hashicorp.list
# Install
sudo apt update
sudo apt install -y terraform
# Verify
terraform version# Version and platform
terraform version
# Enable shell completion
terraform -install-autocomplete
source ~/.bashrc
# Confirm the provider registry is reachable
terraform initCreate main.tf in an empty directory:
terraform {
required_version = ">= 1.16.0"
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 6.0"
}
}
}
provider "aws" {
region = var.region
}
resource "aws_s3_bucket" "example" {
bucket_prefix = "example-"
}
variable "region" {
type = string
default = "eu-central-1"
}# Download the providers and the backend configuration
terraform init
# See what would change — always run this first
terraform plan
# Apply the changes
terraform apply
# Inspect the state
terraform show
terraform state listCommit the lock file so every machine and every CI job uses exactly the same provider builds:
# Generate the lock file for all the platforms you use
terraform providers lock -platform=linux_amd64 -platform=darwin_arm64 -platform=darwin_amd64
# Commit .terraform.lock.hcl to version controlState is the record of what Terraform manages. It usually belongs in a remote backend with locking enabled.
terraform {
backend "s3" {
bucket = "my-terraform-state"
key = "prod/terraform.tfstate"
region = "eu-central-1"
dynamodb_table = "terraform-locks"
encrypt = true
}
}# Switch to the remote backend
terraform init -migrate-state
# A stuck lock has to be released by hand — use with care
terraform force-unlock <LOCK_ID>State files contain the full attributes of your infrastructure, including secrets that were written to resources. Keep them in an encrypted remote backend with versioning enabled.
Terraform has a native test framework — no extra tooling required:
# tests/setup.tftest.hcl
variables {
bucket_prefix = "test-"
}
run "validate_bucket_name" {
command = plan
assert {
condition = length(aws_s3_bucket.example.bucket) > 0
error_message = "The bucket name must not be empty"
}
}# Run the tests
terraform testTerraform 1.5 and newer can generate configuration for resources you already have:
# Generate configuration for an existing resource
terraform plan -generate-config-out=generated.tf
# Or define the import block by handimport {
to = aws_s3_bucket.example
id = "my-existing-bucket"
}terraform plan # shows the import
terraform apply# Check for a newer version
terraform version
# Upgrade the binary
sudo apt update && sudo apt install --only-upgrade terraform
# Upgrade the providers and re-pin them
terraform init -upgrade
terraform providers lock -platform=linux_amd64
# Review the change before applying
terraform planTerraform supports a version skew of one minor version between the CLI and providers.
Upgrade Terraform first, then the providers, and run terraform plan between the two
steps.
terraform plan before apply — and review the diff, do not pipe it
into apply in CI..terraform.lock.hcl so every run uses the same provider builds.*.tfstate or *.tfvars to Git — use a secret manager for variables.terraform test for every module.import blocks to bring existing infrastructure under management instead of
rebuilding it.check block for assertions on data sources and outputs.Install and configure Terraform, Ansible, HashiCorp Vault, Consul, Vagrant and Packer with reproducible infrastructure examples, secret management patterns and safe rollout strategies.
Install Ansible 14 with ansible-core 2.21 and Python 3.12+, build an inventory, write your first playbook, use Ansible Vault and run tasks in execution environments.