13 January 2021

Terraform Cheatsheet

CategoryCommandPurpose
Initializeterraform initInitialize working directory, providers, and modules
Initializeterraform init -upgradeUpgrade providers and modules to newer versions
Formattingterraform fmtFormat Terraform files
Formattingterraform fmt -recursiveFormat all Terraform files recursively
Validationterraform validateValidate Terraform configuration
Planningterraform planShow infrastructure changes before applying
Planningterraform plan -out=tfplanSave execution plan to a file
Planningterraform plan -detailed-exitcodeReturn exit code based on changes detected
Applyterraform applyApply infrastructure changes
Applyterraform apply -auto-approveApply changes without confirmation
Applyterraform apply tfplanApply a saved plan file
Destroyterraform destroyDestroy managed resources
Destroyterraform destroy -auto-approveDestroy resources without confirmation
Outputterraform outputDisplay output variables
Outputterraform output <name>Display a specific output
Outputterraform output -jsonDisplay outputs in JSON format
Showterraform showDisplay current state or plan details
Showterraform show -jsonDisplay state/plan in JSON format
Consoleterraform consoleInteractive Terraform expression console
Graphterraform graphGenerate dependency graph
Versionterraform versionShow Terraform version
Providersterraform providersList providers in use
Providersterraform providers schema -jsonView provider schemas

Import & Resource Replacement

CommandPurpose
terraform import <resource> <id>Import existing infrastructure
terraform plan -replace=<resource>Plan resource replacement
terraform apply -replace=<resource>Replace resource during apply

State Lock & Terraform Cloud

CommandPurpose
terraform loginLogin to Terraform Cloud
terraform logoutLogout from Terraform Cloud
terraform force-unlock <lock_id>Remove stale state lock

Refresh & Targeting

CommandPurpose
terraform plan -refresh-onlyRefresh state without making changes
terraform apply -refresh-onlyApply refreshed state
terraform plan -target=<resource>Plan specific resource only
terraform apply -target=<resource>Apply specific resource only

Terraform workspaces in more detail -
Terraform workspaces are a feature that allows you to manage multiple distinct sets of Terraform state files within a single Terraform configuration.

Workspaces are useful when you need to maintain multiple copies of the same infrastructure for different purposes, such as development, testing, staging, and production, without duplicating your configuration files.

When you work with Terraform workspaces, each workspace has its own state file, which means that you can have different resource instances or configurations for each workspace.

This separation helps prevent accidental changes to the wrong environments and makes it easier to manage complex deployments.

Terraform starts with a single, default workspace named `default` that you cannot delete. If you have not created a new workspace, you are using the default workspace in your Terraform working directory.

Few commands to work on workspaces -

List workspaces:
$ terraform workspace list  
 * default
Create a workspace:
$ terraform workspace new dev
 Created and switched to workspace "dev"!
You're now on a new, empty workspace. Workspaces isolate their state,
so if you run "terraform plan" Terraform will not see any existing state
for this configuration.

$ terraform workspace new staging
 Created and switched to workspace "staging"!

You're now on a new, empty workspace. Workspaces isolate their state,
so if you run "terraform plan" Terraform will not see any existing state
for this configuration.
To check the current workspace, use the `terraform workspace show` command.

$ terraform workspace show
staging
Select a Workspace:
To switch b/w workspaces

$ terraform workspace list
  default
  dev
* staging 

$ terraform workspace select dev
 Switched to workspace "dev".

$ terraform workspace show
 dev

Destroy Resources per workspace:
$ terraform workspace delete dev



No comments:

Post a Comment