Bitzu

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

Single choice

What is the main purpose of a data block?

  1. Create and manage a new infrastructure object
  2. Read information about an object without managing it
  3. Store the Terraform state file remotely
  4. 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.

Select all that apply

Which statements about the block header resource "aws_instance" "web" {} are true?

  1. "aws_instance" is the resource type defined by a provider
  2. "web" is a local name used to refer to this resource in the module
  3. "web" becomes the name tag of the instance automatically
  4. "aws_instance" can be any string you choose
  5. 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

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.

Download on the App StoreGet it on Google Play

More Terraform Associate 004 topics in this module