[DevOps]Infrastructure Provisioning (IaC)Terraform Resources — Deep Dive

Tonytruong
4 min readDec 30, 2022

--

Welcome Back! To another post on Infrastructure Provisioning — with Terraform! If you’ve stumbled across my recent posts I’ve covered the beginning steps and stages one should take to begin a DevOps Developer Career — following up with the first Infrastructure Provisioning post introducing Terraform, going over the basics, and what you should know

What better way to create a new deep dive series going over the basics we covered in our previous Terraform Post!

In this Medium Post with many scheduled others rapidly coming after, we will be doing a deep dive into Terraform Resources specifically:

  • What is it?
  • Key Concepts
  • Code Examples

What is Terraform Resources?

Resource Block Example for creating a new EC2 Server on AWS

Resources are the things that Terraform helps you set up and manage. These could be things like servers, databases, or networks. When you use Terraform, you write special files that tell it what kind of resources you want, and how you want them set up.

For example, if you want to create a server in the cloud, you can use a resource to tell Terraform what kind of server you want, and what you want to run on it. Then, Terraform will set up the server for you, and make sure it’s working the way you want.

You can use resources to set up all sorts of things, like networks, load balancers, and databases. And you can use them to manage things you’ve already set up, too.

Overall, resources are a really helpful part of Terraform because they make it easier to set up and manage the things you need to run your programs and websites.

Key Concepts

I personally believe the following list are some key concepts one should understand when it comes to dealing with Terraform Resources

  1. Resource types: Each resource belongs to a specific resource type, which determines the properties and behavior of the resource. For example, the aws_instance resource type represents an Amazon Web Services EC2 instance, and the azurerm_virtual_machine resource type represents an Azure virtual machine.
  2. Properties: Each resource type has a set of properties that can be configured to customize the behavior of the resource. For example, the aws_instance resource type has properties such as ami, instance_type, and key_name which can be used to customize the AMI, instance type, and SSH key pair used when creating the instance.
  3. Declarative approach: Terraform uses a declarative approach to resource management, which means that you only need to specify the desired end state of your infrastructure, and Terraform will figure out how to create it. This makes it easy to define and manage your infrastructure and helps to ensure that your resources are always in the desired state.
  4. Providers: Terraform uses “providers” to create and manage resources. Providers are responsible for understanding the API of the infrastructure platform, and for translating the Terraform configuration into API calls to create and manage resources.
  5. Dependencies: Resources can have dependencies on other resources, which means that one resource must be created before another can be created. Terraform will automatically manage these dependencies to ensure that resources are created in the correct order.
  6. Resource identifiers: Each resource has a unique identifier, called the “resource name”, which is used to refer to the resource in other parts of the configuration.

Code Examples

Below are a few quick examples you can start using to create AWS Resources using Terraform Resources.

Creating an EC2 Instance

resource "aws_instance" "example" {
ami = "ami-0ff8a91507f77f867"
instance_type = "t2.micro"
}

This resource block creates an AWS EC2 instance using the specified AMI (Amazon Machine Image) and instance type.

Creating an Azure Virtual Machine

resource "azurerm_virtual_machine" "example" {
name = "vm-example"
resource_group_name = azurerm_resource_group.example.name
location = azurerm_resource_group.example.location
vm_size = "Standard_D1_v2"
storage_image_reference {
publisher = "Canonical"
offer = "UbuntuServer"
sku = "20.04-LTS"
version = "latest"
}
storage_os_disk {
name = "vm-os-disk"
caching = "ReadWrite"
create_option = "FromImage"
managed_disk_type = "Standard_LRS"
}
os_profile {
computer_name = "vm-example"
admin_username = "azureuser"
admin_password = "P@$$w0rd1234"
}
os_profile_linux_config {
disable_password_authentication = false
}
network_interface_ids = [azurerm_network_interface.example.id]
}

This resource block creates an Azure virtual machine using the specified configuration options.

Creating a Google Cloud Platform (GCP) Instance

resource "google_compute_instance" "example" {
name = "vm-example"
machine_type = "f1-micro"
zone = "us-central1-a"

boot_disk {
initialize_params {
image = "debian-cloud/debian-10"
}
}

network_interface {
network = "default"
}
}

This resource block creates a GCP Compute Engine instance using the specified configuration options.

These are certainly a few specific examples of the many different resources one can start creating with Terraform and it is a good start for us to cover in our Infrastructure Provisioning Series. Consider giving me a follow for more content like this!

As always, if this content has helped you in any way — please feel free to give me a few claps 👏

--

--

Tonytruong

DevOps Fanatic with a penchant to automate anything and everything — Terraform Nerd, Docker practitioner and self proclaimed AWS guru.