What is Terrafom? — VELV

velv Nearshore
3 min readNov 27, 2020
  • terraform.state: This file is responsible for saving the state in your cloud, it keeps stored what is deployed and what will change in your next update, terraform knows what needs to create, update or destroy.
  • main.tf: This file is responsible for saving all resources that the Developer/SysAdmin needs in the cloud. We can define VMs, Databases and all properties necessary to create it.
  • terraform.tfvars: This file will save all parameters that you need to configure, for example, depending which environment you want, how many RAM you need in your VM or which kind of storage do you need for your environment.

Terraform core will “compile” these files, send it to providers (Microsoft Azure, Amazon AWS, etc.) and apply your changes.

What is the main advantage to use Terraform?

  • Avoid misconfiguration, human error to create a resource, because everything is specified in terraform files.
  • It can be an automated process instead of waiting for someone to have time to create a new resource for you.

Using Terraform to manage your resources, you can easily create whole environments using terraform files and after that you can easily replicate this environment just by creating a new workspace.

Let’s make a practical example:

You decide that you need to create a production environment using these resources:

This should be your production environment. Now you need to create a staging or development environment using less resources:

You’ll be able also to reduce costs, assuming that your technical team just works workdays, you don’t need to pay for staging or development environments, every weekends or extra hours when no one is using it.

You can also easily manipulate this by using different using variables and workspaces.

Using terraform, someone that needs to create a new environment, doesn’t need to read all resources that the team uses, neither to read tons of documentation to see which kind of resource is necessary, just run some of the commands in terraform cli.

File structure

Assuming that you need to create a new Virtual machine in Microsoft Azure.

And following the steps in Azure portal you need to create

This file we will create an Ubuntu Server 16.04, using DS1 VM, Dynamic IP address, located in West US 2.

variable "prefix" {default = "tfvmex"resource "azurerm_resource_group" "main" {name = "${var.prefix}-resources"location = "West US 2"resource "azurerm_virtual_network" "main" {name = "${var.prefix}-network"address_space = ["10.0.0.0/16"]location = azurerm_resource_group.main.locationresource_group_name = azurerm_resource_group.main.nameresource "azurerm_subnet" "internal" {name = "internal"resource_group_name = azurerm_resource_group.main.namevirtual_network_name = azurerm_virtual_network.main.nameaddress_prefixes = ["10.0.2.0/24"]resource "azurerm_network_interface" "main" {name = "${var.prefix}-nic"location = azurerm_resource_group.main.locationresource_group_name = azurerm_resource_group.main.nameip_configuration {name = "testconfiguration1"subnet_id = azurerm_subnet.internal.idprivate_ip_address_allocation = "Dynamic"resource "azurerm_virtual_machine" "main" {name = "${var.prefix}-vm"location = azurerm_resource_group.main.locationresource_group_name = azurerm_resource_group.main.namenetwork_interface_ids = [azurerm_network_interface.main.id]vm_size = "Standard_DS1_v2"# Uncomment this line to delete the OS disk automatically when deleting the VM# delete_os_disk_on_termination = true# Uncomment this line to delete the data disks automatically when deleting the VM# delete_data_disks_on_termination = truestorage_image_reference {publisher = "Canonical"offer = "UbuntuServer"sku = "16.04-LTS"version = "latest"storage_os_disk {name = "myosdisk1"caching = "ReadWrite"create_option = "FromImage"managed_disk_type = "Standard_LRS"computer_name = "hostname"admin_username = "testadmin"admin_password = "Password1234!"os_profile_linux_config {disable_password_authentication = falseenvironment = "staging"

Basic Commands (terraform cli)

Terraform CLI, provides a tool where you can use it to run changes in your environment. Follow some basic commands that you can find in the documentation where you’ll see more details.

terraform init — This command is used to initialize a working directory containing Terraform configuration files

terraform planCreates an execution plan. Terraform performs a refresh, and then determines what actions are necessary to achieve the desired state specified in the configuration files.

terraform apply — Apply the changes required to reach the desired state of configuration.

Terraform destroy — destroys Terraform-managed infrastructure

Conclusion

This is a quick overview what is terraform does, but in short topics is possible to see how powerful it can be.

We hope it could open your mind to use this tool and any doubts you can keep in touch with our technical team where we can show you the best practices and how to use it correctly.

Originally published at https://www.velv.pt on November 27, 2020.

--

--

velv Nearshore

velv is specialized in software development and we offer an integrated service for nearshore teams.