# Terraform - Essential Cheat Sheet

Terraform is a powerful Infrastructure as Code (IaC) tool that allows you to define and manage your infrastructure with simple, human-readable configuration files. As you dive into Terraform, mastering its essential commands is key to effectively managing your cloud infrastructure.

**<mark>1. terraform init</mark>**

Initializes a working directory. Run this first to set up your environment.

```plaintext
terraform init
```

Output:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723752551184/4ac04244-0c8b-4a47-83ed-b024a3727afe.png align="center")

**<mark>2. terraform plan</mark>**

Generates an execution plan, allowing you to preview the changes Terraform will make to your infrastructure. It helps you avoid unintended changes.

```plaintext
terraform plan
```

Output:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723752880431/e09fa625-b599-460f-9f82-637e3e77d8f7.png align="center")

<mark>3. terraform apply</mark>

Applies the changes defined in your Terraform configuration. This command provisions or updates your infrastructure.

```plaintext
terraform apply
```

Output:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723753453728/d15c947a-fcb3-44ef-a825-aa30e1abc264.png align="center")

If everything looks good, type `yes` to proceed

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723753021635/514cff26-5194-4834-979d-870db3963137.png align="center")

Terraform will then provision the resources

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723753252949/6332830a-72f7-48a3-8a9b-505a2fd668bf.png align="center")

**<mark>4. terraform destroy</mark>**

Removes all resources managed by Terraform. Use this when you want to clean up or decommission an environment.

```plaintext
terraform destroy
```

Output:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1723756432307/b6487973-a382-4827-9870-9067392f832b.png align="center")

**<mark>5.terraform refresh</mark>**

Updates the Terraform `state file` to reflect the current state of your infrastructure

```plaintext
terraform refresh
```

**<mark>6. terraform import</mark>**

Allows you to bring existing infrastructure under Terraform management.

```plaintext
terraform import
```
