Implement and maintain state Flashcards
What are Backends?
A “backend” in Terraform determines how state is loaded and how an operation such as apply is executed. This abstraction enables non-local file state storage, remote execution, etc.
By default, Terraform uses the “local” backend, which is the normal behavior of Terraform
What is local Backend?
The local backend stores state on the local filesystem, locks that state using system APIs, and performs operations locally. // Example terraform { backend "local" { path = "relative/path/to/terraform.tfstate" } }
What is the default path for the local backend?
This defaults to “terraform.tfstate” relative to the root module by default.
What is State Locking?
If supported by your backend, Terraform will lock your state for all
operations that could write state. This prevents others from
acquiring the lock and potentially corrupting your state.
State locking happens automatically on all operations that could
write state. You won’t see any message that it is happening. If state
locking fails, Terraform will not continue.
Does Terraform continue if state locking fails?
No.
If state locking fails, Terraform will not continue.
Can you disable state locking?
Yes.
You can disable state locking for most commands with the -lock flag
but it is not recommended.
What are the types of Backend?
Standard: State management, functionality covered in State Storage & Locking
Enhanced: Everything in standard plus remote operations.
What are remote Backends?
Remote backends allow Terraform to use a shared storage space for state data, so any member of your team can use Terraform to manage the same infrastructure.
What is the benefit of using remote backend?
Remote state storage makes collaboration easier and keeps state and secret information off your local disk.
Remote state is loaded only in memory when it is used.
If you want to switch from using remote backend to local backend. What should you do?
If you want to move back to local state, you can remove the backend
configuration block from your configuration and run terraform init
again.
Terraform will once again ask if you want to migrate your state back
to local.
What does the command refresh do?
The terraform refresh command is used to reconcile the state
Terraform knows about (via its state file) with the real-world
infrastructure.
This can be used to detect any drift from the last-known state, and
to update the state file.
Does the command refresh modify the infrastructure?
The command refresh does not modify infrastructure, but does modify
the state file.
If the state is changed, this may cause changes to occur during the
next plan or apply.
How do you backup the state to the remote backend?
- When configuring a backend for the first time (moving from no
defined backend to explicitly configuring one), Terraform will give
you the option to migrate your state to the new backend. This lets
you adopt backends without losing any existing state. - To be extra careful, we always recommend manually backing up your
state as well. You can do this by simply copying your
terraform.tfstate file to another location.
What is a partial configuration in terms of configuring Backends?
You do not need to specify every required argument in the backend
configuration. Omitting certain arguments may be desirable to avoid
storing secrets, such as access keys, within the main configuration.
When some or all of the arguments are omitted, we call this a partial
configuration.
What are the ways to provide remaining arguments when using partial configuration?
Interactively: Terraform will interactively ask you for the required values, unless interactive input is disabled. Terraform will not prompt for optional values.
File: A configuration file may be specified via the init command line. To specify a file, use the -backend-config=PATH option when running terraform init. If the file contains secrets it may be kept in a secure data store, such as Vault, in which case it must be downloaded to the local disk before running Terraform.
Command-line key/value pairs: Key/value pairs can be specified via the init command line. Note that many shells retain command-line flags in a history file, so this isn’t recommended for secrets. To specify a single key/value pair, use the -backend-config=”KEY=VALUE”
option when running terraform init.
https://www.terraform.io/docs/backends/config.html