0
0
Terraformcloud~5 mins

Why resources are Terraform's core - Why It Works

Choose your learning style9 modes available
Introduction
Terraform manages infrastructure by defining resources that represent real-world components like servers or databases. Resources are the main building blocks that Terraform creates, updates, and deletes to match your desired setup.
When you want to create a virtual machine in the cloud to run your application
When you need to set up a database instance for storing your app data
When you want to configure a network with subnets and firewalls for security
When you need to manage cloud storage buckets for your files
When you want to automate the setup of any cloud service or infrastructure component
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 tells Terraform which cloud to use and where.

The resource block defines a virtual machine (EC2 instance) with a specific image and size.

Terraform uses this resource to create and manage the server in the cloud.

Commands
This command sets up Terraform in your folder by downloading the necessary plugins for AWS. It prepares Terraform to work with your configuration.
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 to reach the desired state. It previews creating the EC2 instance without making any changes yet.
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" + instance_type = "t2.micro" + tags = { + "Name" = "example-instance" } } Plan: 1 to add, 0 to change, 0 to destroy. ───────────────────────────────────────────────────────────────────────────── Note: You didn't specify an "-out" parameter to save this plan, so Terraform can't guarantee to take exactly these actions if you run "terraform apply" later.
This command applies the changes to create the EC2 instance. The flag skips the confirmation prompt to speed up the process.
Terminal
terraform apply -auto-approve
Expected OutputExpected
aws_instance.example: Creating... aws_instance.example: Still creating... [10s elapsed] aws_instance.example: Creation complete after 20s [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 infrastructure Terraform manages, showing details of the created EC2 instance.
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: resources are the real things Terraform creates and manages to build your infrastructure.

Common Mistakes
Writing a resource block with missing required fields like AMI or instance type
Terraform cannot create the resource without essential information, causing errors during apply
Always include all required fields for each resource type as documented
Running terraform apply before terraform init
Terraform needs to download provider plugins first; skipping init causes errors
Always run terraform init first to prepare the working directory
Not running terraform plan before apply
You miss the chance to review changes, which can lead to unexpected infrastructure updates
Run terraform plan to preview changes before applying
Summary
Define resources in Terraform files to represent real infrastructure components.
Use terraform init to prepare Terraform and download providers.
Use terraform plan to preview changes and terraform apply to create or update resources.