Terraform CLI commands Flashcards

1
Q

State lock file

A

State lock file

.terraform.tfstate.lock.hcl
in directory terraform.tfstate.d

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

terraform workspace select default

A

change to the selected workspace

terraform workspace select default

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

terraform destroy –auto-approve

A

destroy/cleanup deployment without being prompted for “yes”

terraform destroy –auto-approve

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

terraform apply -replace=aws_instance.web

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

terraform init -lockfile=MODE

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Dependency lock file

A

Dependency lock file

,terraform.lock.hcl

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

terraform init -from-module=MODULE-SOURCE

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

terraform state list

A

list out all the resources tracked via the current state file

terraform state list

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

terraform plan -replace module.example.aws_instance.example

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

terraform validate -backend=false

A

validate local code but skip backend validation

terraform validate -backend=false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

terraform state show aws_instance.my_ec2

A

show details stored in Terraform state for the resource

terraform state show aws_instance.my_ec2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

terraform import aws_instance.new_ec2_instance i-abcd1234

A

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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

terraform apply does what?

A

terraform apply does what?

Refresh, executes plan, validate and the apply.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

terraform apply -lock=true

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

terraform workspace new mynewworkspace

A

create a new workspace and select

terraform workspace new mynewworkspace

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

terraform untaint aws_instance.my_ec2

A

Remove taint from a resource

terraform untaint aws_instance.my_ec2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

terraform apply -target=aws_instance.my_ec2

A

only apply/deploy changes to the targeted resource

terraform apply -target=aws_instance.my_ec2

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

terraform state pull > terraform.tfstate

A

Pull current remote state and output to stdoutput

terraform state pull > terraform.tfstate

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

terraform output instance_public_ip

A

terraform output instance_public_ip

list out a specific declared output

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

terraform plan -destroy

A
terraform plan -destroy
#outputs a destroy plan
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

terraform console

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

terraform fmt

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

terraform get -update=true

A

terraform get -update=true

Modules that are already downloaded will be checked for updates and the updates will be downloaded if present.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

terraform workspace delete example

A

This command will delete the specified workspace “example”

