Bird
Raised Fist0
Terraformcloud~10 mins

Secret management integration (Vault, Secrets Manager) in Terraform - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Process Flow - Secret management integration (Vault, Secrets Manager)
Define secret in Vault/Secrets Manager
Configure Terraform provider for Vault/Secrets Manager
Reference secret in Terraform configuration
Terraform plan reads secret securely
Terraform apply uses secret to configure resources
Secret remains secure, not exposed in state or logs
This flow shows how Terraform integrates with secret managers to securely use secrets during infrastructure deployment.
Execution Sample
Terraform
provider "vault" {
  address = "https://vault.example.com"
}

data "vault_generic_secret" "db_password" {
  path = "secret/data/db"
}

resource "aws_db_instance" "example" {
  password = data.vault_generic_secret.db_password.data["password"]
}
Terraform reads a database password from Vault and uses it to create an AWS database instance.
Process Table
StepActionTerraform StateSecret AccessedResult
1Initialize Vault providerProvider configuredNoReady to read secrets
2Read secret at path 'secret/data/db'Secret data fetchedYesPassword retrieved securely
3Plan AWS DB instance with secret passwordPlan createdYesPassword referenced in plan
4Apply AWS DB instance creationResource createdYesDB instance created with secret password
5Post-apply stateState saved without secret valueNoSecret not exposed in state file
💡 Terraform completes apply; secret used securely without exposure.
Status Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
vault_secret_passwordnull"superSecret123""superSecret123""superSecret123"null (not stored)
terraform_stateemptyprovider configuredplan createdresource createdstate saved without secret
Key Moments - 2 Insights
Why doesn't the secret value appear in the Terraform state file after apply?
Terraform reads the secret at runtime but does not store the actual secret value in the state file to keep it secure, as shown in execution_table step 5.
How does Terraform access the secret without exposing it in logs?
Terraform uses the Vault provider's secure API to fetch secrets at runtime and references them internally without printing them, as seen in execution_table steps 2 and 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step does Terraform fetch the secret from Vault?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Check the 'Secret Accessed' column in execution_table row for step 2.
According to the variable tracker, what is the value of 'vault_secret_password' after step 4?
Anull
Bempty string
C"superSecret123"
Dnot defined
💡 Hint
Look at the 'vault_secret_password' row in variable_tracker after step 4.
If Terraform stored the secret in the state file, which step in the execution table would change?
AStep 5
BStep 3
CStep 2
DStep 1
💡 Hint
Refer to the 'Result' column in execution_table step 5 about state file contents.
Concept Snapshot
Terraform secret management integration:
- Configure Vault or Secrets Manager provider
- Use data sources to read secrets securely
- Reference secrets in resource configs
- Secrets fetched at runtime, not stored in state
- Keeps secrets safe during infrastructure deployment
Full Transcript
This visual execution shows how Terraform integrates with secret management systems like Vault or Secrets Manager. First, Terraform configures the provider to connect securely. Then it reads the secret data at runtime without exposing it in logs or state files. The secret is used to configure resources, such as a database password. After apply, the secret remains secure and is not stored in the Terraform state file. This ensures secrets are handled safely during infrastructure deployment.

Practice

(1/5)
1. What is the main purpose of integrating Terraform with a secret management tool like Vault or AWS Secrets Manager?
easy
A. To securely store and access sensitive data like passwords and API keys outside the code
B. To speed up Terraform plan and apply operations
C. To automatically generate Terraform configuration files
D. To monitor cloud resource usage and billing

Solution

  1. Step 1: Understand secret management purpose

    Secret management tools keep sensitive data safe and separate from code to reduce risk.
  2. Step 2: Connect to Terraform integration goal

    Terraform uses these tools to fetch secrets securely during infrastructure deployment without hardcoding them.
  3. Final Answer:

    To securely store and access sensitive data like passwords and API keys outside the code -> Option A
  4. Quick Check:

    Secret management = Secure external storage [OK]
Hint: Secrets keep sensitive data out of code [OK]
Common Mistakes:
  • Thinking secret managers speed up Terraform
  • Confusing secret management with billing tools
  • Assuming secret managers generate configs
