top of page
  • Writer's pictureChristoffer Windahl Madsen

Getting started with using Terraform

This blog is all about helping anyone getting started with Terraform. First of all, let me disclose that, I myself, work on Windows and I use the IDE VsCode - I want to illustrate that it's important to use these exact tools that im using. With this in mind its very crucial to state that Terraform works as a simple binary/executeable file on all the different operation systems. Its simple to install, regardless of, OS, which is the local machine. Furthermore, I highly recommend installing a 'Hashicorp Debugging Extension' in your IDE of choice as it will help you greatly while diving into the world of coding Terraform.


Because I am on Windows I will also highly recommend using the following package manager 'chocolatey' to install Terraform. This is because the package manager will automatically not only download Terraform but it will also add the path to the binary in the Windows PATH environmental variable.


To get started, we need to install the packet manager itself. Open Powershell (either 5 or 7) as an administrator:


Copy in the following command to actually install 'chocolatey'

Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))

When it is complete run the following command to install Terraform:

choco install terraform

When this is done, simply run the command 'Terraform' to validate that everything is okay

Terraform has now been successfully installed and is ready to be used by the local system. Please now open VsCode and go to the extension page:

In the search field, search for 'Hashicorp Terraform' and select the first one. Install it. It will show up in the extensions list like so:

Now, the only thing left to do is to create main.tf file in a folder of your choosing. Inside of the main file, you will notice that the extension will start helping you. For example, lets start with defining the 'Terraform block' before I am even done typing it the extension will help me out like this:

Now lets say I define the following terraform code:

terraform {
  required_providers {
    azurerm = {
      source = "value"
    }
  }
}

Notice what happens if I take the mouse over the attribute 'source'

You can use the extension to your advantage by simply pulling the cursor in front of whatever you want to know more about. Now, lets make sure that Terraform can actually compile and execute some Terraform code.


Inside of VsCode press SHIFT + CTRL + P to open up the top middle menu. Search for your terminal of choice, in this example, I will be using Powershell:

If you look at the bottom option 'Terminal Window' will be present. Before we execute though, we need to have some codes to execute. If you already know Terraform you can type whatever you want or copy the following sample:

terraform {
  required_providers {
    azurerm = {
      source = "hashicorp/azurerm"
    }
  }
}

provider "azurerm" {
  features {
  }
}

locals {
  Write_to_screen = "Hello world"
}

output "write_out" {
  value = local.Write_to_screen
}

In the 'Terminal Window' make sure to have the session running in the same root folder as the main.tf file and then run command 'Terraform init'


Hopefully you recieved the message down below:

Now to finally initiate the deployment, run the command 'terraform apply--auto-approve=true'


(This command will not ask for confirmation, it is your responsibillity to know what you are deploying)


Congratulations on getting started with Terraform!


If you want to continue reading about Terraform, please click on -> Blog | Codeterraform

For any other subjects check out -> Blog | Codeterraform


Cheers!

23 views0 comments

Comentarios

Obtuvo 0 de 5 estrellas.
Aún no hay calificaciones

Agrega una calificación
bottom of page