DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 968,873 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
Ernesto Lopez
Ernesto Lopez

Posted on

Terraform useful commands - Survival starting point

According to Terraform webpage:

Terraform is an open-source infrastructure as code software tool that provides a consistent CLI workflow to manage hundreds of cloud services. Terraform codifies cloud APIs into declarative configuration files.

Terraform provides Infrastructure as code for AWS, GCP, OCI, docker and others.

The idea with this post is to provide a series of useful commands in one place when you are starting with Terraform. This blog entry is not intended to be an exhaustive step by step guide of Terraform.

Terraform already provides a great resource for beginners:
Terraform Tutorials


Useful commands

NOTE this commands should be executed on the folder where you have your template.


terraform init
Enter fullscreen mode Exit fullscreen mode

Before start working with deployments, you need to initialize your directory, this should be done even if you are updating your configuration by downloading your template from a code repository.

Initializing a configuration directory downloads and installs the providers defined in the configuration.

A provider is the plugin that Terraform uses to create and manage your resources, for example if you are creating resources on aws your provider is aws.

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 3.27"
    }
  }
Enter fullscreen mode Exit fullscreen mode

Example source


terraform plan
Enter fullscreen mode Exit fullscreen mode

This is one of the most useful commands on terraform, it creates an execution plan

An execution plan is a list of resources that are going to be created or modified, allowing you to have a clear view if something is going to be deleted or updated, and also showing you any error on your templates


terraform state list
Enter fullscreen mode Exit fullscreen mode

List all resources we have state for, mostly resources that i created
State is used by Terraform to map resources on cloud providers to your local configuration. State is stored on a local file called "terraform.tfstate" , this file is used to generate plans and implement those changes on the infrastructure.


terraform state show <resource>
Enter fullscreen mode Exit fullscreen mode

Provides details of an specific resource, example:

  • terraform state show aws_instance.myfirstServer

terraform apply --auto-approve
Enter fullscreen mode Exit fullscreen mode

Apply your terraform template with automatic approval (you must be cautious with this), it is recommended to see the plan before applying the changes, you may find that some resources are deleted to generate the changes. For example changing an instance type.


terraform output
Enter fullscreen mode Exit fullscreen mode

Show all the outputs defined on your template

Outputs values are similar tu a return value from a function. These outputs can be used for:

  • Child modules exposing values to parent modules.
  • Printing values in the CLI after a resource is created.
  • Using values by other configuration throughout the remote state.

terraform refresh
Enter fullscreen mode Exit fullscreen mode

Refresh the state of your terraform deployment without actually redeploying anything, helpful if you add outputs to your template and want to see how it looks like


terraform destroy --target resource.name
Enter fullscreen mode Exit fullscreen mode

This command will destroy a specific resource instead of all the resources defined in the template

  • Example: terraform destroy --target aws_instance.myfirstServer

terraform apply --target resource.name
Enter fullscreen mode Exit fullscreen mode

This command will create a specific resource instead of all the resources defined in the template

  • Example: terraform apply --target aws_instance.myfirstServer

terraform apply -var "var_name=var_value"
Enter fullscreen mode Exit fullscreen mode

Apply a terraform template entering the value you defined in a variable inside the template.

  • Lets say you define the following variable:
    • variable "instance_type" { description = "Instance Type"}
    • you can apply it using terraform apply -var "instance_type=t3.micro"

terraform apply -var-file <name>.tfvars
Enter fullscreen mode Exit fullscreen mode

This command apply a template using the variables defined in a specific file.

  • Note by default terraform look for a file named terraform.tfvars for variables but you can rename this file and send it to terraform

You can find some persona examples and updates to this list of commands on my repo:
elopez-terraform-templates

Top comments (0)

🌚 Life is too short to browse without dark mode