Simple way to play around with terraform locally without any cloud account
Csaba Boros
Posted on December 23, 2023
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:
And select "Environment Variables..."
In the "System variables" section find the "Path" variable and click "Edit..."
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 as it will tell us what resources it can create and what are the options we can set.
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.
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
Posted on December 23, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
December 23, 2023