Bird
Raised Fist0
Terraformcloud~5 mins

Resource types and names in Terraform - Commands & Configuration

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
When you create infrastructure with Terraform, you define resources. Each resource has a type and a name. The type tells Terraform what kind of thing to create, like a server or a database. The name is how you refer to that resource in your code.
When you want to create a virtual machine in the cloud.
When you need to set up a storage bucket for your files.
When you want to manage a database instance alongside your app.
When you need to create a network or firewall rule.
When you want to organize your infrastructure code clearly.
Config File - main.tf
main.tf
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.0"
    }
  }
  required_version = ">= 1.0"
}

provider "aws" {
  region = "us-east-1"
}

resource "aws_instance" "my_server" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"
  tags = {
    Name = "MyServer"
  }
}

resource "aws_s3_bucket" "my_bucket" {
  bucket = "my-unique-bucket-terraform-12345"
  acl    = "private"
}

This file defines two resources using Terraform:

  • aws_instance my_server: Creates a virtual machine (server) in AWS with a specific image and type.
  • aws_s3_bucket my_bucket: Creates a private storage bucket with a unique name.

The resource keyword is followed by the resource type (like aws_instance) and the resource name (like my_server). The name is how you refer to this resource inside Terraform.

Commands
This command sets up Terraform in the current folder. It downloads the AWS provider plugin so Terraform can talk to AWS.
Terminal
terraform init
Expected OutputExpected
Initializing the backend... Initializing provider plugins... - Finding hashicorp/aws versions matching "~> 4.0"... - Installing hashicorp/aws v4.60.0... - Installed hashicorp/aws v4.60.0 (signed by HashiCorp) Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes required for your infrastructure.
This command shows what Terraform will do if you apply the configuration. It previews the resources it will create.
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.my_server will be created + resource "aws_instance" "my_server" { + ami = "ami-0c55b159cbfafe1f0" + instance_type = "t2.micro" + tags = { + "Name" = "MyServer" } } # aws_s3_bucket.my_bucket will be created + resource "aws_s3_bucket" "my_bucket" { + acl = "private" + bucket = "my-unique-bucket-terraform-12345" } Plan: 2 to add, 0 to change, 0 to destroy.
This command creates the resources defined in the configuration. The flag skips the confirmation prompt to make it faster.
Terminal
terraform apply -auto-approve
Expected OutputExpected
aws_instance.my_server: Creating... aws_s3_bucket.my_bucket: Creating... aws_s3_bucket.my_bucket: Creation complete after 2s [id=my-unique-bucket-terraform-12345] aws_instance.my_server: Still creating... [10s elapsed] aws_instance.my_server: Creation complete after 15s [id=i-0abcd1234efgh5678] Apply complete! Resources: 2 added, 0 changed, 0 destroyed.
→
-auto-approve - Skip manual approval prompt
This command lists all resources Terraform is currently managing. It shows the resource types and names.
Terminal
terraform state list
Expected OutputExpected
aws_instance.my_server aws_s3_bucket.my_bucket
Key Concept

If you remember nothing else from this pattern, remember: the resource type tells Terraform what to create, and the resource name is how you refer to it in your code.

Common Mistakes
Using the same resource name for two different resources of the same type.
Terraform requires unique names per resource type to track them correctly. Duplicate names cause errors.
Give each resource a unique name within its type, like aws_instance.my_server and aws_instance.my_server2.
Confusing resource type with resource name and using invalid names.
Resource type must be a valid provider resource, and names must be simple identifiers without spaces or special characters.
Use correct resource types from the provider documentation and simple lowercase names with underscores if needed.
Summary
Define resources in Terraform using resource type and resource name.
Use terraform init to set up providers before creating resources.
Use terraform plan to preview changes and terraform apply to create resources.
Use terraform state list to see all managed resources with their types and names.

Practice

(1/5)
1. What does the resource block in Terraform define?
easy
A. A provider configuration
B. A variable to store user input
C. A cloud service to create and manage
D. A function to run code

Solution

  1. Step 1: Understand Terraform resource block purpose

    The resource block tells Terraform what cloud service to create and manage.
  2. Step 2: Differentiate from other blocks

    Variables store input, functions run code, and providers configure cloud access, but resources define actual services.
  3. Final Answer:

    A cloud service to create and manage -> Option C
  4. Quick Check:

    Resource block = cloud service definition [OK]
