Terraform useful commands - Survival starting point
Tags: terraform, hcl, cloudcomputing, iaac
Published: 2021-11-05 18:41:00

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](https://learn.hashicorp.com/terraform)

**********************************************************

## Useful commands

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

**********************************************************

```
terraform init
```
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"
    }
  }
```
[Example source](https://learn.hashicorp.com/tutorials/terraform/aws-build?in=terraform/aws-get-started)

**********************************************************

```
terraform plan
```

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
```
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>
```
Provides details of an specific resource, example:
- terraform state show aws_instance.myfirstServer

**********************************************************

```
terraform apply --auto-approve
```
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
```
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
```
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
```
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
```
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"
```
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
```
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](https://github.com/aernesto24/elopez-terraform-templates)