Terraform Resources
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In the last blog post, we have discussed terraform providers.
https://cloudaffaire.com/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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
## Syntax resource " { depends_on = [" #resource dependencies that Terraform can"t automatically infer. #required only if dependent upon resource is not accessed by the resource count = #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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
##-------------------------- ## Terraform: Resources ## ##-------------------------- ## 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 = " secret_key = " region = "us-east-1" } provider "aws" { access_key = " secret_key = " alias = "mumbai" #non default provider defined region = "ap-south-1" } resource "aws_vpc" "myvpc" { provider = "aws.mumbai" cidr_block = "10.0.0.0/16" 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 = "10.0.0.0/24" tags { Name = "mybvpc_subnet_${count.index+1}" #count.index starts from zero, creates tag "myvpc_subnet_1" } } --------------- :wq ## Format code terraform fmt ## Initialize terraform terraform init ## Create the resource terraform apply #Login to AWS console and check VPC, subnet ## Destroy the resources terraform destroy ## Cleanup 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
https://www.terraform.io/docs/index.html