>_ IzzyLab

Hands-on task

This task is step 4 of the course “Terraform and Ansible”

Terraform: two resources in one apply

medium~15 min

In /root/tf describe two local_file resources: the first creates /opt/app/config.json with the content {"env":"prod"}, the second creates /opt/app/version.txt with the content 1.0. Run apply — both files must appear. There's no internet in the sandbox: only the hashicorp/local provider is available (it sits in a local mirror, terraform init will find it on its own). Write the config in the Editor tab (the /root/tf directory is already created) and run terraform in the terminal.

What you'll practise:
  • terraform
  • resource
  • local_file
  • string escaping
  • jsonencode

Topic breakdown

Terraform builds the dependency graph itself and decides the order of creating resources — several independent resource blocks are applied in a single pass. The tricky part of this task isn't Terraform, it's the quotes: HCL string literals are wrapped in double quotes, so JSON with inner double quotes must be either escaped with a backslash or worked around via the heredoc syntax (<<EOT ... EOT) or a JSON-generating function. That's what people trip over most often.

What's checked

  1. config.json and version.txt are created with the required content
  2. state has at least two local_file resources
  3. infrastructure matches the config (plan shows no changes)

What it looks like