2. Which Terraform block correctly configures AWS Secrets Manager to read a secret named db_password?
easy
A. variable "db_password" { default = "aws_secretsmanager_secret.db_password" }
B. resource "aws_secretsmanager_secret" "example" { name = "db_password" }
C. provider "aws" { secret_name = "db_password" }
D. data "aws_secretsmanager_secret_version" "example" { secret_id = "db_password" }

Solution

  1. Step 1: Identify correct Terraform data source for reading secret

    Terraform uses data "aws_secretsmanager_secret_version" to read secret values from AWS Secrets Manager.
  2. Step 2: Check syntax correctness

    The block uses secret_id = "db_password" to specify the secret name, which is correct for reading.
  3. Final Answer:

    data "aws_secretsmanager_secret_version" "example" { secret_id = "db_password" } -> Option D
  4. Quick Check:

    Read secret = data source block [OK]
Hint: Use data block to read secrets, not resource [OK]
Common Mistakes:
  • Using resource block to read secrets
  • Putting secret name in provider block
  • Assigning secret as variable default incorrectly
3. Given this Terraform snippet using Vault provider:
data "vault_generic_secret" "db" {
  path = "secret/data/database"
}

output "db_password" {
  value = data.vault_generic_secret.db.data["password"]
}

What will be the output if the secret at secret/data/database contains {"password": "pass123"}?
medium
A. "data.vault_generic_secret.db.data[\"password\"]"
B. "pass123"
C. Error: secret not found
D. null

Solution

  1. Step 1: Understand Vault data source usage

    The vault_generic_secret data source reads secrets at the given path and stores them in data map.
  2. Step 2: Access the password key in output

    The output accesses data.vault_generic_secret.db.data["password"], which matches the secret's password value "pass123".
  3. Final Answer:

    "pass123" -> Option B
  4. Quick Check:

    Output secret value = "pass123" [OK]
Hint: Access secret data map keys directly [OK]
Common Mistakes:
  • Expecting error if secret exists
  • Outputting the literal string instead of value
  • Confusing data structure keys
4. You wrote this Terraform code to read a secret from AWS Secrets Manager:
data "aws_secretsmanager_secret_version" "db" {
  secret_id = aws_secretsmanager_secret.db.name
}

resource "aws_secretsmanager_secret" "db" {
  name = "my_db_password"
}

Terraform plan fails with error: Reference to undeclared resource. What is the problem?
medium
A. The resource block is missing required parameters
B. The secret_id should be a string, not a resource attribute
C. The data source references the resource before it is declared
D. Terraform cannot read secrets from AWS Secrets Manager

Solution

  1. Step 1: Analyze resource and data source order

    The data source references aws_secretsmanager_secret.db.name before the resource is declared, causing a dependency error.
  2. Step 2: Understand Terraform resource referencing rules

    Terraform requires resources to be declared before referencing them in data sources to resolve dependencies correctly.
  3. Final Answer:

    The data source references the resource before it is declared -> Option C
  4. Quick Check:

    Reference order matters in Terraform [OK]
Hint: Declare resources before referencing them [OK]
Common Mistakes:
  • Using resource attributes as string literals
  • Ignoring declaration order
  • Assuming Terraform can't read AWS secrets
5. You want to securely pass a database password stored in Vault to an AWS RDS instance using Terraform. Which approach follows best practices?
hard
A. Use vault_generic_secret data source to fetch password, then pass it as password argument in aws_db_instance resource without storing it in Terraform state
B. Hardcode the password in Terraform variables and update Vault manually
C. Store the password in a local file and read it in Terraform
D. Create the RDS instance first, then manually update password in Vault

Solution

  1. Step 1: Identify secure secret retrieval method

    Using vault_generic_secret data source fetches the password securely at runtime without hardcoding.
  2. Step 2: Pass secret directly to resource without storing in state

    Passing the secret as an argument avoids exposing it in Terraform files or state, following best practices.
  3. Final Answer:

    Use vault_generic_secret data source to fetch password, then pass it as password argument in aws_db_instance resource without storing it in Terraform state -> Option A
  4. Quick Check:

    Fetch secrets dynamically and avoid hardcoding [OK]
Hint: Fetch secrets dynamically, never hardcode passwords [OK]
Common Mistakes:
  • Hardcoding secrets in variables
  • Storing secrets in local files
  • Manual secret updates outside Terraform