terraform cli - ghdrako/doc_snipets GitHub Wiki

  • terraform console - open a Terraform console to test or get the output of the code of certain Terraform functions.
$ terraform console
> max(5,10,-5)
10
>  { for k,v in tomap({"a"="one","b"="two"}): v=>k}
{
  "one" = "a"
  "two" = "b"
}
> lookup(tomap({"a"="ala", "e"="ela"}),"a","brak")
"ala"
> lookup(tomap({"a"="ala", "e"="ela"}),"c","brak")
"brak"
  • terraform fmt - rewrite configuration files to a canonical format and style - linting.\
terraform fmt -recursive
  • terraform graph - generate a visual representation of the Terraform configuration or execution plan. You can expect output in a DOT format. You can use Graphviz (http://www.graphviz.org/) to generate charts
terraform graph | dot -Tsvg > graph.svg
  • terraform output

  • terraform refresh This command helps you to update the Terraform state file, comparing it with the real-world infrastructure. It doesn't make changes to the infrastructure directly, but your infrastructure may expect changes when you run the terraform plan and apply commands after terraform refresh. This command also helps to figure out whether there is any kind of drift from the last-known state and updates the state file accordingly. For a better understanding of this command, refer to the following link: https://www.terraform.io/docs/commands/refresh.html.

  • terraform show: This command can help you see outputs on the CLI itself from the state file, and these will be in a human-readable format. If you are looking for specific information from your state file, you can use this command.

  • terraform taint Using this command, you can mark specific resources to get destroyed and recreated. Many times, if you encounter some sort of deployment error, then this command can be very useful. This command doesn't modify the infrastructure but it does perform changes to the state file, which means that if you are going to run terraform apply, it will destroy the specific resource that has been marked as tainted and recreate it.

$ terraform taint aws_security_group.rdp_allow

The resource has been marked as tainted, so this specific resource will get destroyed and recreated when we perform terraform apply.

Better alternative:

terraform apply -replace="aws_instance.example[0]"
  • terraform workspace - create virtual environments for Terraform's reference (in example lab,test,dev,prod), and Terraform will maintain the state file and plugins for the respective environment accordingly

  • terraform force-unlock - unlock state file (if someone else lock only in remote backend). In local backend is imposible to unlock.

  • terraform validate - some sort of syntax error in your configuration file

  • terraform import - import some already existing resources or services (referencing as resource id) into your Terraform state file

terraform import aws_instance.abc i-acdf10234

The aws_security_group.rdp_allow resource has been marked as tainted, so this