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
Resource block syntax
📖 Scenario: You are setting up a simple cloud infrastructure using Terraform. You want to create a virtual machine instance on a cloud provider.
🎯 Goal: Build a Terraform configuration with a resource block that defines a virtual machine instance with specific properties.
📋 What You'll Learn
Create a resource block for an aws_instance named example
Set the ami attribute to "ami-0c55b159cbfafe1f0"
Set the instance_type attribute to "t2.micro"
Add a tags block with key Name and value ExampleInstance
💡 Why This Matters
🌍 Real World
Terraform resource blocks are the building blocks for defining cloud infrastructure in code. This skill helps automate cloud resource creation.
💼 Career
Cloud engineers and DevOps professionals use Terraform resource blocks daily to manage infrastructure efficiently and reliably.
Progress0 / 4 steps
1
Create the resource block skeleton
Write a Terraform resource block for an aws_instance named example with empty curly braces {}.
Terraform
Hint
Start with resource "aws_instance" "example" { } to define the resource block.
2
Add AMI and instance type
Inside the resource "aws_instance" "example" block, add the attribute ami set to "ami-0c55b159cbfafe1f0" and the attribute instance_type set to "t2.micro".
Terraform
Hint
Use ami = "ami-0c55b159cbfafe1f0" and instance_type = "t2.micro" inside the resource block.
3
Add tags block
Inside the resource "aws_instance" "example" block, add a tags block with a key Name and value ExampleInstance.
Terraform
Hint
Use a tags block with Name = "ExampleInstance" inside the resource block.
4
Complete the resource block
Ensure the resource "aws_instance" "example" block is properly closed with a closing curly brace } and all attributes are correctly indented.
Terraform
Hint
Make sure the resource block ends with a closing curly brace }.
Practice
(1/5)
1. What is the correct way to start a resource block in Terraform?
easy
A. resource "aws_instance" { example }
B. resource aws_instance example { }
C. resource "example" "aws_instance" { }
D. resource "aws_instance" "example" { }
Solution
Step 1: Understand resource block syntax
A resource block starts with the keyword resource, followed by the resource type in quotes, then the resource name in quotes.
Step 2: Match the correct syntax
resource "aws_instance" "example" { } correctly uses resource "aws_instance" "example" { }, which matches the required syntax.
2. Which of the following is a valid property assignment inside a Terraform resource block?
easy
A. instance_type = t2.micro
B. instance_type => "t2.micro"
C. instance_type = "t2.micro"
D. instance_type: "t2.micro"
Solution
Step 1: Identify correct property syntax
Terraform uses = to assign values, and string values must be in quotes.
Step 2: Check each option
instance_type = "t2.micro" uses instance_type = "t2.micro", which is correct. instance_type = t2.micro misses quotes, B and C use invalid assignment symbols.
Final Answer:
instance_type = "t2.micro" -> Option C
Quick Check:
Property assignment uses = and quotes for strings [OK]
Hint: Use = and quotes for string values inside resource blocks [OK]
Common Mistakes:
Forgetting quotes around string values
Using colon or arrow instead of =
Missing equal sign
3. Given this resource block snippet, what will Terraform create?
5. You want to create two AWS EC2 instances with different names but the same AMI and instance type using resource blocks. Which is the best way to write this in Terraform?