Bird
Raised Fist0
Terraformcloud~5 mins

Block syntax and structure 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
Terraform uses blocks to organize configuration into sections. Each block defines a resource, variable, or other settings to build infrastructure step-by-step.
When you want to create a virtual machine in the cloud with specific settings.
When you need to define a network with subnets and security rules.
When you want to set variables to reuse values across your infrastructure.
When you want to group related settings like provider details or outputs.
When you want to organize your infrastructure code clearly and logically.
Config File - main.tf
main.tf
terraform {
  required_version = ">= 1.0"
}

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

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

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

variable "instance_count" {
  type    = number
  default = 1
}

output "instance_id" {
  value = aws_instance.example.id
}

terraform block sets Terraform version requirements.

provider block configures the cloud provider and region.

resource block defines a cloud resource with its properties.

variable block declares input variables with types and defaults.

output block specifies values to show after deployment.

Commands
Initializes the Terraform working directory and downloads provider plugins.
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. All Terraform commands should now work.
Shows the execution plan to create the defined resources without applying changes.
Terminal
terraform plan
Expected OutputExpected
Refreshing Terraform state in-memory prior to plan... 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.
Applies the planned changes to create the resources without asking for confirmation.
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. Outputs: instance_id = "i-0abcd1234efgh5678"
→
-auto-approve - Skips interactive approval before applying changes
Displays the value of the output variable 'instance_id' after deployment.
Terminal
terraform output instance_id
Expected OutputExpected
i-0abcd1234efgh5678
Key Concept

If you remember nothing else from this pattern, remember: Terraform blocks group related settings to define infrastructure clearly and logically.

Common Mistakes
Forgetting to include required arguments inside a resource block.
Terraform will fail to plan or apply because essential information is missing.
Always check the resource documentation and include all required arguments inside the resource block.
Using incorrect block names or nesting blocks improperly.
Terraform syntax errors occur and the configuration won't load.
Use official Terraform documentation to verify block names and structure; blocks should not be nested unless specified.
Not running 'terraform init' before other commands.
Terraform cannot download providers or initialize the working directory, causing errors.
Always run 'terraform init' first after creating or changing configuration files.
Summary
Use blocks like terraform, provider, resource, variable, and output to organize your infrastructure code.
Run 'terraform init' to prepare your working directory and download providers.
Use 'terraform plan' to preview changes and 'terraform apply' to create resources.
Use 'terraform output' to see values from your deployed infrastructure.

Practice

(1/5)
1. What is the main purpose of a block in Terraform configuration?
easy
A. To store secret values securely
B. To write comments in the code
C. To execute commands on the cloud provider
D. To group related settings and resources together

Solution

  1. Step 1: Understand Terraform block role

    Blocks in Terraform group related configuration settings and resources logically.
  2. Step 2: Differentiate from other uses

    Blocks are not for comments, commands, or secrets but for organizing configuration.
  3. Final Answer:

    To group related settings and resources together -> Option D
  4. Quick Check:

    Blocks = Group related settings [OK]
Hint: Blocks group settings inside braces {} [OK]
Common Mistakes:
  • Thinking blocks run commands
  • Confusing blocks with comments
  • Assuming blocks store secrets
2. Which of the following is the correct syntax to start a Terraform block named resource with label aws_instance and name web?
easy
A. resource aws_instance web {
B. resource "aws_instance" "web" {
C. resource: aws_instance: web {
D. resource(aws_instance, web) {

Solution

  1. Step 1: Recall Terraform block syntax

    Terraform blocks start with block type, then labels in quotes, then braces.
  2. Step 2: Match correct syntax

    resource "aws_instance" "web" { uses quotes and braces correctly: resource "aws_instance" "web" {
  3. Final Answer:

    resource "aws_instance" "web" { -> Option B
  4. Quick Check:

    Block type + "label" + "name" + braces [OK]
Hint: Use quotes around labels and braces to start block [OK]
Common Mistakes:
  • Omitting quotes around labels
  • Using colons or commas instead of spaces
  • Using parentheses instead of braces
3. Given this Terraform block snippet:
resource "aws_s3_bucket" "mybucket" {
  bucket = "my-bucket-name"
  acl    = "private"
}

What does this block define?
medium
A. A provider configuration for AWS S3
B. A variable named mybucket for S3 bucket name
C. An AWS S3 bucket named mybucket with private access
D. A local file named mybucket with private permissions

Solution

  1. Step 1: Identify block type and labels

    The block is a resource of type aws_s3_bucket with name mybucket.
  2. Step 2: Understand block arguments

    Arguments set bucket name and access control list (acl) to private.
  3. Final Answer:

    An AWS S3 bucket named mybucket with private access -> Option C
  4. Quick Check:

    resource block creates AWS S3 bucket [OK]
Hint: resource blocks define cloud resources with settings [OK]
Common Mistakes:
  • Confusing resource with variable
  • Thinking acl is file permission
  • Assuming provider config instead of resource
4. Which of the following Terraform blocks has a syntax error?
medium
A. resource "aws_instance" "web" ami = "ami-123456" instance_type = "t2.micro" }
B. provider "aws" { region = "us-east-1" }
C. variable "region" { default = "us-west-2" }
D. output "instance_ip" { value = aws_instance.web.public_ip }

Solution

  1. Step 1: Check block opening and braces

    resource "aws_instance" "web" ami = "ami-123456" instance_type = "t2.micro" } misses the opening brace '{' after the block header.
  2. Step 2: Confirm other blocks are correct

    Options B, C, and D have proper braces and syntax.
  3. Final Answer:

    Missing opening brace in resource block -> Option A
  4. Quick Check:

    Blocks need opening and closing braces [OK]
Hint: Always check for matching braces after block header [OK]
Common Mistakes:
  • Forgetting opening or closing braces
  • Misplacing block labels without quotes
  • Incorrect indentation causing confusion
5. You want to create a Terraform configuration that defines a resource block for an AWS EC2 instance named app_server and inside it, add a nested tags block with key-value pairs. Which is the correct way to structure this?
hard
A. resource "aws_instance" "app_server" { ami = "ami-abc123" instance_type = "t3.micro" tags = { Name = "AppServer" Environment = "Production" } }
B. resource "aws_instance" "app_server" { ami = "ami-abc123" instance_type = "t3.micro" tags { Name = "AppServer" Environment = "Production" } }
C. resource "aws_instance" "app_server" { ami = "ami-abc123" instance_type = "t3.micro" tags: { Name = "AppServer" Environment = "Production" } }
D. resource "aws_instance" "app_server" { ami = "ami-abc123" instance_type = "t3.micro" tags = [ Name = "AppServer", Environment = "Production" ] }

Solution

  1. Step 1: Understand tags argument type

    In Terraform, tags are a map (key-value pairs) assigned with = and braces { }.
  2. Step 2: Identify correct syntax for nested map

    resource "aws_instance" "app_server" { ami = "ami-abc123" instance_type = "t3.micro" tags = { Name = "AppServer" Environment = "Production" } } correctly uses tags = { ... } with key-value pairs inside.
  3. Step 3: Check other options

    resource "aws_instance" "app_server" { ami = "ami-abc123" instance_type = "t3.micro" tags { Name = "AppServer" Environment = "Production" } } wrongly uses a nested block for tags (not supported). resource "aws_instance" "app_server" { ami = "ami-abc123" instance_type = "t3.micro" tags: { Name = "AppServer" Environment = "Production" } } uses colon instead of =. resource "aws_instance" "app_server" { ami = "ami-abc123" instance_type = "t3.micro" tags = [ Name = "AppServer", Environment = "Production" ] } uses list syntax which is invalid for tags.
  4. Final Answer:

    Use tags = { ... } map syntax inside resource block -> Option A
  5. Quick Check:

    Tags = map with = and braces [OK]
Hint: Use tags = { key = value } map syntax, not nested block [OK]
Common Mistakes:
  • Using nested block instead of map for tags
  • Using colon instead of equals sign
  • Using list syntax for key-value pairs