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
Q

terraform taint aws_instance.my_ec2

A

taints resource to be recreated on next apply

terraform taint aws_instance.my_ec2

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

echo “aws_instance.my_ec2.public_ip” | terraform console

A

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

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

terraform state mv aws_iam_role.my_ssm_role module.custom_module

A

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
Q

terraform fmt -diff

A

terraform fmt -diff

Used to see the differences?

29
Q

terraform output -json

A

list all outputs in JSON format

terraform output -json

30
Q

terraform -install-autocomplete

A

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
Q

terraform apply plan.out

A

use the plan.out plan file to deploy infrastructure

terraform apply plan.out

32
Q

terraform apply –auto-approve

A

apply changes without being prompted to enter “yes”

terraform apply –auto-approve

33
Q

terraform force-unlock LOCK_ID

A

forcefully unlock a locked state file, LOCK_ID provided when locking the State file beforehand

terraform force-unlock LOCK_ID

34
Q

terraform init -from-module=MODULE-SOURCE

A

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
Q

terraform apply -refresh-only flag

A

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
Q

terraform login

A

obtain and save API token for Terraform cloud

terraform login

37
Q

echo ‘join(“,”,[“foo”,”bar”])’ | terraform console

A

echo an expression into terraform console and see its expected result as output

echo ‘join(“,”,[“foo”,”bar”])’ | terraform console

38
Q

terraform providers

A

get information about providers used in current configuration

terraform providers

39
Q

terraform validate

A

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
Q

terraform plan -detailed-exitcode

A

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
Q

terraform import ‘aws_instance.new_ec2_instance[0]’ i-abcd1234

A

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
Q

terraform init

A

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
Q

terraform graph | dot -Tpng > graph.png

A

produce a PNG diagrams showing relationship and dependencies between Terraform resource in your configuration/code

terraform graph | dot -Tpng > graph.png

44
Q

terraform state pull

A

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
Q

terraform apply refresh=false

A

terraform apply refresh=false

do not reconcile state file with real-world resources(helpful with large complex deployments for saving deployment time)

46
Q

terraform fmt -list=false

A

terraform fmt -list=false

Don’t want to see the list of file changes.

47
Q

terraform state replace-provider hashicorp/aws registry.custom.com/aws

A

replace an existing provider with another

terraform state replace-provider hashicorp/aws registry.custom.com/aws

48
Q

terraform fmt -recursive

A

terraform fmt -recursive

Run format in subdirectories as well.

49
Q

terraform init -get-plugins=false

A

initialize directory, do not download plugins

terraform init -get-plugins=false

50
Q

terraform workspace show

A

terraform workspace show

Output the current workspace

51
Q

terraform apply -var my_region_variable=us-east-1

A

pass a variable via command-line while applying a configuration

terraform apply -var my_region_variable=us-east-1

52
Q

terraform state push


A
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
Q

terraform init -verify-plugins=false

A

initialize directory, do not verify plugins for Hashicorp signature

terraform init -verify-plugins=false

54
Q

terraform output

A

terraform output

Used to extract the value of an output variable from the state file.

55
Q

terraform state rm ‘packet_device.worker’


A

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.

terraform state rm ‘packet_device.worker’


56
Q

terraform get

A

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
Q

terraform plan -target=ADDRESS

A

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
Q

terraform logout

A

terraform logout

Log out of Terraform Cloud, defaults to hostname app.terraform.io

59
Q

terraform workspace list

A

list out all workspaces

terraform workspace list

60
Q

terraform state rm aws_instance.myinstace

A

unmanage a resource, delete it from Terraform state file

terraform state rm aws_instance.myinstace

61
Q

terraform apply –parallelism=5

A

number of simultaneous resource operations

terraform apply –parallelism=5

62
Q

terraform init -reconfigure

A

terraform init -reconfigure

When backend changes, run this to migrate to new settings, like move to S3 remote,

63
Q

terraform plan -out plan.out

A

output the deployment plan to plan.out

terraform plan -out plan.out

64
Q

terraform init -get=false

A

terraform init -get=false

modules that are already downloaded will NOT be checked for updates

65
Q

terraform version

A

display Terraform binary version, also warns if version is old

terraform version

66
Q

Terraform plan

A

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