In this post we are going to cover Terraform fundamentals and the core workflow. These are 7 real Terraform interview questions for data engineers, asked the way they come up on a real project, and the last one is a scenario: the team wants to start using Terraform for a new data platform next sprint, and you set it up on day one.
Terraform lets us describe the platform as code. We write what we want, Terraform shows a plan, and nothing changes until someone approves it. Interviewers start here because everything else, state, modules, pipelines, builds on this workflow. If you can explain init, plan and apply clearly, and read a plan before you approve it, the rest of the interview gets much easier.
Part 1 of the Real Time Terraform Interview Questions and Answers series on TechBrothersIT.
- The short answer
- Let's set up the project
- Question 1: Your manager asks why the team should use Terraform instead of creating resources in the Azure portal. What do you say?
- Question 2: A new teammate asks what init, plan and apply actually do. How do you explain the workflow?
- Question 3: Someone asks what a provider is and why the team pins provider versions. How do you answer?
- Question 4: The plan says one resource will be replaced, not updated. How do you read a plan before you approve it?
- Question 5: A teammate asks how Terraform knows the order to create things. How do dependencies and the graph work?
- Question 6: Your manager asks what happens if someone runs terraform destroy by mistake. How do you protect against it?
- Scenario 7: The team wants to start using Terraform for a new data platform next sprint. What is your setup on day one?
- The full script
The short answer
Terraform turns the platform into reviewed code: init sets up the backend and installs providers, plan shows the exact change, apply makes it. Pin providers and commit the lock file, read every plan for replacements, let references build the dependency graph, and protect key resources with prevent_destroy and Azure-side locks.
Let's set up the project
We use a small Terraform 1.17 project, the current release. For plan and apply we use local resources: terraform_data stands in for the resource group and the role assignment, and local_file stands in for the storage account, the containers and a pipeline. So every plan and apply is real and nothing is created in Azure. The azure/ folder holds real azurerm 5.11 code, and we check it with terraform validate.
terraform {
required_version = "~> 1.17"
required_providers {
local = {
source = "hashicorp/local"
version = "~> 2.9"
}
}
}# Local stand-ins: the plan is real, nothing is created in Azure
locals {
storage_name = "stsalesdatadev"
tags = { env = "dev", owner = "sales-data" }
}
resource "terraform_data" "resource_group" {
input = { name = "rg-sales-data-dev", tags = local.tags }
}resource "local_file" "storage_account" {
filename = "${path.module}/${local.storage_name}.json"
content = jsonencode({
resource_group = terraform_data.resource_group.input.name
})
}
resource "local_file" "container" {
for_each = toset(["landing", "raw", "curated"])
filename = "${path.module}/lake/${each.key}.keep"
content = local_file.storage_account.id
}resource "terraform_data" "role_assignment" {
# pipeline identity: Storage Blob Data Contributor on the lake
triggers_replace = local_file.storage_account.id
}
resource "local_file" "pipeline" {
filename = "${path.module}/pipeline/nightly.json"
content = jsonencode({ source = "landing", target = "curated" })
depends_on = [terraform_data.role_assignment] # needs access first
}terraform {
required_version = "~> 1.17"
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~> 5.11"
}
}
backend "azurerm" {
use_oidc = true
use_azuread_auth = true
storage_account_name = "stsalestfstate"
container_name = "tfstate"
key = "sales-data/dev.terraform.tfstate"
}
}provider "azurerm" {
features {}
# subscription, tenant and client IDs: ARM_* variables in the pipeline
}locals {
tags = { env = "dev", owner = "sales-data" }
}
resource "azurerm_resource_group" "sales" {
name = "rg-sales-data-dev"
location = "eastus2"
tags = local.tags
}
resource "azurerm_storage_account" "lake" {
name = "stsalesdatadev"
resource_group_name = azurerm_resource_group.sales.name
location = azurerm_resource_group.sales.location
account_tier = "Standard"
account_replication_type = "LRS"
is_hns_enabled = true
shared_access_key_enabled = false
min_tls_version = "TLS1_2"
tags = local.tags
}resource "azurerm_storage_container" "layer" {
for_each = toset(["landing", "raw", "curated"])
name = each.key
storage_account_id = azurerm_storage_account.lake.id
}mock_provider "azurerm" {}
run "lake_is_locked_down" {
command = plan
assert {
condition = azurerm_storage_account.lake.is_hns_enabled
error_message = "The lake needs the hierarchical namespace."
}
assert {
condition = !azurerm_storage_account.lake.shared_access_key_enabled
error_message = "Shared keys must be off: Entra ID only."
}
}Question 1: Your manager asks why the team should use Terraform instead of creating resources in the Azure portal. What do you say?
The portal is fine for learning, but it doesn't scale. With Terraform, the platform is code: we declare what we want, Terraform shows a plan, and nothing changes until someone approves it.
Let's compare the two ways of working.
| Portal clicks | Terraform | |
|---|---|---|
| Repeatable | click again for every environment | same code for dev, test and prod |
| Reviewed | no review before the change | pull request with the plan |
| History | Activity Log: who and what, never why | Git: who, what and why |
| Knows reality | no record of what it should be | state + plan show the difference |
Clicks aren't repeatable or reviewed. With code, the same files build dev, test and prod, every change goes through a pull request, and Git shows who changed what and why. The state maps the code to the real resources, so each plan shows exactly what will change.
Here is the dev lake as code in the azure/ folder: a resource group, an ADLS Gen2 storage account with the hierarchical namespace on and shared keys off, and three containers. Validate needs the provider installed, so we run terraform -chdir=azure init -backend=false first (we look at that output in question 3). Then we run terraform -chdir=azure validate. It checks the code against the azurerm schema, without calling Azure.
$ terraform -chdir=azure validate
Success! The configuration is valid.It prints Success! The configuration is valid.
And it isn't Azure only. Providers cover Databricks, GitHub and many more, so one workflow covers the whole data platform.
People get read access to production in the portal. If someone fixes something by hand at night, it goes back into code the next day.
Question 2: A new teammate asks what init, plan and apply actually do. How do you explain the workflow?
Init prepares the folder, plan shows what would change, and apply makes the change.
terraform init- backend, providers, modules, lock fileterraform plan- code vs state vs real: what would changeterraform apply- makes the change, updates the state
First we run terraform init. It sets up the backend, installs the providers and modules, and writes the lock file.
$ terraform init
Initializing the backend...
Initializing provider plugins...
- terraform.io/builtin/terraform is built in to Terraform
- Finding hashicorp/local versions matching "~> 2.9"...
- Installing hashicorp/local v2.9.1...
Terraform has created a lock file .terraform.lock.hcl to record the provider
selections it made above. Include this file in your version control repository
so that Terraform can guarantee to make the same selections by default when
you run "terraform init" in the future.
Terraform has been successfully initialized!
You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.
If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.It finds the versions of the local provider that match ~> 2.9 and installs v2.9.1.
Then we run terraform plan -out=tfplan. Plan compares the code with the state and the real resources, and prints what it would do. Nothing changes yet.
$ terraform plan -out=tfplan
Terraform used the selected providers to generate the following execution
plan. Resource actions are indicated with the following symbols:
+ create
Terraform will perform the following actions:
# local_file.container["curated"] will be created
+ resource "local_file" "container" {
+ content = (known after apply)
+ content_base64sha256 = (known after apply)
+ content_base64sha512 = (known after apply)
+ content_md5 = (known after apply)
+ content_sha1 = (known after apply)
+ content_sha256 = (known after apply)
+ content_sha512 = (known after apply)
+ directory_permission = "0777"
+ file_permission = "0777"
+ filename = "./lake/curated.keep"
+ id = (known after apply)
}
# local_file.container["landing"] will be created
+ resource "local_file" "container" {
+ content = (known after apply)
+ content_base64sha256 = (known after apply)
+ content_base64sha512 = (known after apply)
+ content_md5 = (known after apply)
+ content_sha1 = (known after apply)
+ content_sha256 = (known after apply)
+ content_sha512 = (known after apply)
+ directory_permission = "0777"
+ file_permission = "0777"
+ filename = "./lake/landing.keep"
+ id = (known after apply)
}
# local_file.container["raw"] will be created
+ resource "local_file" "container" {
+ content = (known after apply)
+ content_base64sha256 = (known after apply)
+ content_base64sha512 = (known after apply)
+ content_md5 = (known after apply)
+ content_sha1 = (known after apply)
+ content_sha256 = (known after apply)
+ content_sha512 = (known after apply)
+ directory_permission = "0777"
+ file_permission = "0777"
+ filename = "./lake/raw.keep"
+ id = (known after apply)
}
# local_file.pipeline will be created
+ resource "local_file" "pipeline" {
+ content = jsonencode(
{
+ source = "landing"
+ target = "curated"
}
)
+ content_base64sha256 = (known after apply)
+ content_base64sha512 = (known after apply)
+ content_md5 = (known after apply)
+ content_sha1 = (known after apply)
+ content_sha256 = (known after apply)
+ content_sha512 = (known after apply)
+ directory_permission = "0777"
+ file_permission = "0777"
+ filename = "./pipeline/nightly.json"
+ id = (known after apply)
}
# local_file.storage_account will be created
+ resource "local_file" "storage_account" {
+ content = jsonencode(
{
+ resource_group = "rg-sales-data-dev"
}
)
+ content_base64sha256 = (known after apply)
+ content_base64sha512 = (known after apply)
+ content_md5 = (known after apply)
+ content_sha1 = (known after apply)
+ content_sha256 = (known after apply)
+ content_sha512 = (known after apply)
+ directory_permission = "0777"
+ file_permission = "0777"
+ filename = "./stsalesdatadev.json"
+ id = (known after apply)
}
# terraform_data.resource_group will be created
+ resource "terraform_data" "resource_group" {
+ id = (known after apply)
+ input = {
+ name = "rg-sales-data-dev"
+ tags = {
+ env = "dev"
+ owner = "sales-data"
}
}
+ output = (known after apply)
}
# terraform_data.role_assignment will be created
+ resource "terraform_data" "role_assignment" {
+ id = (known after apply)
+ triggers_replace = (known after apply)
}
Plan: 7 to add, 0 to change, 0 to destroy.
─────────────────────────────────────────────────────────────────────────────
Saved the plan to: tfplan
To perform exactly these actions, run the following command to apply:
terraform apply "tfplan"The summary says Plan: 7 to add, 0 to change, 0 to destroy. and, because of -out, the plan is saved to the file tfplan.
Now we run terraform apply tfplan. Given a saved plan, apply runs exactly those actions, with no prompt.
$ terraform apply tfplan
terraform_data.resource_group: Creating...
terraform_data.resource_group: Creation complete after <time> [id=<uuid>]
local_file.storage_account: Creating...
local_file.storage_account: Creation complete after <time> [id=4a92724d08d3a46e482d7b8bc5ce7a3171e1ab7d]
Apply complete! Resources: 7 added, 0 changed, 0 destroyed.We get Apply complete! Resources: 7 added, 0 changed, 0 destroyed. The resource group is created first, then the storage account, then everything that depends on it. Without a plan file, apply makes a fresh plan and asks for yes.
Plan to a file, review it, and apply that same file. If the state changed in between, Terraform rejects the stale plan, so nobody applies a change that wasn't reviewed. And run init again whenever providers, modules or the backend change.
Question 3: Someone asks what a provider is and why the team pins provider versions. How do you answer?
A provider is a plugin that turns our HCL into API calls: azurerm talks to Azure Resource Manager, and the Databricks provider talks to Databricks. We declare it in required_providers, with a source and a version constraint.
Let's install azurerm in the azure/ folder with terraform -chdir=azure init -backend=false (we skip the backend because we only validate here), and then read the lock file.
$ terraform -chdir=azure init -backend=false
Initializing provider plugins...
- Finding hashicorp/azurerm versions matching "~> 5.11"...
- Installing hashicorp/azurerm v5.11.0...
Terraform has created a lock file .terraform.lock.hcl to record the provider
selections it made above. Include this file in your version control repository
so that Terraform can guarantee to make the same selections by default when
you run "terraform init" in the future.
Terraform has been successfully initialized!
You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.
If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.
$ cat azure/.terraform.lock.hcl
# This file is maintained automatically by "terraform init".
# Manual edits may be lost in future updates.
provider "registry.terraform.io/hashicorp/azurerm" {
version = "5.11.0"
constraints = "~> 5.11"
hashes = [ ... checksums ... ]
}The constraint ~> 5.11 allows 5.11 and newer, but never 6. Init picks the newest match, v5.11.0, and records the exact version in .terraform.lock.hcl. We commit that file, so every laptop and pipeline gets the same provider.
Why pin? Major versions remove things. azurerm 5 removed storage_account_name on containers. Let's paste container code copied from an azurerm 4.x example and run terraform -chdir=azure validate again.
# copied from an azurerm 4.x example
resource "azurerm_storage_container" "layer" {
for_each = toset(["landing", "raw", "curated"])
name = each.key
storage_account_name = azurerm_storage_account.lake.name
}$ terraform -chdir=azure validate
Error: Missing required argument
on containers.tf line 2, in resource "azurerm_storage_container" "layer":
2: resource "azurerm_storage_container" "layer" {
The argument "storage_account_id" is required, but no definition was found.
Error: Unsupported argument
on containers.tf line 5, in resource "azurerm_storage_container" "layer":
5: storage_account_name = azurerm_storage_account.lake.name
An argument named "storage_account_name" is not expected here.It fails with two errors: The argument "storage_account_id" is required, and An argument named "storage_account_name" is not expected here.
So upgrades are a choice, not a surprise. Here is the routine:
terraform init -upgrade- on a branch, never straight in main- Read the upgrade guide - removed and renamed arguments
- Plan in dev first - look for replacements
- Review
.terraform.lock.hcl- in the pull request required_version- pins Terraform itself
Question 4: The plan says one resource will be replaced, not updated. How do you read a plan before you approve it?
Start with the symbols at the top. A ~ means update in place: the resource stays. A -/+ means destroy, then create a replacement: a brand new resource, and in Azure the old one is gone, with its data.
Let's try it. Someone renamed the storage account to stsalesdatadev01 and changed the owner tag to sales-analytics. We run terraform plan -out=tfplan.
# Local stand-ins: the plan is real, nothing is created in Azure
locals {
storage_name = "stsalesdatadev01"
tags = { env = "dev", owner = "sales-analytics" }
}
resource "terraform_data" "resource_group" {
input = { name = "rg-sales-data-dev", tags = local.tags }
}$ terraform plan -out=tfplan
Terraform used the selected providers to generate the following execution
plan. Resource actions are indicated with the following symbols:
~ update in-place
-/+ destroy and then create replacement
Terraform will perform the following actions:
# local_file.container["curated"] must be replaced
-/+ resource "local_file" "container" {
~ content = "4a92724d08d3a46e482d7b8bc5ce7a3171e1ab7d" -> (known after apply) #
forces replacement
~ content_base64sha256 = "cwrODY6ZwAqhp/mW95+0EyrGnNGHzhEH45Mqdw3pZ0c=" -> (known after apply)
~ content_base64sha512 =
"d2b4fEMMI5wOpehEYXmvPSWo/ILdAM4vGQPgcM3CCZJ4VZVi5nklZoeC7WQ0zr5IBa4tTAjatvewK/lsmPzS7w==" ->
(known after apply)
~ content_md5 = "69a2605293b96f282c964435a220d0ed" -> (known after apply)
~ content_sha1 = "2c05ab7e073b7e52878871b4906e1bc4c0475764" -> (known after apply)
~ content_sha256 = "730ace0d8e99c00aa1a7f996f79fb4132ac69cd187ce1107e3932a770de96747" ->
(known after apply)
~ content_sha512 =
"7766f87c430c239c0ea5e8446179af3d25a8fc82dd00ce2f1903e070cdc2099278559562e67925668782ed6434cebe4
805ae2d4c08dab6f7b02bf96c98fcd2ef" -> (known after apply)
~ id = "2c05ab7e073b7e52878871b4906e1bc4c0475764" -> (known after apply)
# (3 unchanged attributes hidden)
}
# local_file.container["landing"] must be replaced
-/+ resource "local_file" "container" {
~ content = "4a92724d08d3a46e482d7b8bc5ce7a3171e1ab7d" -> (known after apply) #
forces replacement
~ content_base64sha256 = "cwrODY6ZwAqhp/mW95+0EyrGnNGHzhEH45Mqdw3pZ0c=" -> (known after apply)
~ content_base64sha512 =
"d2b4fEMMI5wOpehEYXmvPSWo/ILdAM4vGQPgcM3CCZJ4VZVi5nklZoeC7WQ0zr5IBa4tTAjatvewK/lsmPzS7w==" ->
(known after apply)
~ content_md5 = "69a2605293b96f282c964435a220d0ed" -> (known after apply)
~ content_sha1 = "2c05ab7e073b7e52878871b4906e1bc4c0475764" -> (known after apply)
~ content_sha256 = "730ace0d8e99c00aa1a7f996f79fb4132ac69cd187ce1107e3932a770de96747" ->
(known after apply)
~ content_sha512 =
"7766f87c430c239c0ea5e8446179af3d25a8fc82dd00ce2f1903e070cdc2099278559562e67925668782ed6434cebe4
805ae2d4c08dab6f7b02bf96c98fcd2ef" -> (known after apply)
~ id = "2c05ab7e073b7e52878871b4906e1bc4c0475764" -> (known after apply)
# (3 unchanged attributes hidden)
}
# local_file.container["raw"] must be replaced
-/+ resource "local_file" "container" {
~ content = "4a92724d08d3a46e482d7b8bc5ce7a3171e1ab7d" -> (known after apply) #
forces replacement
~ content_base64sha256 = "cwrODY6ZwAqhp/mW95+0EyrGnNGHzhEH45Mqdw3pZ0c=" -> (known after apply)
~ content_base64sha512 =
"d2b4fEMMI5wOpehEYXmvPSWo/ILdAM4vGQPgcM3CCZJ4VZVi5nklZoeC7WQ0zr5IBa4tTAjatvewK/lsmPzS7w==" ->
(known after apply)
~ content_md5 = "69a2605293b96f282c964435a220d0ed" -> (known after apply)
~ content_sha1 = "2c05ab7e073b7e52878871b4906e1bc4c0475764" -> (known after apply)
~ content_sha256 = "730ace0d8e99c00aa1a7f996f79fb4132ac69cd187ce1107e3932a770de96747" ->
(known after apply)
~ content_sha512 =
"7766f87c430c239c0ea5e8446179af3d25a8fc82dd00ce2f1903e070cdc2099278559562e67925668782ed6434cebe4
805ae2d4c08dab6f7b02bf96c98fcd2ef" -> (known after apply)
~ id = "2c05ab7e073b7e52878871b4906e1bc4c0475764" -> (known after apply)
# (3 unchanged attributes hidden)
}
# local_file.storage_account must be replaced
-/+ resource "local_file" "storage_account" {
~ content_base64sha256 = "nBDRbF9rs3O9ioH6cYlBAOaBJG2v33yUc5lkIM8C/Eg=" -> (known after apply)
~ content_base64sha512 =
"hpcaf/NUa/k36G76hBAElZ5uznqhvdj8zSIROeOVvl9B0tGPBBHtIDoT4F2KQt8W3LNrsPSAJFoOCCkE9ArM2w==" ->
(known after apply)
~ content_md5 = "4bbbca4c10ee1faae24f3ddf97f4a3a9" -> (known after apply)
~ content_sha1 = "4a92724d08d3a46e482d7b8bc5ce7a3171e1ab7d" -> (known after apply)
~ content_sha256 = "9c10d16c5f6bb373bd8a81fa71894100e681246dafdf7c9473996420cf02fc48" ->
(known after apply)
~ content_sha512 =
"86971a7ff3546bf937e86efa841004959e6ece7aa1bdd8fccd221139e395be5f41d2d18f0411ed203a13e05d8a42df1
6dcb36bb0f480245a0e082904f40accdb" -> (known after apply)
~ filename = "./stsalesdatadev.json" -> "./stsalesdatadev01.json" # forces replacement
~ id = "4a92724d08d3a46e482d7b8bc5ce7a3171e1ab7d" -> (known after apply)
# (3 unchanged attributes hidden)
}
# terraform_data.resource_group will be updated in-place
~ resource "terraform_data" "resource_group" {
id = "<uuid>"
~ input = {
name = "rg-sales-data-dev"
~ tags = {
env = "dev"
~ owner = "sales-data" -> "sales-analytics"
}
}
~ output = {
- name = "rg-sales-data-dev"
- tags = {
- env = "dev"
- owner = "sales-data"
}
} -> (known after apply)
}
# terraform_data.role_assignment must be replaced
-/+ resource "terraform_data" "role_assignment" {
~ id = "<uuid>" -> (known after apply)
~ triggers_replace = "4a92724d08d3a46e482d7b8bc5ce7a3171e1ab7d" -> (known after apply) # forces
replacement
}
Plan: 5 to add, 1 to change, 5 to destroy.
─────────────────────────────────────────────────────────────────────────────
Saved the plan to: tfplan
To perform exactly these actions, run the following command to apply:
terraform apply "tfplan"First, the tag. terraform_data.resource_group will be updated in-place, and the line ~ owner = "sales-data" -> "sales-analytics" shows the change. Then the account. local_file.storage_account must be replaced, and the line marked # forces replacement says why: the file name, our stand-in for the account name, which can't change in Azure.
Then read the summary: Plan: 5 to add, 1 to change, 5 to destroy. A replacement counts once in add and once in destroy. And see what else is replaced: the three containers and the role assignment, because they point at the account.
In a pipeline we don't read this by eye. We run terraform show -json tfplan and pick out the actions per resource with jq.
$ terraform show -json tfplan | jq -r '.resource_changes[] | "\(.change.actions) \(.address)"'
["delete","create"] local_file.container["curated"]
["delete","create"] local_file.container["landing"]
["delete","create"] local_file.container["raw"]
["no-op"] local_file.pipeline
["delete","create"] local_file.storage_account
["update"] terraform_data.resource_group
["delete","create"] terraform_data.role_assignmentEvery replacement shows as ["delete","create"], the tag change as ["update"], and the pipeline as ["no-op"].
Any destroy in a production plan needs a reason. With show -json, the run can stop when it finds a delete that nobody approved.
Question 5: A teammate asks how Terraform knows the order to create things. How do dependencies and the graph work?
From references. The storage account uses the resource group's name, and the containers use the account's ID. So Terraform knows: resource group first, then the account, then the containers. That's an implicit dependency.
Terraform builds a graph of every resource and walks it. Things that don't depend on each other run in parallel, 10 operations at a time by default. Destroy walks the same graph in reverse.
Let's put main.tf back to the original values and print the graph with terraform graph -format=mermaid.
# Local stand-ins: the plan is real, nothing is created in Azure
locals {
storage_name = "stsalesdatadev"
tags = { env = "dev", owner = "sales-data" }
}
resource "terraform_data" "resource_group" {
input = { name = "rg-sales-data-dev", tags = local.tags }
}$ terraform graph -format=mermaid
flowchart LR
local_file.container["local_file.container"]
local_file.pipeline["local_file.pipeline"]
local_file.storage_account["local_file.storage_account"]
terraform_data.resource_group["terraform_data.resource_group"]
terraform_data.role_assignment["terraform_data.role_assignment"]
local_file.container --> local_file.storage_account
local_file.pipeline --> terraform_data.role_assignment
local_file.storage_account --> terraform_data.resource_group
terraform_data.role_assignment --> local_file.storage_accountThe edges read like this: local_file.container --> local_file.storage_account, and local_file.storage_account --> terraform_data.resource_group.
Some dependencies are hidden. The nightly pipeline doesn't reference the role assignment, but it fails without that access. So pipeline.tf has depends_on = [terraform_data.role_assignment]. That makes the edge explicit, and the graph shows it: local_file.pipeline --> terraform_data.role_assignment.
Prefer references, and keep depends_on for hidden cases like this one. The docs call it a last resort, because it makes plans more conservative, with more values unknown until apply.
Question 6: Your manager asks what happens if someone runs terraform destroy by mistake. How do you protect against it?
First, destroy shows its plan and asks. Let's answer no with echo no | terraform destroy.
$ echo no | terraform destroy
Terraform used the selected providers to generate the following execution
plan. Resource actions are indicated with the following symbols:
- destroy
Terraform will perform the following actions:
# local_file.container["curated"] will be destroyed
- resource "local_file" "container" {
- content = "4a92724d08d3a46e482d7b8bc5ce7a3171e1ab7d" -> null
- content_base64sha256 = "cwrODY6ZwAqhp/mW95+0EyrGnNGHzhEH45Mqdw3pZ0c=" -> null
- content_base64sha512 =
"d2b4fEMMI5wOpehEYXmvPSWo/ILdAM4vGQPgcM3CCZJ4VZVi5nklZoeC7WQ0zr5IBa4tTAjatvewK/lsmPzS7w==" ->
null
- content_md5 = "69a2605293b96f282c964435a220d0ed" -> null
- content_sha1 = "2c05ab7e073b7e52878871b4906e1bc4c0475764" -> null
- content_sha256 = "730ace0d8e99c00aa1a7f996f79fb4132ac69cd187ce1107e3932a770de96747" ->
null
- content_sha512 =
"7766f87c430c239c0ea5e8446179af3d25a8fc82dd00ce2f1903e070cdc2099278559562e67925668782ed6434cebe4
805ae2d4c08dab6f7b02bf96c98fcd2ef" -> null
- directory_permission = "0777" -> null
- file_permission = "0777" -> null
- filename = "./lake/curated.keep" -> null
- id = "2c05ab7e073b7e52878871b4906e1bc4c0475764" -> null
}
# local_file.container["landing"] will be destroyed
- resource "local_file" "container" {
- content = "4a92724d08d3a46e482d7b8bc5ce7a3171e1ab7d" -> null
- content_base64sha256 = "cwrODY6ZwAqhp/mW95+0EyrGnNGHzhEH45Mqdw3pZ0c=" -> null
- content_base64sha512 =
"d2b4fEMMI5wOpehEYXmvPSWo/ILdAM4vGQPgcM3CCZJ4VZVi5nklZoeC7WQ0zr5IBa4tTAjatvewK/lsmPzS7w==" ->
null
- content_md5 = "69a2605293b96f282c964435a220d0ed" -> null
- content_sha1 = "2c05ab7e073b7e52878871b4906e1bc4c0475764" -> null
- content_sha256 = "730ace0d8e99c00aa1a7f996f79fb4132ac69cd187ce1107e3932a770de96747" ->
null
- content_sha512 =
"7766f87c430c239c0ea5e8446179af3d25a8fc82dd00ce2f1903e070cdc2099278559562e67925668782ed6434cebe4
805ae2d4c08dab6f7b02bf96c98fcd2ef" -> null
- directory_permission = "0777" -> null
- file_permission = "0777" -> null
- filename = "./lake/landing.keep" -> null
- id = "2c05ab7e073b7e52878871b4906e1bc4c0475764" -> null
}
# local_file.container["raw"] will be destroyed
- resource "local_file" "container" {
- content = "4a92724d08d3a46e482d7b8bc5ce7a3171e1ab7d" -> null
- content_base64sha256 = "cwrODY6ZwAqhp/mW95+0EyrGnNGHzhEH45Mqdw3pZ0c=" -> null
- content_base64sha512 =
"d2b4fEMMI5wOpehEYXmvPSWo/ILdAM4vGQPgcM3CCZJ4VZVi5nklZoeC7WQ0zr5IBa4tTAjatvewK/lsmPzS7w==" ->
null
- content_md5 = "69a2605293b96f282c964435a220d0ed" -> null
- content_sha1 = "2c05ab7e073b7e52878871b4906e1bc4c0475764" -> null
- content_sha256 = "730ace0d8e99c00aa1a7f996f79fb4132ac69cd187ce1107e3932a770de96747" ->
null
- content_sha512 =
"7766f87c430c239c0ea5e8446179af3d25a8fc82dd00ce2f1903e070cdc2099278559562e67925668782ed6434cebe4
805ae2d4c08dab6f7b02bf96c98fcd2ef" -> null
- directory_permission = "0777" -> null
- file_permission = "0777" -> null
- filename = "./lake/raw.keep" -> null
- id = "2c05ab7e073b7e52878871b4906e1bc4c0475764" -> null
}
# local_file.pipeline will be destroyed
- resource "local_file" "pipeline" {
- content = jsonencode(
{
- source = "landing"
- target = "curated"
}
) -> null
- content_base64sha256 = "50s1wqO5ag5jInYWuCZveXctzaalWQ+DF+uhJgnQUzI=" -> null
- content_base64sha512 =
"VTb5Ph/f6Sw4IX1+QZyN8jq0cfe9OZ623AzYqYA2gw2jlPCy95DZxEj9NGTU6L86NIJxBC1CZG+hElueBCPhxg==" ->
null
- content_md5 = "49195edbbf9b16f22a7909f27c6d4955" -> null
- content_sha1 = "9fa8a3e5f766784cfcc4ca260490e6964c53326d" -> null
- content_sha256 = "e74b35c2a3b96a0e63227616b8266f79772dcda6a5590f8317eba12609d05332" ->
null
- content_sha512 =
"5536f93e1fdfe92c38217d7e419c8df23ab471f7bd399eb6dc0cd8a98036830da394f0b2f790d9c448fd3464d4e8bf3
a348271042d42646fa1125b9e0423e1c6" -> null
- directory_permission = "0777" -> null
- file_permission = "0777" -> null
- filename = "./pipeline/nightly.json" -> null
- id = "9fa8a3e5f766784cfcc4ca260490e6964c53326d" -> null
}
# local_file.storage_account will be destroyed
- resource "local_file" "storage_account" {
- content = jsonencode(
{
- resource_group = "rg-sales-data-dev"
}
) -> null
- content_base64sha256 = "nBDRbF9rs3O9ioH6cYlBAOaBJG2v33yUc5lkIM8C/Eg=" -> null
- content_base64sha512 =
"hpcaf/NUa/k36G76hBAElZ5uznqhvdj8zSIROeOVvl9B0tGPBBHtIDoT4F2KQt8W3LNrsPSAJFoOCCkE9ArM2w==" ->
null
- content_md5 = "4bbbca4c10ee1faae24f3ddf97f4a3a9" -> null
- content_sha1 = "4a92724d08d3a46e482d7b8bc5ce7a3171e1ab7d" -> null
- content_sha256 = "9c10d16c5f6bb373bd8a81fa71894100e681246dafdf7c9473996420cf02fc48" ->
null
- content_sha512 =
"86971a7ff3546bf937e86efa841004959e6ece7aa1bdd8fccd221139e395be5f41d2d18f0411ed203a13e05d8a42df1
6dcb36bb0f480245a0e082904f40accdb" -> null
- directory_permission = "0777" -> null
- file_permission = "0777" -> null
- filename = "./stsalesdatadev.json" -> null
- id = "4a92724d08d3a46e482d7b8bc5ce7a3171e1ab7d" -> null
}
# terraform_data.resource_group will be destroyed
- resource "terraform_data" "resource_group" {
- id = "<uuid>" -> null
- input = {
- name = "rg-sales-data-dev"
- tags = {
- env = "dev"
- owner = "sales-data"
}
} -> null
- output = {
- name = "rg-sales-data-dev"
- tags = {
- env = "dev"
- owner = "sales-data"
}
} -> null
}
# terraform_data.role_assignment will be destroyed
- resource "terraform_data" "role_assignment" {
- id = "<uuid>" -> null
- triggers_replace = "4a92724d08d3a46e482d7b8bc5ce7a3171e1ab7d" -> null
}
Plan: 0 to add, 0 to change, 7 to destroy.
Do you really want to destroy all resources?
Terraform will destroy all your managed infrastructure, as shown above.
There is no undo. Only 'yes' will be accepted to confirm.
Enter a value:
Destroy cancelled.The plan says Plan: 0 to add, 0 to change, 7 to destroy., then Only 'yes' will be accepted to confirm., and we get Destroy cancelled. But a script with -auto-approve doesn't ask anyone.
So we protect key resources in code. We add prevent_destroy = true in a lifecycle block on the storage account and run terraform destroy -auto-approve.
resource "local_file" "storage_account" {
filename = "${path.module}/${local.storage_name}.json"
content = jsonencode({
resource_group = terraform_data.resource_group.input.name
})
lifecycle {
prevent_destroy = true
}
}
resource "local_file" "container" {
for_each = toset(["landing", "raw", "curated"])
filename = "${path.module}/lake/${each.key}.keep"
content = local_file.storage_account.id
}$ terraform destroy -auto-approve
Terraform used the selected providers to generate the following execution
plan. Resource actions are indicated with the following symbols:
- destroy
Terraform planned the following actions, but then encountered a problem:
# local_file.container["curated"] will be destroyed
- resource "local_file" "container" {
- content = "4a92724d08d3a46e482d7b8bc5ce7a3171e1ab7d" -> null
- content_base64sha256 = "cwrODY6ZwAqhp/mW95+0EyrGnNGHzhEH45Mqdw3pZ0c=" -> null
- content_base64sha512 =
"d2b4fEMMI5wOpehEYXmvPSWo/ILdAM4vGQPgcM3CCZJ4VZVi5nklZoeC7WQ0zr5IBa4tTAjatvewK/lsmPzS7w==" ->
null
- content_md5 = "69a2605293b96f282c964435a220d0ed" -> null
- content_sha1 = "2c05ab7e073b7e52878871b4906e1bc4c0475764" -> null
- content_sha256 = "730ace0d8e99c00aa1a7f996f79fb4132ac69cd187ce1107e3932a770de96747" ->
null
- content_sha512 =
"7766f87c430c239c0ea5e8446179af3d25a8fc82dd00ce2f1903e070cdc2099278559562e67925668782ed6434cebe4
805ae2d4c08dab6f7b02bf96c98fcd2ef" -> null
- directory_permission = "0777" -> null
- file_permission = "0777" -> null
- filename = "./lake/curated.keep" -> null
- id = "2c05ab7e073b7e52878871b4906e1bc4c0475764" -> null
}
# local_file.container["landing"] will be destroyed
- resource "local_file" "container" {
- content = "4a92724d08d3a46e482d7b8bc5ce7a3171e1ab7d" -> null
- content_base64sha256 = "cwrODY6ZwAqhp/mW95+0EyrGnNGHzhEH45Mqdw3pZ0c=" -> null
- content_base64sha512 =
"d2b4fEMMI5wOpehEYXmvPSWo/ILdAM4vGQPgcM3CCZJ4VZVi5nklZoeC7WQ0zr5IBa4tTAjatvewK/lsmPzS7w==" ->
null
- content_md5 = "69a2605293b96f282c964435a220d0ed" -> null
- content_sha1 = "2c05ab7e073b7e52878871b4906e1bc4c0475764" -> null
- content_sha256 = "730ace0d8e99c00aa1a7f996f79fb4132ac69cd187ce1107e3932a770de96747" ->
null
- content_sha512 =
"7766f87c430c239c0ea5e8446179af3d25a8fc82dd00ce2f1903e070cdc2099278559562e67925668782ed6434cebe4
805ae2d4c08dab6f7b02bf96c98fcd2ef" -> null
- directory_permission = "0777" -> null
- file_permission = "0777" -> null
- filename = "./lake/landing.keep" -> null
- id = "2c05ab7e073b7e52878871b4906e1bc4c0475764" -> null
}
# local_file.container["raw"] will be destroyed
- resource "local_file" "container" {
- content = "4a92724d08d3a46e482d7b8bc5ce7a3171e1ab7d" -> null
- content_base64sha256 = "cwrODY6ZwAqhp/mW95+0EyrGnNGHzhEH45Mqdw3pZ0c=" -> null
- content_base64sha512 =
"d2b4fEMMI5wOpehEYXmvPSWo/ILdAM4vGQPgcM3CCZJ4VZVi5nklZoeC7WQ0zr5IBa4tTAjatvewK/lsmPzS7w==" ->
null
- content_md5 = "69a2605293b96f282c964435a220d0ed" -> null
- content_sha1 = "2c05ab7e073b7e52878871b4906e1bc4c0475764" -> null
- content_sha256 = "730ace0d8e99c00aa1a7f996f79fb4132ac69cd187ce1107e3932a770de96747" ->
null
- content_sha512 =
"7766f87c430c239c0ea5e8446179af3d25a8fc82dd00ce2f1903e070cdc2099278559562e67925668782ed6434cebe4
805ae2d4c08dab6f7b02bf96c98fcd2ef" -> null
- directory_permission = "0777" -> null
- file_permission = "0777" -> null
- filename = "./lake/raw.keep" -> null
- id = "2c05ab7e073b7e52878871b4906e1bc4c0475764" -> null
}
# local_file.pipeline will be destroyed
- resource "local_file" "pipeline" {
- content = jsonencode(
{
- source = "landing"
- target = "curated"
}
) -> null
- content_base64sha256 = "50s1wqO5ag5jInYWuCZveXctzaalWQ+DF+uhJgnQUzI=" -> null
- content_base64sha512 =
"VTb5Ph/f6Sw4IX1+QZyN8jq0cfe9OZ623AzYqYA2gw2jlPCy95DZxEj9NGTU6L86NIJxBC1CZG+hElueBCPhxg==" ->
null
- content_md5 = "49195edbbf9b16f22a7909f27c6d4955" -> null
- content_sha1 = "9fa8a3e5f766784cfcc4ca260490e6964c53326d" -> null
- content_sha256 = "e74b35c2a3b96a0e63227616b8266f79772dcda6a5590f8317eba12609d05332" ->
null
- content_sha512 =
"5536f93e1fdfe92c38217d7e419c8df23ab471f7bd399eb6dc0cd8a98036830da394f0b2f790d9c448fd3464d4e8bf3
a348271042d42646fa1125b9e0423e1c6" -> null
- directory_permission = "0777" -> null
- file_permission = "0777" -> null
- filename = "./pipeline/nightly.json" -> null
- id = "9fa8a3e5f766784cfcc4ca260490e6964c53326d" -> null
}
# local_file.storage_account will be destroyed
- resource "local_file" "storage_account" {
- content = jsonencode(
{
- resource_group = "rg-sales-data-dev"
}
) -> null
- content_base64sha256 = "nBDRbF9rs3O9ioH6cYlBAOaBJG2v33yUc5lkIM8C/Eg=" -> null
- content_base64sha512 =
"hpcaf/NUa/k36G76hBAElZ5uznqhvdj8zSIROeOVvl9B0tGPBBHtIDoT4F2KQt8W3LNrsPSAJFoOCCkE9ArM2w==" ->
null
- content_md5 = "4bbbca4c10ee1faae24f3ddf97f4a3a9" -> null
- content_sha1 = "4a92724d08d3a46e482d7b8bc5ce7a3171e1ab7d" -> null
- content_sha256 = "9c10d16c5f6bb373bd8a81fa71894100e681246dafdf7c9473996420cf02fc48" ->
null
- content_sha512 =
"86971a7ff3546bf937e86efa841004959e6ece7aa1bdd8fccd221139e395be5f41d2d18f0411ed203a13e05d8a42df1
6dcb36bb0f480245a0e082904f40accdb" -> null
- directory_permission = "0777" -> null
- file_permission = "0777" -> null
- filename = "./stsalesdatadev.json" -> null
- id = "4a92724d08d3a46e482d7b8bc5ce7a3171e1ab7d" -> null
}
# terraform_data.role_assignment will be destroyed
- resource "terraform_data" "role_assignment" {
- id = "<uuid>" -> null
- triggers_replace = "4a92724d08d3a46e482d7b8bc5ce7a3171e1ab7d" -> null
}
Plan: 0 to add, 0 to change, 6 to destroy.
Error: Instance cannot be destroyed
on storage.tf line 1:
1: resource "local_file" "storage_account" {
Resource local_file.storage_account has lifecycle.prevent_destroy set, but
the plan calls for this resource to be destroyed. To avoid this error and
continue with the plan, either disable lifecycle.prevent_destroy or reduce
the scope of the plan using the -target option.It fails with Error: Instance cannot be destroyed, even with auto approve, and nothing is deleted. It also stops any plan that would replace the account.
The protection lives in the resource block. If someone deletes the block, prevent_destroy goes with it, and the next plan destroys the account. So review still matters.
Then we add layers outside Terraform. And nobody applies production from a laptop:
- Azure resource lock - on production resource groups
- Soft delete - blobs and containers on the storage account
- Purge protection - on Key Vault
- Pipeline-only applies - one identity can write, after an approval
Scenario 7: The team wants to start using Terraform for a new data platform next sprint. What is your setup on day one?
One Git repo for the platform code. Pin Terraform with required_version and the providers with constraints, and commit .terraform.lock.hcl. Never commit *.tfstate files or the .terraform/ folder.
Then remote state from the first apply. The backend "azurerm" block in azure/versions.tf points to a storage account just for state, with one key per environment. It uses Entra ID and OIDC from the pipeline (use_oidc and use_azuread_auth). No storage keys, and no client secrets.
Then the checks on every pull request. We run terraform fmt -check -recursive, terraform -chdir=azure validate and terraform -chdir=azure test. First we put the containers file back to the azurerm 5 version.
resource "azurerm_storage_container" "layer" {
for_each = toset(["landing", "raw", "curated"])
name = each.key
storage_account_id = azurerm_storage_account.lake.id
}$ terraform fmt -check -recursive
$ terraform -chdir=azure validate
Success! The configuration is valid.
$ terraform -chdir=azure test
tests/lake.tftest.hcl... in progress
run "lake_is_locked_down"... pass
tests/lake.tftest.hcl... tearing down
tests/lake.tftest.hcl... pass
Success! 1 passed, 0 failed.The format check prints nothing, so the code is clean. Validate passes. And the test says Success! 1 passed, 0 failed. It uses a mocked azurerm provider (mock_provider "azurerm" {}), and proves the lake keeps the hierarchical namespace on and shared keys off, without any Azure credentials.
Then the flow:
- Pull request - fmt, validate, test, plan
- Review - a reviewer approves
- Pipeline - applies the saved plan
- Azure - people get read access
Resource group, lake and Key Vault in dev, then grow. Getting state, pinning and the pipeline right on day one is much cheaper than fixing them later.
Key points to remember
- Platform as code: reviewed, repeatable, with a plan first
- init installs, plan shows, apply runs the saved plan
- Pin providers and commit .terraform.lock.hcl
- Read must be replaced and forces replacement before you approve
- References build the graph; prevent_destroy + Azure locks protect
The full script
This is a Python script that writes every file and runs every command above in order: put Terraform 1.17 on your PATH and run it in an empty folder.
Show the full script that writes the files and runs every step
# ========================================================================
# Terraform Interview Q&A #1 - Terraform fundamentals and workflow
# TechBrothersIT. Names and sample values are invented for teaching.
# Every step below was run with Terraform 1.17.0; the outputs in the video and the blog post are the real outputs.
# Providers used: hashicorp/random, null, local, time (they create nothing in any cloud). Azure and Databricks
# code in this series is checked with terraform validate only: nothing is created in a real subscription.
# To run it yourself (Linux, macOS or WSL; some steps use grep / cat):
# 1. Install Terraform 1.17 from developer.hashicorp.com/terraform/install and put it on your PATH.
# 2. python Terraform_Interview_QA_01_Code.py in an empty folder (terraform init downloads the providers).
# ========================================================================
import os, subprocess
def write(path, text):
if text is None:
if os.path.exists(path): os.remove(path)
return
os.makedirs(os.path.dirname(path) or ".", exist_ok=True)
with open(path, "w", encoding="utf-8") as f:
f.write(text.strip("\n") + "\n")
def run(cmd, expect_error=False, allow_error=False, env=None):
print("\n$ " + cmd, flush=True)
rc = subprocess.run(cmd, shell=True, env=dict(os.environ, **(env or {}))).returncode
if rc != 0 and not (expect_error or allow_error):
raise SystemExit("stopped: this command was expected to succeed")
# Step 0: the starting files
write('versions.tf', """\
terraform {
required_version = "~> 1.17"
required_providers {
local = {
source = "hashicorp/local"
version = "~> 2.9"
}
}
}
""")
write('main.tf', """\
# Local stand-ins: the plan is real, nothing is created in Azure
locals {
storage_name = "stsalesdatadev"
tags = { env = "dev", owner = "sales-data" }
}
resource "terraform_data" "resource_group" {
input = { name = "rg-sales-data-dev", tags = local.tags }
}
""")
write('storage.tf', """\
resource "local_file" "storage_account" {
filename = "${path.module}/${local.storage_name}.json"
content = jsonencode({
resource_group = terraform_data.resource_group.input.name
})
}
resource "local_file" "container" {
for_each = toset(["landing", "raw", "curated"])
filename = "${path.module}/lake/${each.key}.keep"
content = local_file.storage_account.id
}
""")
write('pipeline.tf', """\
resource "terraform_data" "role_assignment" {
# pipeline identity: Storage Blob Data Contributor on the lake
triggers_replace = local_file.storage_account.id
}
resource "local_file" "pipeline" {
filename = "${path.module}/pipeline/nightly.json"
content = jsonencode({ source = "landing", target = "curated" })
depends_on = [terraform_data.role_assignment] # needs access first
}
""")
write('azure/versions.tf', """\
terraform {
required_version = "~> 1.17"
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~> 5.11"
}
}
backend "azurerm" {
use_oidc = true
use_azuread_auth = true
storage_account_name = "stsalestfstate"
container_name = "tfstate"
key = "sales-data/dev.terraform.tfstate"
}
}
""")
write('azure/providers.tf', """\
provider "azurerm" {
features {}
# subscription, tenant and client IDs: ARM_* variables in the pipeline
}
""")
write('azure/main.tf', """\
locals {
tags = { env = "dev", owner = "sales-data" }
}
resource "azurerm_resource_group" "sales" {
name = "rg-sales-data-dev"
location = "eastus2"
tags = local.tags
}
resource "azurerm_storage_account" "lake" {
name = "stsalesdatadev"
resource_group_name = azurerm_resource_group.sales.name
location = azurerm_resource_group.sales.location
account_tier = "Standard"
account_replication_type = "LRS"
is_hns_enabled = true
shared_access_key_enabled = false
min_tls_version = "TLS1_2"
tags = local.tags
}
""")
write('azure/containers.tf', """\
resource "azurerm_storage_container" "layer" {
for_each = toset(["landing", "raw", "curated"])
name = each.key
storage_account_id = azurerm_storage_account.lake.id
}
""")
write('azure/tests/lake.tftest.hcl', """\
mock_provider "azurerm" {}
run "lake_is_locked_down" {
command = plan
assert {
condition = azurerm_storage_account.lake.is_hns_enabled
error_message = "The lake needs the hierarchical namespace."
}
assert {
condition = !azurerm_storage_account.lake.shared_access_key_enabled
error_message = "Shared keys must be off: Entra ID only."
}
}
""")
run('terraform fmt -check -recursive')
# Step 1: Install the azurerm provider (backend skipped: we only validate) and read the lock file
run('terraform -chdir=azure init -backend=false')
run('cat azure/.terraform.lock.hcl')
# Step 2: Validate the Azure code against the azurerm 5.11 schema (no Azure calls)
run('terraform -chdir=azure validate')
# Step 3: azurerm 4.x style container code fails on azurerm 5.11
write('azure/containers.tf', """\
# copied from an azurerm 4.x example
resource "azurerm_storage_container" "layer" {
for_each = toset(["landing", "raw", "curated"])
name = each.key
storage_account_name = azurerm_storage_account.lake.name
}
""")
run('terraform -chdir=azure validate', expect_error=True) # EXPECTED TO FAIL: the video shows this error
# Step 4: init: backend, providers, lock file
run('terraform init')
# Step 5: plan, saved to a file
run('terraform plan -out=tfplan')
# Step 6: apply exactly the saved plan
run("terraform apply tfplan > tf.log 2>&1; rc=$?; grep -E '^(terraform_data\\.resource_group|local_file\\.storage_account):|Apply complete' tf.log; exit $rc")
# Step 7: Rename the storage account and change one tag
write('main.tf', """\
# Local stand-ins: the plan is real, nothing is created in Azure
locals {
storage_name = "stsalesdatadev01"
tags = { env = "dev", owner = "sales-analytics" }
}
resource "terraform_data" "resource_group" {
input = { name = "rg-sales-data-dev", tags = local.tags }
}
""")
run("terraform plan -out=tfplan > tf.log 2>&1; rc=$?; grep -v 'Refreshing state' tf.log; exit $rc")
# Step 8: The same plan as JSON: the actions per resource
run('terraform show -json tfplan | jq -r \'.resource_changes[] | "\\(.change.actions) \\(.address)"\'')
# Step 9: The dependency graph (code back to the original)
write('main.tf', """\
# Local stand-ins: the plan is real, nothing is created in Azure
locals {
storage_name = "stsalesdatadev"
tags = { env = "dev", owner = "sales-data" }
}
resource "terraform_data" "resource_group" {
input = { name = "rg-sales-data-dev", tags = local.tags }
}
""")
run('terraform graph -format=mermaid')
# Step 10: destroy asks first: only yes is accepted
run("echo no | terraform destroy > tf.log 2>&1; rc=$?; grep -v 'Refreshing state' tf.log; exit $rc", allow_error=True)
# Step 11: prevent_destroy on the storage account
write('storage.tf', """\
resource "local_file" "storage_account" {
filename = "${path.module}/${local.storage_name}.json"
content = jsonencode({
resource_group = terraform_data.resource_group.input.name
})
lifecycle {
prevent_destroy = true
}
}
resource "local_file" "container" {
for_each = toset(["landing", "raw", "curated"])
filename = "${path.module}/lake/${each.key}.keep"
content = local_file.storage_account.id
}
""")
run("terraform destroy -auto-approve > tf.log 2>&1; rc=$?; grep -v 'Refreshing state' tf.log; exit $rc", expect_error=True) # EXPECTED TO FAIL: the video shows this error
# Step 12: Day-one checks for every pull request
write('azure/containers.tf', """\
resource "azurerm_storage_container" "layer" {
for_each = toset(["landing", "raw", "curated"])
name = each.key
storage_account_id = azurerm_storage_account.lake.id
}
""")
run('terraform fmt -check -recursive')
run('terraform -chdir=azure validate')
run('terraform -chdir=azure test')
Part 2: State and Remote Backends
The sample data on this page is invented for teaching. Every terminal output is the real output of the command above it, run with Terraform 1.17.0; IDs, timestamps and durations are masked. Azure and Databricks code is checked with terraform validate. Part of the Real Time Terraform Interview Questions and Answers series on TechBrothersIT. Features, limits and their status (GA or preview) are current as of October 2026; check the official Terraform documentation (developer.hashicorp.com/terraform) for anything you quote in an interview.



No comments:
Post a Comment
Note: Only a member of this blog may post a comment.