0
0
Terraformcloud~5 mins

Resource block syntax in Terraform - Commands & Configuration

Choose your learning style9 modes available
Introduction
Terraform uses resource blocks to define infrastructure components like servers or databases. Each block tells Terraform what to create and how to configure it.
When you want to create a virtual machine in the cloud with specific settings.
When you need to set up a storage bucket to hold files for your application.
When you want to define a network with certain rules and subnets.
When you need to manage DNS records for your website.
When you want to automate creating and updating cloud resources consistently.
Config File - main.tf
main.tf
provider "aws" {
  region = "us-east-1"
}

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

  tags = {
    Name = "example-instance"
  }
}

The provider block sets the cloud provider and region.

The resource block defines an AWS EC2 instance with a specific AMI and instance type.

The tags section adds a name label to the instance for easy identification.

Commands
This command initializes the Terraform working directory. It downloads the provider plugins needed to create resources.
Terminal
terraform init
Expected OutputExpected
Initializing the backend... Initializing provider plugins... - Finding latest version of hashicorp/aws... - Installing hashicorp/aws v4.0.0... - Installed hashicorp/aws v4.0.0 (signed by HashiCorp) Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure.
This command shows what Terraform will do before making any changes. It previews the resources to be created.
Terminal
terraform plan
Expected OutputExpected
An execution plan has been generated and is shown below. Resource actions are indicated with the following symbols: + create Terraform will perform the following actions: # aws_instance.example will be created + resource "aws_instance" "example" { + ami = "ami-0c55b159cbfafe1f0" + arn = (known after apply) + instance_type = "t2.micro" + tags = { + "Name" = "example-instance" } } Plan: 1 to add, 0 to change, 0 to destroy.
This command applies the changes to create the resources defined in the configuration. The flag skips the confirmation prompt.
Terminal
terraform apply -auto-approve
Expected OutputExpected
aws_instance.example: Creating... aws_instance.example: Still creating... [10s elapsed] aws_instance.example: Creation complete after 15s [id=i-0abcd1234efgh5678] Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
-auto-approve - Skips manual approval to apply changes immediately
This command displays the current state of the resources Terraform manages, showing details of the created infrastructure.
Terminal
terraform show
Expected OutputExpected
aws_instance.example: id = i-0abcd1234efgh5678 ami = ami-0c55b159cbfafe1f0 instance_type = t2.micro tags = { Name = example-instance }
Key Concept

If you remember nothing else from this pattern, remember: a resource block in Terraform defines one piece of infrastructure with its settings.

Common Mistakes
Forgetting to specify the resource type or name inside the resource block.
Terraform cannot identify what resource to create or how to reference it later.
Always include both the resource type and a unique name, like resource "aws_instance" "example".
Using incorrect or missing required arguments inside the resource block.
Terraform will fail to plan or apply because it lacks necessary information to create the resource.
Check the provider documentation for required arguments and include them all.
Not running terraform init before terraform plan or apply.
Terraform won't have the provider plugins downloaded and will error out.
Always run terraform init first to prepare the working directory.
Summary
Define infrastructure components using resource blocks with a type and name.
Initialize Terraform with terraform init to download providers.
Use terraform plan to preview changes and terraform apply to create resources.
Check created resources with terraform show.