Cheatography
https://cheatography.com
Terraform CLI cheat sheet
Code Syntax
terraform fmt |
Formats Terraform configuration files to a canonical format. |
terraform validate |
Validates the syntax and configuration of Terraform files. |
Initializing Terraform
terraform init |
Initializes the working directory containing Terraform configuration files. |
terraform init -upgrade |
Updates provider plugins to the latest versions. |
Provisioning Infrastructure
terraform plan |
Generates an execution plan showing what Terraform will do. |
terraform apply |
Applies the changes required to reach the desired state. |
terraform destroy |
Destroy all remote objects managed. |
Miscellaneous
terraform version |
Displays the Terraform version information. |
terraform console |
Opens an interactive console to evaluate Terraform expressions. |
terraform graph |
Generates a visual representation of Terraform resources and dependencies. |
Azure Service Principal
provider "azurerm" {
features {}
subscription_id = "<subscription_id>"
client_id = "<application_id>"
client_secret = "<secret_value>"
tenant_id = "<tenant_id>"
}
|
Azure Backend
terraform {
backend "azurerm" {
resource_group_name = "<resource_group_name>"
storage_account_name = "<storage_account_name>"
container_name = "<container_name>"
key = "<state_file_key>"
access_key = "<storage_account_access_key>"
}
}
|
Azure Required Provider
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~> x.xx.x "
}
}
}
|
Valid Operators:
= : Allows only one exact version number.
!= : Excludes an exact version number.
>, >=, <, <=: Comparisons against a specified version.
~>: Allows only the rightmost version component to increment.
|
|
Terraform State
terraform state list |
Lists all resources in the Terraform state. |
terraform state show <resource> |
Shows the attributes of a specific resource in the Terraform state. |
terraform state rm <resource> |
Removes a resource from the Terraform state. |
terraform state mv <old-resource> <new-resource> |
Moves a resource within the Terraform state. |
Terraform State Backend
terraform init -backend-config=<config> |
Initializes Terraform with a specific backend configuration. |
terraform remote config |
Configures remote state storage. |
terraform remote pull |
Retrieves the current state from the remote backend. |
terraform remote push |
Uploads the local state to the remote backend. |
Command-Line Flags
-upgrade |
Opt to upgrade modules and plugins |
-reconfigure |
Disregards any existing configuration and state. |
-input=true|false |
Ask for input if necessary. If false, will error if input was required. |
-no-color |
Disable text coloring & formatting in the output. |
-var 'NAME=VALUE' |
Sets a value for a single input variable. |
-var-file=FILENAME |
Sets values for variables as TFVARS. |
-target=ADDRESS |
Focus its efforts only on specific resource instance. |
Resource Manipulation
moved {
from = azurerm_resource_group.example.rg01
to = azurerm_resource_group.example.rg02
}
import {
to = azurerm_resource_group.example.rg03
id = /subscriptions/<subscription_id>/resourceGroups/<resource_group_name>
}
removed {
from = azurerm_resource_group.example.rg02
lifecycle {
destroy = false
}
}
|
|
Created By
Metadata
Comments
No comments yet. Add yours below!
Add a Comment
Related Cheat Sheets