Terraform

From Bonus Bits
Jump to: navigation, search

Description

This article gives references to useful syntax examples related to terraform CLI. Most if not all of these commands work out of your current working directory. It will create a .terraform folder for project configurations, providers and module downloads. Another directory created is a terraform.tfstate.d.<project name> folder when a plan is applied. This folder is critical because it holds unique information about the resources created with the plan. This is the folder you must share with others in a shared environment.

Setup New Project

Setup a new project or update after adding more dependencies (providers or modules).

Initialize

From the root of the terraform project directory where main.tf, outputs.tf and variables.tf reside. Run the init command to download providers and modules required by the project.

terraform int

Upgrade Providers & Modules

From the root of the terraform project directory where main.tf, outputs.tf and variables.tf reside. Run the init -upgrade command to download providers and modules required by the project.

terraform int -upgrade


Workspaces

Workspaces are a way to segment work or environments when using a shared Terraform project.

List Workspaces

terraform workspace list

List Current Workspace

terraform workspace show

Create a Workspace

terraform workspace new <workspace name>

Switch Workspace

terraform workspace select <workspace name>

Delete a Workspace

terraform workspace delete <workspace name>


Run Project

Show Plan

Show the changes that are going to occur if applied without any input varables.

terraform plan

Show the changes that are going to occur if applied with one or more variable files

terraform plan -var-files=environments/mywebstack/dev.tfvars -var-file=$HOME/.secrets/mywebstack/dev.tfvars

Apply

Apply all create and changes from the current directory

terraform apply

Apply all create and changes from another directory

terraform apply /stacks/stack1

Apply all create and changes from the current directory with variable files

terraform apply -var-files=environments/mywebstack/dev.tfvars -var-file=$HOME/.secrets/mywebstack/dev.tfvars

Apply all create and changes from the current directory with variable file & cli arg variables

terraform apply -var-files=environments/mywebstack/dev.tfvars -var 'data_bag_secret=FrzeVa8cyDAHWicV8hKni7P40xDPUn2Cm8638QvFS24iK7DRtP5VlDpogSaxDm050riKcAZ'


Related Articles


Sources