Terraform CLI commands Flashcards
State lock file
State lock file
.terraform.tfstate.lock.hcl
in directory terraform.tfstate.d
terraform workspace select default
change to the selected workspace
terraform workspace select default
terraform destroy –auto-approve
destroy/cleanup deployment without being prompted for “yes”
terraform destroy –auto-approve
terraform apply -replace=aws_instance.web
terraform apply -replace=aws_instance.web
Marks the resource as tainted, forcing it to be destroyed and recreated in the next apply.
Only modifies the state file marking the resource as tainted.
terraform init -lockfile=MODE
terraform init -lockfile=MODE
Set a dependency lockfile mode.
readonly: suppress the lockfile changes, but verify checksums against the information already recorded.
It conflicts with the -upgrade flag. If you update the lockfile with third-party dependency management tools, it would be useful to control when it changes explicitly.
Dependency lock file
Dependency lock file
,terraform.lock.hcl
terraform init -from-module=MODULE-SOURCE
terraform init -from-module=MODULE-SOURCE
init can be run against an empty directory with the -from-module=MODULE-SOURCE option, in which case the given module will be copied into the target directory before any other initialization steps are run.
terraform state list
list out all the resources tracked via the current state file
terraform state list
terraform plan -replace module.example.aws_instance.example
terraform plan -replace module.example.aws_instance.example
instructs Terraform to plan to replace the resource instance with the given address. This is helpful when one or more remote objects have become degraded, and you can use replacement objects with the same configuration to align with immutable infrastructure patterns.
terraform validate -backend=false
validate local code but skip backend validation
terraform validate -backend=false
terraform state show aws_instance.my_ec2
show details stored in Terraform state for the resource
terraform state show aws_instance.my_ec2
terraform import aws_instance.new_ec2_instance i-abcd1234
terraform import aws_instance.new_ec2_instance i-abcd1234
#import EC2 instance with id i-abcd1234 into the Terraform resource state. Have to add a blank resource "aws_instance.new_ec2_instance" first.
terraform apply does what?
terraform apply does what?
Refresh, executes plan, validate and the apply.
terraform apply -lock=true
lock the state file so it can’t be modified by any other Terraform apply or modification action(possible only where backend allows locking
terraform apply -lock=true
terraform workspace new mynewworkspace
create a new workspace and select
terraform workspace new mynewworkspace
terraform untaint aws_instance.my_ec2
Remove taint from a resource
terraform untaint aws_instance.my_ec2
terraform apply -target=aws_instance.my_ec2
only apply/deploy changes to the targeted resource
terraform apply -target=aws_instance.my_ec2
terraform state pull > terraform.tfstate
Pull current remote state and output to stdoutput
terraform state pull > terraform.tfstate
terraform output instance_public_ip
terraform output instance_public_ip
list out a specific declared output
terraform plan -destroy
terraform plan -destroy #outputs a destroy plan
terraform console
terraform console
This command provides an interactive command-line console for evaluating and experimenting with expressions.
Reads the configuration in the current working directory and locks the state file.
terraform fmt
terraform fmt
rewrite Terraform configuration files to a HCL canonical format and style. This command applies a subset of the Terraform language style conventions, along with other minor adjustments for readability.
terraform get -update=true
terraform get -update=true
Modules that are already downloaded will be checked for updates and the updates will be downloaded if present.
terraform workspace delete example
This command will delete the specified workspace “example”
terraform workspace delete example
terraform taint aws_instance.my_ec2
taints resource to be recreated on next apply
terraform taint aws_instance.my_ec2
echo “aws_instance.my_ec2.public_ip” | terraform console
display the Public IP against the “my_ec2” Terraform resource as seen in the Terraform state file
echo “aws_instance.my_ec2.public_ip” | terraform console