Quiz Questions
-
Can a data source be used to create, update, and destroy infrastructure?
Answer: False. A data source only reads resource data and makes it available in OpenTofu.
For more details, see the official OpenTofu documentation. -
Can a data source be created using the
datablock?
Answer: True. Data sources are defined with adatablock, analogous toresourceblocks.
Hands-On: Local File Data Source
In the directory/root/OpenTofu/project/lexcorp, update main.tf to:
- Read the contents of
/etc/os-releasevia thelocal_filedata source. - Output that content as an OpenTofu output variable.
Incorrect Configuration
- The block keyword must be
data, notdatasource. - The output reference requires the data source name (
os).
Corrected Configuration
Apply the Configuration
AWS Data Sources
Next, we’ll work with AWS data sources. Below is a quick reference table for the examples covered.| AWS Data Source | Required Argument | Description |
|---|---|---|
| aws_ebs_volume | volume_id | Fetches the EBS volume ID |
| aws_s3_bucket | bucket | Specifies the existing bucket name |
EBS Volume
Openebs.tf to find a data source block for an AWS EBS volume:
Answer:
volume_id
S3 Bucket
Ins3.tf, you’ll see a data source intended to read an existing S3 bucket:
The argument
bucket_name is invalid. The correct argument is bucket.That completes this lesson on OpenTofu data sources. In upcoming modules, you’ll get more hands-on practice with AWS data sources and resources. Happy building!