DEV Community

Cover image for Simple way to play around with terraform locally without any cloud account
Csaba Boros
Csaba Boros

Posted on • Originally published at boroscsaba.com

Simple way to play around with terraform locally without any cloud account

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.

In this tutorial we will be using the local 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. 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
And select "Environment Variables..."
Environment properties
In the "System variables" section find the "Path" variable and click "Edit..."
Add terraform env variable

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"
}
Enter fullscreen mode Exit fullscreen mode

It's always a good idea to check the documentation of the provider as it will tell us what resources it can create and what are the options we can set.
terraform docs
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
Enter fullscreen mode Exit fullscreen mode

This will Prepare your working directory and download all the necessary files for the provider.
Next run

terraform validate
Enter fullscreen mode Exit fullscreen mode

This command will validate the syntax of your code.
And now the fun part, let's run

terraform plan
Enter fullscreen mode Exit fullscreen mode

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 
Enter fullscreen mode Exit fullscreen mode

The apply command will persist the planned changes. In this case it will create the file.
terraform file
You can remove the file by running the destroy command:

terraform destroy
Enter fullscreen mode Exit fullscreen mode

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

Top comments (0)