backend
block is within a terraform
block usually in a versions.tf
file.terraform plan
the blob container goes into a lease state of leased, once activity is finished it goes into an available state.backend
block within a terraform
block to use an Azure storage account:
backend "azurerm" {
resource_group_name = "rg1"
storage_account_name = "sta1"
container_name = "tfstatefiles"
key = "terraform.tfstate"
}
terraform_remote_state
data source retrieves the root module output values from some other terraform configuration.terraform_remote_state
block configuration used by Project-2 to access the output values defined in Project-1.data "terraform_remote_state" "Project-1" {
backend = "azurerm"
config = {
resource_group_name = "rg1"
storage_account_name = "sta1"
container_name = "tfstatefiles"
key = "terraform.tfstate"
}
}
azurerm_linux_virtual_machine
resource block as follows:resource_group_name = data.terraform_remote_state.Project-1.outputs.resource_group_name
terraform show
by default outputs the local terraform.tfstate file contents.terraform plan -out="v1plan.out"
- write a terraform plan out to a file.
terraform show "v1plan.out"
- view contents of terraform plan output file.terraform show -json "v1plan.out"
- as above but in json format.terraform apply "v1plan.out"
- this applies the terraform plan without re-running a plan.
terraform state list
- list resources in a state file. Note: data sources appear as resources.terraform state show data.azurerm_subscription.current
- shows attributes of a single resource in the state file.
terraform state mv
this allows you to change local resource names, for example:
terraform state list
terraform state mv -dry-run azurerm_virtual_network.myvnet azurerm_virtual_network.myvnet-new
terraform state mv azurerm_virtual_network.myvnet azurerm_virtual_network.myvnet-new
terraform state list
Note: If configuration has different local names then it will see that it needs to create a new resource and destroy the incorrectly named local named resource. They need to match.
terraform apply -refresh-only
- refreshes the state file to match the current settings of managed remote objects.
terraform refresh
- This is now deprecated as default behaviour is unsafe, it is the equivalent to terraform apply -refresh-only -auto-approve
.terraform state rm
- remove resources from the terraform state e.g. terraform state rm azurerm_virtual_network.myvnet-new
.
terraform state replace-provider
- you may download a copy of a provider plugin and store it in an internal repository and access it from an internal source. This command allows you to update the configuration to use an internal source e.g. terraform state replace-provider hashicorp/aws registry.acme.corp/acme/aws.
terraform state pull
- download and output state file to cli. You could copy and paste the contents into a file to make a local terraform.tfstate.terraform state push terraform.tfstate
- used to migrate local state to remote state.
terraform force-unlock LOCK_ID
- only apply’s to AWS Dynamo DB.
terraform taint
- force the re-creation of resources. It marks a resource as tainted so that it is destroyed and then created.
terraform taint azurerm_virtual_network.myvnet-new
would result in the recreation of the VNET on the next terraform apply
.terraform untaint
- remove tain mark on resource.
terraform plan/appy -target
- apply configuration to a specific resource. It is only used in exceptional circumstances e.g. recover from mistakes or work around limitations.resource "azurerm_resource_group" "rg1" {
}
terraform init
terraform import azurerm_resource_group.rg1 /subscriptions/a529f686-82de-4a8d-b643-747ed505372a/resourceGroups/rg1
Note: The typical format of $env:TF_VAR_<variable_name>=value
that is the equivalent to .tfvars
does not apply.
$env:TF_LOG='TRACE'
$env:TF_LOG_PATH='terraform-trace.log'
Dir env:
.bashrc
. In windows open $profile
(opens Microsoft.Powershell_profile.ps1) and add the lines above.terraform.rc
in $env:APPDATA
–> C:\Users\userName\AppData\Roaming
..terraformrc
in $home
.credentials_block
- add terraform cloud token.credentials_helper
- you can retrieve credentials using external source.disable_checkpoint
- if true disables upgrade and security check to HashiCorp, if used in conjuction with plugin_cache_dir
terraform cli will not reach out to HashiCorp network services.plugin_cache_dir
- store plugins that are downloaded into a central place as well as terraform working directory.disable_checkpoint_signature
- disables use of anonymous id used to de-duplicate warning messages.Provider_installation
- default uses registry.terraform.io, you may wish to use another path.mkdir -p $HOME/.terraform.d/plugin-cache
.terraformrc
file contentsplugin_cache_dir = "$HOME/.terraform.d/plugin-cache"
disable_checkpoint = true
terraform init
output demonstrating use of plugin-cache:Initializing provider plugins...
- Reusing previous version of hashicorp/external from the dependency lock file
- Reusing previous version of hashicorp/azurerm from the dependency lock file
- Reusing previous version of hashicorp/random from the dependency lock file
- Using previously-installed hashicorp/external v2.2.0
- Using previously-installed hashicorp/azurerm v2.93.1
- Using previously-installed hashicorp/random v3.1.0
Terraform has been successfully initialized!
terraform providers
- show which providers are required for the configuration.terraform version
- current version of terraform cli, it will also show plugin versions after terraform init
has run.terraform providers lock
- gathers information regarding providers and inserts it into .terraform.lock.hcl
..terraform.lock.hcl
for all plaforms that you intend to execute code from: terraform providers lock -platform=windows_amd64 -platform=darwin_amd64 -platform=linux_amd64
.terraform providers mirror .\mirror1\
- downloads the providers required for the current configuration and copies them into a directory.
terraform providers mirror -platform=windows_amd64 -platform=darwin_amd64 -platform=linux_amd64 .\mirror2multiplatform\
.terraform providers schema -json | jq
- print detailed schemas, this can be used to understand what resources and arguments may be used with the provider.