Skip to content

004 · Domain 3: Core Terraform workflow · 13% of the exam

Task 3.2: Initialize a Terraform working directory

What terraform init does: backend set-up and state migration, module installation, provider installation and the lock file, the .terraform directory, and the options that change each step.

Study it

  • terraform init: backends, modules, providers and the .terraform directory

    Lesson coming

Sample questions

Try each one before opening the answer. Every option is explained, with the HashiCorp documentation page that proves it.

Question 1 · choose 1

A team copies a configuration as the starting point for a new project. Its backend block still points at the old project's state, so the engineer edits the bucket name. terraform init now asks whether to copy the old state to the new location, which the engineer does not want. Which option starts with the new backend and leaves the old state alone?

  1. Aterraform init -migrate-state
  2. Bterraform init -reconfigure
  3. Cterraform init -force-copy
  4. Dterraform init -get=false
Show the answer and why
  • Aterraform init -migrate-state

    Incorrect

    -migrate-state copies the existing state to the new backend, the opposite of what is wanted.

  • Bterraform init -reconfigure

    Correct

    -reconfigure disregards the existing backend configuration and prevents migration of any existing state.

  • Cterraform init -force-copy

    Incorrect

    -force-copy answers yes to the migration prompts and implies -migrate-state.

  • Dterraform init -get=false

    Incorrect

    That skips child module installation and does not answer the migration question.

After a backend change, init needs either -migrate-state (bring the state along) or -reconfigure (start clean with the new settings).

Question 2 · choose 1

An engineer is unsure whether a working directory is fully initialized after pulling teammates' changes. Is it safe to run terraform init again?

  1. ANo; running it twice re-creates the infrastructure
  2. BYes; init is safe to repeat and never deletes configuration or state
  3. CNo; a second run deletes the .terraform directory and the local state file with it
  4. DYes, but only after running terraform destroy
Show the answer and why
  • ANo; running it twice re-creates the infrastructure

    Incorrect

    init does not plan or apply anything.

  • BYes; init is safe to repeat and never deletes configuration or state

    Correct

    init is always safe to run multiple times to bring the working directory up to date; it will never delete existing configuration or state.

  • CNo; a second run deletes the .terraform directory and the local state file with it

    Incorrect

    init never deletes existing configuration or state.

  • DYes, but only after running terraform destroy

    Incorrect

    Re-running init needs no destroy.

When in doubt, run init: it only brings the directory up to date.

Question 3 · choose 2

Which two tasks does terraform init perform when a configuration is initialized for the first time? (Choose TWO.)

  1. AIt installs the provider plugins the configuration requires
  2. BIt initializes the configured backend
  3. CIt creates the resources declared in the configuration
  4. DIt formats the configuration files
  5. EIt shows a plan of pending changes
Show the answer and why
  • AIt installs the provider plugins the configuration requires

    Correct

    Init finds and installs the providers that the configuration and its modules require.

  • BIt initializes the configured backend

    Correct

    Init consults the backend configuration and initializes that backend.

  • CIt creates the resources declared in the configuration

    Incorrect

    Creating resources is the job of apply.

  • DIt formats the configuration files

    Incorrect

    Formatting is done by fmt.

  • EIt shows a plan of pending changes

    Incorrect

    Plans are produced by plan or apply.

init has three jobs: backend, child modules and providers; it never touches infrastructure.

Question 4 · choose 1

Which change to a configuration requires running terraform init again before the next plan?

  1. AChanging an instance type argument in a resource block
  2. BAdding a new output block that refers to an existing resource
  3. CChanging a provider version constraint in required_providers
  4. DChanging a variable's default value
Show the answer and why
  • AChanging an instance type argument in a resource block

    Incorrect

    Ordinary argument changes need only a new plan.

  • BAdding a new output block that refers to an existing resource

    Incorrect

    Outputs need no reinitialization.

  • CChanging a provider version constraint in required_providers

    Correct

    Changes to provider requirements and to backend settings require running init again.

  • DChanging a variable's default value

    Incorrect

    Defaults are read at plan time.

Anything that changes what init installs or connects to means running init again.

Question 5 · choose 1

A provider developer has built a local copy of a provider and wants one working directory to install providers only from the folder holding that build, just this once, without changing the CLI configuration. Which init option fits?

  1. A-plugin-dir=PATH
  2. B-from-module=PATH
  3. C-backend-config=PATH
  4. D-get=false
Show the answer and why
  • A-plugin-dir=PATH

    Correct

    -plugin-dir makes init read plugins only from the given directory, as a one-time override such as testing a local provider build.

  • B-from-module=PATH

    Incorrect

    -from-module copies a module into an empty directory; it does not choose where providers come from.

  • C-backend-config=PATH

    Incorrect

    That supplies backend settings.

  • D-get=false

    Incorrect

    That skips child module installation; providers are still installed normally.

For routine mirrors use the CLI configuration; -plugin-dir is the one-off override.

Practise domain 3 →Practise all domains →