Understanding Data Source vs Resource in Terraform
📖 Scenario: You are setting up cloud infrastructure using Terraform. You need to understand the difference between data sources and resources in Terraform to manage your infrastructure effectively.
🎯 Goal: Build a simple Terraform configuration that uses a data block to read existing information and a resource block to create new infrastructure.
📋 What You'll Learn
Create a data source block to fetch existing AWS AMI information
Create a resource block to launch an AWS EC2 instance using the fetched AMI
Use exact block names and attributes as specified
Follow Terraform best practices for configuration
💡 Why This Matters
🌍 Real World
Terraform data sources let you read existing cloud information, like AMIs, without creating them. Resources let you create and manage cloud infrastructure, like EC2 instances.
💼 Career
Understanding data sources vs resources is essential for cloud engineers and DevOps professionals to write efficient and reusable Terraform configurations.
Progress0 / 4 steps
1
Create a data source to fetch the latest Amazon Linux 2 AMI
Write a Terraform data block named aws_ami with the name amazon_linux that fetches the latest Amazon Linux 2 AMI using the filter name = "amzn2-ami-hvm-*-x86_64-gp2" and owners set to ["amazon"].
Terraform
Hint
Use a data block with type aws_ami and name amazon_linux. Set most_recent to true. Use a filter block with name and values. Set owners to ["amazon"].
2
Create a resource to launch an EC2 instance using the fetched AMI
Add a Terraform resource block named aws_instance with the name example. Use the AMI ID from the data source data.aws_ami.amazon_linux.id and set the instance type to t2.micro.
Terraform
Hint
Use a resource block with type aws_instance and name example. Set ami to data.aws_ami.amazon_linux.id and instance_type to t2.micro.
3
Add a tag to the EC2 instance resource
Inside the aws_instance.example resource block, add a tags attribute with a map containing Name = "ExampleInstance".
Terraform
Hint
Add a tags block inside the resource with Name = "ExampleInstance".
4
Add provider configuration for AWS
At the top of the file, add a provider block for aws with the region set to us-east-1.
Terraform
Hint
Add a provider block for aws with region = "us-east-1" at the top of the file.
Practice
(1/5)
1. What is the main difference between a resource and a data source in Terraform?
easy
A. Resources and data sources both create infrastructure but in different clouds.
B. Resources create or manage infrastructure; data sources only read existing information.
C. Resources only read existing infrastructure; data sources create new infrastructure.
D. Resources are temporary; data sources are permanent.
Solution
Step 1: Understand resource role
Resources in Terraform are used to create or manage cloud infrastructure components.
Step 2: Understand data source role
Data sources are used to read information about existing infrastructure without changing it.
Final Answer:
Resources create or manage infrastructure; data sources only read existing information. -> Option B
Quick Check:
Resource = create/manage, Data source = read [OK]
Hint: Resources build; data sources just look up info [OK]
Common Mistakes:
Thinking data sources create resources
Confusing resource and data source roles
Assuming both modify infrastructure
Believing data sources are temporary
2. Which of the following is the correct syntax to declare a data source in Terraform?
easy
A. data "aws_ami" "example" {}
B. resource "aws_instance" "example" {}
C. source "aws_vpc" "example" {}
D. resource "data_aws_subnet" "example" {}
Solution
Step 1: Identify data source syntax
Data sources use the keyword data followed by the type and name.
Step 2: Check options
data "aws_ami" "example" {} uses data "aws_ami" "example" {}, which is correct syntax for a data source.