Home More tutorials

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 variables

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".

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

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

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

Comments available on dev.to

Posts and tutorials
Terraform local file
An easy way to start playing around with terraform locally without using any cloud account or any complicated local kubernetes setup.
DevOps Terraform
Encryption at rest
Encryption at rest can come in handy when we have some sensitive data that we need to store in our database and we don't want to store it in plain text for everyone to see. In my case I needed to store some API keys in the database...
Entity Framework Security C# .NET
SSL
As with any technology, I think it's very important that first we understand the problem that this technology is trying to solve. Let's try to understand the problem through an example...
DevOps Security
Portfolio architecture
Initially I just created a simple static HTML and CSS page with some images to serve as my portfolio page - no frameworks, (almost) no javascript - why over complicate it? After some time I decided that I would also write a few blog posts and tutorials...
11ty Amazon S3 Github Actions CloudFront
SQL add days
Recently I had a task that seemed very simple at first. I had a table with billing start dates and number of business days to bill. The task was to calculate the end date for each row. Basically to calculate the start date + number of business days...
SQL MS SQL Server