terraform workspace delete example

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
terraform taint aws_instance.my_ec2
terraform taint aws_instance.my_ec2 #taints resource to be recreated on next apply
26
echo "aws_instance.my_ec2.public_ip" | terraform console
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
27
terraform state mv aws_iam_role.my_ssm_role module.custom_module
terraform state mv aws_iam_role.my_ssm_role module.custom_module * rename existing resources * move a resource into a module * move a module into a module
28
terraform fmt -diff
terraform fmt -diff Used to see the differences?
29
terraform output -json
terraform output -json #list all outputs in JSON format
30
terraform -install-autocomplete
terraform -install-autocomplete ``` #Setup tab auto-completion, requires logging back in If you use either bash or zsh as your command shell, Terraform can provide tab-completion support for all command names and (at this time) some command arguments. ```
31
terraform apply plan.out
terraform apply plan.out #use the plan.out plan file to deploy infrastructure
32
terraform apply --auto-approve
terraform apply --auto-approve #apply changes without being prompted to enter “yes”
33
terraform force-unlock LOCK_ID
terraform force-unlock LOCK_ID #forcefully unlock a locked state file, LOCK_ID provided when locking the State file beforehand
34
terraform init -from-module=MODULE-SOURCE
terraform init -from-module=MODULE-SOURCE the given module will be copied into the target directory before any other initialization steps are run. Given a version control source, it can serve as a shorthand for checking out a configuration from version control and then initializing the working directory for it. If the source refers to an example configuration, it can be copied into a local directory to be used as a basis for a new configuration.
35
terraform apply -refresh-only flag
terraform apply -refresh-only flag When an approved manual configuration of a resource has changed or removed We use the––refresh--only flag to reflect the changes in our state file
36
terraform login
terraform login #obtain and save API token for Terraform cloud
37
echo 'join(",",["foo","bar"])' | terraform console
echo 'join(",",["foo","bar"])' | terraform console #echo an expression into terraform console and see its expected result as output
38
terraform providers
terraform providers #get information about providers used in current configuration
39
terraform validate
terraform validate ``` #validate code for syntax. Validate runs checks that verify whether a configuration is syntactically valid and internally consistent, regardless of any provided variables or existing state. Directory must be initialized or will error. ```
40
terraform plan -detailed-exitcode
terraform plan -detailed-exitcode Return a detailed exit code when the command exits. When provided, this argument changes the exit codes and their meanings to provide more granular information about what the resulting plan contains:
41
terraform import 'aws_instance.new_ec2_instance[0]' i-abcd1234
terraform import 'aws_instance.new_ec2_instance[0]' i-abcd1234 imports a real-world resource into state. Have to add a blank resource "aws_instance" "new_ec2_instance" first.
42
terraform init
terraform init • Downloading plugin dependencies e.g. Providers and Modules to .terraform\provifers\registry.terraform.io • Create a .terraform directory • Create a dependency lock file to enforce expected versions for plugins and terraform itself - Caches source code locally
43
terraform graph | dot -Tpng > graph.png
terraform graph | dot -Tpng > graph.png #produce a PNG diagrams showing relationship and dependencies between Terraform resource in your configuration/code
44
terraform state pull
terraform state pull This command will download the state from its current location, upgrade the local copy to the latest state file version that is compatible with locally-installed Terraform, and output the raw format to stdout. This is useful for reading values out of state (potentially pairing this command with something like jq). It is also useful if you need to make manual modifications to state.
45
terraform apply refresh=false
terraform apply refresh=false do not reconcile state file with real-world resources(helpful with large complex deployments for saving deployment time)
46
terraform fmt -list=false
terraform fmt -list=false Don’t want to see the list of file changes.
47
terraform state replace-provider hashicorp/aws registry.custom.com/aws
terraform state replace-provider hashicorp/aws registry.custom.com/aws #replace an existing provider with another
48
terraform fmt -recursive
terraform fmt -recursive Run format in subdirectories as well.
49
terraform init -get-plugins=false
terraform init -get-plugins=false #initialize directory, do not download plugins
50
terraform workspace show
terraform workspace show Output the current workspace
51
terraform apply -var my_region_variable=us-east-1
terraform apply -var my_region_variable=us-east-1 #pass a variable via command-line while applying a configuration
52
terraform state push

``` terraform state push
 # The terraform state push command is used to manually upload a local state file to remote state. This command also works with local state. ```
53
terraform init -verify-plugins=false
terraform init -verify-plugins=false #initialize directory, do not verify plugins for Hashicorp signature
54
terraform output
terraform output Used to extract the value of an output variable from the state file.
55
terraform state rm 'packet_device.worker'

terraform state rm 'packet_device.worker'
 #The terraform state rm command is used to remove items from the Terraform state. This command can remove single resources, single instances of a resource, entire modules, and more.
56
terraform get
terraform get Used to download and update modules mentioned in the root module. The modules are downloaded into a .terraform subdirectory of the current working directory. Don't commit this directory to your version control repository.
57
terraform plan -target=ADDRESS
terraform plan -target=ADDRESS Instructs Terraform to focus its planning efforts only on resource instances which match the given address and on any objects that those instances depend on. Note: Use -target=ADDRESS in exceptional circumstances only, such as recovering from mistakes or working around Terraform limitations.
58
terraform logout
terraform logout Log out of Terraform Cloud, defaults to hostname app.terraform.io
59
terraform workspace list
terraform workspace list #list out all workspaces
60
terraform state rm aws_instance.myinstace
terraform state rm aws_instance.myinstace #unmanage a resource, delete it from Terraform state file
61
terraform apply --parallelism=5
terraform apply --parallelism=5 #number of simultaneous resource operations
62
terraform init -reconfigure
terraform init -reconfigure When backend changes, run this to migrate to new settings, like move to S3 remote,
63
terraform plan -out plan.out
terraform plan -out plan.out #output the deployment plan to plan.out
64
terraform init -get=false
terraform init -get=false modules that are already downloaded will NOT be checked for updates
65
terraform version
terraform version #display Terraform binary version, also warns if version is old
66
Terraform plan
Terraform plan Read current state of the managed environment - refresh Compare to the current state file Propose a set of changes to make remote match configurations Validate