Hint: Resource blocks create services, not variables or functions [OK]
Common Mistakes:
  • Confusing resource with variable or provider blocks
  • Thinking resource defines code logic
  • Mixing resource with output blocks
2. Which of the following is the correct syntax to declare an AWS S3 bucket resource named my_bucket in Terraform?
easy
A. resource aws_s3_bucket "my_bucket" {}
B. resource aws_s3_bucket my_bucket {}
C. resource "aws_s3_bucket" my_bucket {}
D. resource "aws_s3_bucket" "my_bucket" {}

Solution

  1. Step 1: Recall Terraform resource syntax

    Terraform resource syntax requires resource type and name as strings inside quotes: resource "type" "name" {}
  2. Step 2: Match correct option

    Only resource "aws_s3_bucket" "my_bucket" {} uses quotes correctly around both type and name.
  3. Final Answer:

    resource "aws_s3_bucket" "my_bucket" {} -> Option D
  4. Quick Check:

    Resource type and name must be quoted [OK]
Hint: Always quote resource type and name in Terraform [OK]
Common Mistakes:
  • Omitting quotes around type or name
  • Using quotes inconsistently
  • Swapping order of type and name
3. Given this Terraform code snippet:
resource "aws_instance" "web" {
  ami           = "ami-123456"
  instance_type = "t2.micro"
}

What is the resource type and name?
medium
A. Type: aws_instance, Name: web
B. Type: web, Name: aws_instance
C. Type: ami-123456, Name: t2.micro
D. Type: aws_instance, Name: ami-123456

Solution

  1. Step 1: Identify resource type and name in block

    The first quoted string after resource is the type: "aws_instance". The second is the name: "web".
  2. Step 2: Confirm values are correct

    AMI and instance_type are attributes, not resource type or name.
  3. Final Answer:

    Type: aws_instance, Name: web -> Option A
  4. Quick Check:

    Resource type and name = aws_instance, web [OK]
Hint: First quoted string = type, second = name [OK]
Common Mistakes:
  • Confusing attributes with resource type or name
  • Swapping type and name
  • Using attribute values as names
4. What is wrong with this Terraform resource declaration?
resource "aws_s3_bucket" my_bucket {
  bucket = "my-bucket-name"
}
medium
A. Resource name is not quoted
B. Resource type is missing
C. Bucket attribute is invalid
D. Missing provider block

Solution

  1. Step 1: Check resource declaration syntax

    Terraform requires both resource type and name to be quoted strings.
  2. Step 2: Identify the error

    The resource name my_bucket is not quoted, causing a syntax error.
  3. Final Answer:

    Resource name is not quoted -> Option A
  4. Quick Check:

    Resource names must be quoted strings [OK]
Hint: Always quote resource names to avoid syntax errors [OK]
Common Mistakes:
  • Forgetting quotes around resource names
  • Assuming attributes cause errors
  • Thinking provider block is mandatory here
5. You want to create two AWS EC2 instances with different names but the same type in Terraform. Which is the best way to name the resources?
hard
A. resource "aws_instance" "aws_instance" {} and resource "aws_instance" "aws_instance" {}
B. resource "aws_instance" "app1" {} and resource "aws_instance" "app2" {}
C. resource "aws_instance" "1" {} and resource "aws_instance" "2" {}
D. resource "aws_instance" "instance" {} and resource "aws_instance" "instance" {}

Solution

  1. Step 1: Understand resource naming rules

    Resource names must be unique within the same type to avoid conflicts.
  2. Step 2: Evaluate naming options

    resource "aws_instance" "app1" {} and resource "aws_instance" "app2" {} uses unique, descriptive names app1 and app2. Choices that reuse the same name cause errors. The choice using numeric names is valid but less descriptive.
  3. Final Answer:

    resource "aws_instance" "app1" {} and resource "aws_instance" "app2" {} -> Option B
  4. Quick Check:

    Unique descriptive names prevent conflicts [OK]
Hint: Use unique, clear names for each resource [OK]
Common Mistakes:
  • Using duplicate resource names
  • Using unclear or numeric-only names
  • Ignoring naming uniqueness rules