Terraform Associate 004 ยท Configuration building blocks
Resource block vs data block
Managed resources vs read-only data sources, block syntax, local names, when to use a data source (4a).
Key points
- resource "TYPE" "NAME" {} declares an infrastructure object that Terraform creates, updates and destroys. data "TYPE" "NAME" {} reads information about something that already exists (or is computed by a provider) and never changes it.
- Use a data source to look up values you do not manage in this configuration, such as an existing image or network.
Source: https://developer.hashicorp.com/terraform/language/block/data
Practice questions
What is the main purpose of a data block?
- Create and manage a new infrastructure object
- Read information about an object without managing it
- Store the Terraform state file remotely
- Install the provider plugin during init
Show answer
Answer: B. Read information about an object without managing it
Data sources are read-only lookups. Creating objects is the job of resource blocks; neither state storage nor provider installation is done by data blocks.
Which statements about the block header resource "aws_instance" "web" {} are true?
- "aws_instance" is the resource type defined by a provider
- "web" is a local name used to refer to this resource in the module
- "web" becomes the name tag of the instance automatically
- "aws_instance" can be any string you choose
- The two labels together are used in references such as aws_instance.web
Show answer
Answer: A. "aws_instance" is the resource type defined by a provider, B. "web" is a local name used to refer to this resource in the module, E. The two labels together are used in references such as aws_instance.web
The first label is the type chosen from the provider, the second is a local name unique per type within the module. The local name is not sent to the cloud as the real name, and the type is not free-form.
True or false: running terraform destroy will delete the real objects that your configuration only reads through data blocks.
Show answer
Answer: False
False. Data sources only read; destroy removes managed resources. The data source is simply re-read next time.
Practice 8 questions on this topic in Bitzu
Short lessons, instant explanations and spaced review of what you get wrong. Free on iOS and Android.
