π How I Used Vault with Terraform to Securely Deploy AWS Infrastructure


π Why I Did This Project
When building infrastructure with Terraform, one of the scariest mistakes is leaving secrets inside your code.
I used to write database passwords, role IDs, and API keys directly into .tf
files.
Bad idea π . If that code ever lands on GitHub or a shared system, my secrets are out in the wild.
So I tried something new: I connected Terraform with HashiCorp Vault. Instead of hardcoding secrets, Terraform now logs in to Vault using AppRole and securely fetches values at runtime. Then I used those secrets to tag an AWS instance.
Hereβs how I set it up π
π What This Project Does
Authenticates Terraform with Vault using AppRole login
Fetches secrets from Vaultβs KV v2 secret engine
Injects those secrets into an AWS EC2 resource dynamically
βοΈ Prerequisites
Before running this project, youβll need:
A running Vault server (in my case on
http://13.50.5.78:8200
)An AppRole configured in Vault with a
role_id
andsecret_id
AWS account + credentials configured
Terraform installed
π Project Structure
vault-aws/
βββ main.tf
βββ README.md
π The Terraform Code
Hereβs my main.tf
:
provider "aws" {
region = "eu-north-1"
}
provider "vault" {
address = "http://13.50.5.78:8200"
skip_child_token = true
auth_login {
path = "auth/approle/login"
parameters = {
role_id = "a3ce1f6a-b1eb-2b4f-0355-1cc2054eb440"
secret_id = "a265ed42-401c-d40a-e675-e6b45269fe2f"
}
}
}
# Fetch secret from Vault KV v2
data "vault_kv_secret_v2" "example" {
mount = "kv"
name = "test-secret"
}
# Use the secret in AWS EC2 resource
resource "aws_instance" "name" {
ami = "ami-042b4708b1d05f512"
instance_type = "t3.micro"
tags = {
secret = data.vault_kv_secret_v2.example.data["username"]
}
}
π How It Works
Vault Provider
Terraform connects to Vault athttp://13.50.5.78:8200
.
Instead of a static token, it logs in using AppRole authentication (role_id
+secret_id
).Secret Retrieval
It fetches a secret from thekv/test-secret
path.
Example secret inside Vault:{ "username": "my-app-user", "password": "super-secret-pass" }
Using the Secret
Theusername
from Vault is used as a tag on the AWS EC2 instance.
This shows how Terraform can dynamically inject Vault secrets into infrastructure.
βΆοΈ Running the Project
Initialize Terraform
terraform init
Plan
terraform plan
Apply
terraform apply -auto-approve
Verify
Go to your AWS console
Find the EC2 instance
Check the tags β youβll see the
username
pulled straight from Vault π
π Security Lessons Learned
π AppRole beats static tokens: Instead of keeping one token forever, AppRole allows Vault to generate scoped, revocable credentials for Terraform.
π« No secrets in code: Notice that
username
never exists in.tf
files, only in Vault.π Vault + Terraform = IaC done right: This approach keeps infra automation both secure and repeatable.
π― Wrapping Up
This was a small experiment, but it showed me how powerful Vault + Terraform integration can be.
Instead of worrying about leaking secrets in GitHub, I can confidently manage them in Vault while still automating infrastructure with Terraform.
π Check out the project on GitHub:
Terraform Vault Integration
π Author: Harshal Vernekar
GitHub: @Harshalv21
Subscribe to my newsletter
Read articles from HARSHAL VERNEKAR directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

HARSHAL VERNEKAR
HARSHAL VERNEKAR
π Aspiring DevOps & Cloud Engineer with a strong foundation in cloud platforms (AWS), infrastructure automation, and container orchestration tools like Docker and Kubernetes. Iβm passionate about building reliable, scalable, and secure cloud-native applications. π§ Currently building real-world projects using Terraform, Ansible, Jenkins, GitHub Actions, and EKS to understand how modern infrastructure is deployed, managed, and monitored. I enjoy breaking things (safely), debugging, and learning from hands-on experience. π¦ Comfortable working with: AWS (EC2, S3, IAM, VPC, EKS) Docker, Kubernetes (Minikube & EKS) CI/CD tools like Jenkins & GitHub Actions IaC tools like Terraform & Ansible Monitoring with Prometheus & Grafana Linux, Bash, Git, and Networking fundamentals π‘ Always learning β currently exploring deeper concepts in Kubernetes workloads, Helm, and scaling best practices. π Open to DevOps, Cloud, or SRE roles where I can grow, contribute, and solve real-world problems.