Simple way to play around with terraform locally without any cloud account
Tags: terraform, iac, tutorial, devops
Published: 2023-12-23 11:23:59

I don't want to live under a bridge or on the streets. For this reason when I was learning terraform I was stressed about messing up something and getting a huge bill from aws or gcp. 
Fortunately I found an easy way to start playing around with terraform locally without using any cloud account or any complicated local kubernetes setup.
	
As you probably already know terraform is a tool that lets you provision and manage infrastructure resources. Things like virtual machines, EC2 instances, IAM users and so on... 
Basically terraform is able to create and manage any type of resources if there is a Provider written for it. There are separate providers for aws, gcp, digital ocean, you name it. You can browse the list of available Providers or you can even write your own. Here is the official [list of providers](https://registry.terraform.io/browse/providers).

In this tutorial we will be using the [local](https://registry.terraform.io/providers/hashicorp/local/latest) Provider as this can provision local files without needing any other setup. 

## Install terraform
Step 0 is to configure terraform on your machine. This is very easy to do, you just need to download a binary and set up its path in the path system variable. 
Download and unzip the terraform binary from the [terraform website](https://developer.hashicorp.com/terraform/install). I will place it in a newly created C:/terraform folder.
Next step is to add the location of the terraform.exe file to your PATH system variable. 
Go to System properties:
![System properties](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ortm71tqn9cvg746jqcj.png)
And select "Environment Variables..."
![Environment properties](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b16rtbb56kk9bl7lki2o.png)
In the "System variables" section find the "Path" variable and click "Edit..."
![Add terraform env variable](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lwnj9yk62z0oe6jalcwj.png)

## Write some terraform
Open up vs code (or any other IDE) in an empty folder and create a new file and name it main.tf
First we need to specify the providers we will be using and then we can specify the resources we want to create. Here is an example terraform file that will create a hello.txt file with the content of "hello world".

```
terraform {
    required_providers {
      local = {
        source = "hashicorp/local"
      }
    }
}

resource "local_file" "hello" {
    content = "hello world"
    filename = "hello.txt"
}
```
It's always a good idea to check the [documentation of the provider](https://registry.terraform.io/providers/hashicorp/local/latest/docs/resources/file) as it will tell us what resources it can create and what are the options we can set.
![terraform docs](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/26p1f7ksuj4j59xg3o7k.png)
As you can see the local provider has a resource called local file and it has a required filename property. Optionally we can also specify the content of the file.
## Terraform commands
Open a new terminal and run 
```
terraform init
```
This will Prepare your working directory and download all the necessary files for the provider.
Next run 
```
terraform validate
```
This command will validate the syntax of your code.
And now the fun part, let's run 
```
terraform plan
```
The plan command will output the list of changes that need to be made to reach the infrastructure described in your code. In this case the plan should be to create one new file.
And now let's run
```
terraform apply 
```
The apply command will persist the planned changes. In this case it will create the file. 
![terraform file](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qcy9d8iivrkz7k46as7y.png)
You can remove the file by running the destroy command:
```
terraform destroy
```

Now you can play around with it and see what happens if you modify your code and plan and apply it again or what happens if you modify the file manually. Have fun!

This tutorial is also available on youtube: [https://www.youtube.com/watch?v=nb6onGm970k](https://www.youtube.com/watch?v=nb6onGm970k)