Terraform Resources

Terraform Resources

Hello Everyone

Welcome to CloudAffaire and this is Debjeet.

In the last blog post, we have discussed terraform providers.


In this blog post, we will discuss terraform resources.

What is Terraform resource?

The resource is one of the core elements of a terraform script. A resource represents infrastructure components like network, server, database, etc. Whatever we need to create\update\delete in the infrastructure through terraform needs to be defined in the resource block. Each resource has its own sets of parameter depending upon the provider specified.

Resource Syntax:

resource "<resource_type>" "<resource_local_name>"
  depends_on = ["<resource_type>.<resource_local_name>"]
      #resource dependencies that Terraform can"t automatically infer.
      #required only if dependent upon resource is not accessed by the resource
  count = <integer> 
      #for multiple resource creation
  provider = <"provider.alias">
      #for selecting non default provider (provider with alias)
  lifecycle {create_before_destroy/prevent_destroy/ignore_changes}
      #describes lifecycle of the resource
      #create_before_destroy: resource will created first before destroy
      #prevent_destroy: disable resource destroy
      #ignore_changes: do not update drift as per configuration setting
  provisioner and connection
      #for taking extra actions after resource creation, covered in provisioner blog post

Resource demo:

#create a directory and get inside it
mkdir terraform && cd terraform

#create main.tf
vi main.tf
#Create a VPC and a subnet
provider "aws" {
access_key = "<your_access_key>"
secret_key = "<your_secret_key>"
region = "us-east-1"
provider "aws" {
access_key = "<your_access_key>"
secret_key = "<your_secret_key>"
alias = "mumbai" #non default provider defined
region = "ap-south-1"
resource "aws_vpc" "myvpc" {
provider = "aws.mumbai"
cidr_block = ""
tags {
Name = "myvpc"
resource "aws_subnet" "myvpc_subnet_one" {
count = 1 #one copy of resource will be created
depends_on = ["aws_vpc.myvpc"] #for demo purpose, not required as we are explicetly passing vpc id
provider = "aws.mumbai" #call non default provider
lifecycle { #defines life cycle of the resource, vpc will be created 1st before destroy
create_before_destroy = true
provisioner "local-exec" { #prints message in the terminal of local host
command = "echo 'creating myvpc subnet'"
#connection {} #subnet does not support connection attribute, will be covered in provisioner demo.
vpc_id = "${aws_vpc.myvpc.id}" #vpc id from above vpc resource is passed for subnet creation
cidr_block = ""
tags {
Name = "mybvpc_subnet_${count.index+1}" #count.index starts from zero, creates tag "myvpc_subnet_1"

#format code
terraform fmt

#initialized terraform
terraform init

#create the resource
terraform apply

#Login to AWS console and check VPC, subnet

#destroy the resources
terraform destroy

cd .. && rm -rf terraform

Hope you have enjoyed this article. In the next blog post, we will discuss terraform variables.

To get more details on terraform, please refer below terraform documentation



Leave a Reply

Close Menu