Bird
Raised Fist0
Terraformcloud~20 mins

Why provisioners run scripts on resources in Terraform - Challenge Your Understanding

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Terraform Provisioner Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why do Terraform provisioners run scripts on resources?

Terraform provisioners allow you to run scripts or commands on a resource after it is created. What is the main reason for using provisioners in Terraform?

ATo configure or customize a resource immediately after creation when Terraform cannot do it natively.
BTo replace Terraform's built-in resource creation logic with custom scripts.
CTo permanently store state information outside of Terraform's state file.
DTo automatically scale resources based on load without manual intervention.
Attempts:
2 left
💡 Hint

Think about what Terraform can and cannot do by itself when creating resources.

❓ Architecture
intermediate
2:00remaining
When should you avoid using Terraform provisioners?

Which scenario is NOT a good reason to use Terraform provisioners?

AWhen the resource supports native Terraform configuration options.
BWhen you need to run a one-time setup script after resource creation.
CWhen you want to install software on a virtual machine after it launches.
DWhen you need to execute commands that Terraform cannot perform directly.
Attempts:
2 left
💡 Hint

Consider if Terraform already supports configuring the resource without extra scripts.

❓ service_behavior
advanced
2:00remaining
What happens if a Terraform provisioner script fails during apply?

Consider a Terraform configuration with a local-exec provisioner that runs a script on a resource. What is the behavior if the script returns a non-zero exit code?

ATerraform ignores the error and continues applying other resources.
BTerraform apply fails and marks the resource as tainted.
CTerraform marks the resource as tainted but continues the apply.
DTerraform retries the script automatically until it succeeds.
Attempts:
2 left
💡 Hint

Think about how Terraform treats errors during resource provisioning.

❓ security
advanced
2:00remaining
What is a security risk when using Terraform provisioners to run scripts on resources?

Running scripts on resources with provisioners can introduce security risks. Which of the following is a common risk?

AProvisioners prevent unauthorized access by default through secure tunnels.
BTerraform automatically encrypts all provisioner scripts, preventing exposure.
CProvisioners isolate scripts from the resource, so no risk exists.
DEmbedding sensitive data like passwords directly in provisioner scripts or Terraform files.
Attempts:
2 left
💡 Hint

Consider how sensitive information is handled in scripts and Terraform configurations.

✅ Best Practice
expert
3:00remaining
What is the recommended best practice regarding the use of Terraform provisioners?

Which statement best describes the recommended best practice for using Terraform provisioners?

AAvoid provisioners completely and rely only on manual post-deployment scripts.
BUse provisioners extensively to handle all configuration tasks for resources.
CUse provisioners only as a last resort when native Terraform resource features cannot achieve the desired configuration.
DUse provisioners to replace Terraform modules for better flexibility.
Attempts:
2 left
💡 Hint

Think about maintainability and reliability of infrastructure code.

Practice

(1/5)
1. Why do Terraform provisioners run scripts on resources?
easy
A. To backup resource data before creation
B. To delete resources automatically
C. To configure resources after they are created
D. To monitor resource usage continuously

Solution

  1. Step 1: Understand provisioners' purpose

    Provisioners run scripts to configure resources after Terraform creates them.
  2. Step 2: Identify correct use case

    They help install software or run commands Terraform cannot manage directly.
  3. Final Answer:

    To configure resources after they are created -> Option C
  4. Quick Check:

    Provisioners configure resources post-creation = D [OK]
Hint: Provisioners run scripts after resource creation [OK]
Common Mistakes:
  • Thinking provisioners delete resources
  • Assuming provisioners monitor resources
  • Believing provisioners backup data
2. Which of the following is the correct syntax to use a local-exec provisioner in Terraform?
easy
A. provisioner "local-exec" { command = "echo Hello" }
B. provisioner local_exec { run = "echo Hello" }
C. provisioner "local-exec" (command: "echo Hello")
D. provisioner local-exec { execute = "echo Hello" }

Solution

  1. Step 1: Recall correct provisioner block syntax

    Terraform uses provisioner blocks with type in quotes and a command attribute.
  2. Step 2: Match syntax to options

    provisioner "local-exec" { command = "echo Hello" } matches the correct syntax: provisioner "local-exec" { command = "echo Hello" }.
  3. Final Answer:

    provisioner "local-exec" { command = "echo Hello" } -> Option A
  4. Quick Check:

    Correct provisioner syntax uses quotes and command attribute = A [OK]
Hint: Use quotes and command attribute for local-exec provisioner [OK]
Common Mistakes:
  • Omitting quotes around provisioner type
  • Using wrong attribute names like run or execute
  • Using parentheses instead of braces
3. Given this Terraform snippet, what will happen when the resource is created?
resource "null_resource" "example" {
  provisioner "local-exec" {
    command = "echo Resource created"
  }
}
medium
A. Terraform will fail because null_resource cannot have provisioners
B. Terraform will print 'Resource created' after creating the resource
C. Terraform will create the resource but not run the command
D. Terraform will run the command before creating the resource

Solution

  1. Step 1: Understand null_resource and local-exec provisioner

    null_resource supports provisioners; local-exec runs commands on the machine running Terraform after resource creation.
  2. Step 2: Analyze command execution timing

    The command 'echo Resource created' runs after the resource is created, printing the message.
  3. Final Answer:

    Terraform will print 'Resource created' after creating the resource -> Option B
  4. Quick Check:

    local-exec runs command post-creation = A [OK]
Hint: local-exec runs commands after resource creation [OK]
Common Mistakes:
  • Thinking null_resource disallows provisioners
  • Assuming commands run before resource creation
  • Believing commands won't run at all
4. You wrote this provisioner block but Terraform shows an error:
provisioner "remote-exec" {
  command = "sudo apt-get update"
}
What is the likely cause of the error?
medium
A. Provisioners cannot run sudo commands
B. The command attribute is invalid for remote-exec
C. remote-exec provisioner requires a local script file
D. Missing connection block to specify how to connect to the resource

Solution

  1. Step 1: Check remote-exec requirements

    remote-exec provisioner needs a connection block to connect to the resource (SSH or WinRM).
  2. Step 2: Identify missing connection block

    The snippet lacks a connection block, causing Terraform to error because it cannot connect to run the command.
  3. Final Answer:

    Missing connection block to specify how to connect to the resource -> Option D
  4. Quick Check:

    remote-exec needs connection block = C [OK]
Hint: Always add connection block for remote-exec provisioners [OK]
Common Mistakes:
  • Assuming command attribute is invalid
  • Thinking sudo commands are disallowed
  • Believing remote-exec runs local scripts only
5. You want to install software on a new VM after Terraform creates it. Which approach best uses provisioners to do this reliably?
hard
A. Use a remote-exec provisioner with a connection block to run installation commands after creation
B. Use a local-exec provisioner to run installation commands on your local machine
C. Run installation commands manually after Terraform finishes
D. Use a provisioner without connection block to run commands remotely

Solution

  1. Step 1: Identify provisioner type for remote VM configuration

    remote-exec provisioner runs commands on the remote VM after creation using a connection block.
  2. Step 2: Evaluate options for reliability

    Using remote-exec with connection block automates installation reliably; local-exec runs locally, manual runs are error-prone, and missing connection causes failure.
  3. Final Answer:

    Use a remote-exec provisioner with a connection block to run installation commands after creation -> Option A
  4. Quick Check:

    remote-exec + connection block = reliable remote config [OK]
Hint: Use remote-exec with connection block for remote installs [OK]
Common Mistakes:
  • Using local-exec for remote installs
  • Skipping connection block for remote-exec
  • Relying on manual post-creation steps
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard