<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Hashicorp-Vault with Terraform]]></title><description><![CDATA[Hashicorp-Vault with Terraform]]></description><link>https://hashi-vault.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Fri, 18 Sep 2026 01:20:27 GMT</lastBuildDate><atom:link href="https://hashi-vault.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[🔐 How I Used Vault with Terraform to Securely Deploy AWS Infrastructure]]></title><description><![CDATA[🌟 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...]]></description><link>https://hashi-vault.hashnode.dev/how-i-used-vault-with-terraform-to-securely-deploy-aws-infrastructure</link><guid isPermaLink="true">https://hashi-vault.hashnode.dev/how-i-used-vault-with-terraform-to-securely-deploy-aws-infrastructure</guid><category><![CDATA[Vault]]></category><category><![CDATA[Terraform]]></category><category><![CDATA[GitHub]]></category><category><![CDATA[#IaC]]></category><category><![CDATA[IaC (Infrastructure as Code)]]></category><category><![CDATA[AWS]]></category><dc:creator><![CDATA[HARSHAL VERNEKAR]]></dc:creator><pubDate>Mon, 25 Aug 2025 10:33:34 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1756117924948/1e37bf58-0b9e-4b23-aa15-58c458c4c9e0.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h2 id="heading-why-i-did-this-project">🌟 Why I Did This Project</h2>
<p>When building infrastructure with Terraform, one of the scariest mistakes is leaving <strong>secrets inside your code</strong>.<br />I used to write database passwords, role IDs, and API keys directly into <code>.tf</code> files.</p>
<p>Bad idea 😅. If that code ever lands on GitHub or a shared system, my secrets are out in the wild.</p>
<p>So I tried something new: I connected <strong>Terraform with HashiCorp Vault</strong>. Instead of hardcoding secrets, Terraform now <strong>logs in to Vault using AppRole</strong> and securely fetches values at runtime. Then I used those secrets to tag an AWS instance.</p>
<p>Here’s how I set it up 👇</p>
<hr />
<h2 id="heading-what-this-project-does">🚀 What This Project Does</h2>
<ul>
<li><p>Authenticates Terraform with Vault using <strong>AppRole login</strong></p>
</li>
<li><p>Fetches secrets from Vault’s <strong>KV v2 secret engine</strong></p>
</li>
<li><p>Injects those secrets into an AWS EC2 resource dynamically</p>
</li>
</ul>
<hr />
<h2 id="heading-prerequisites">⚙️ Prerequisites</h2>
<p>Before running this project, you’ll need:</p>
<ul>
<li><p>A running <strong>Vault server</strong> (in my case on <code>http://13.50.5.78:8200</code>)</p>
</li>
<li><p>An <strong>AppRole configured</strong> in Vault with a <code>role_id</code> and <code>secret_id</code></p>
</li>
<li><p>AWS account + credentials configured</p>
</li>
<li><p>Terraform installed</p>
</li>
</ul>
<hr />
<h2 id="heading-project-structure">📂 Project Structure</h2>
<pre><code class="lang-plaintext">vault-aws/
 ├── main.tf
 └── README.md
</code></pre>
<hr />
<h2 id="heading-the-terraform-code">📝 The Terraform Code</h2>
<p>Here’s my <code>main.tf</code>:</p>
<pre><code class="lang-plaintext">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"]
  }
}
</code></pre>
<hr />
<h2 id="heading-how-it-works">🔎 How It Works</h2>
<ol>
<li><p><strong>Vault Provider</strong><br /> Terraform connects to Vault at <code>http://13.50.5.78:8200</code>.<br /> Instead of a static token, it logs in using <strong>AppRole authentication</strong> (<code>role_id</code> + <code>secret_id</code>).</p>
</li>
<li><p><strong>Secret Retrieval</strong><br /> It fetches a secret from the <code>kv/test-secret</code> path.<br /> Example secret inside Vault:</p>
<pre><code class="lang-plaintext"> {
   "username": "my-app-user",
   "password": "super-secret-pass"
 }
</code></pre>
</li>
<li><p><strong>Using the Secret</strong><br /> The <code>username</code> from Vault is used as a <strong>tag</strong> on the AWS EC2 instance.<br /> This shows how Terraform can dynamically inject Vault secrets into infrastructure.</p>
</li>
</ol>
<hr />
<h2 id="heading-running-the-project">▶️ Running the Project</h2>
<ol>
<li><p><strong>Initialize Terraform</strong></p>
<pre><code class="lang-plaintext"> terraform init
</code></pre>
</li>
<li><p><strong>Plan</strong></p>
<pre><code class="lang-plaintext"> terraform plan
</code></pre>
</li>
<li><p><strong>Apply</strong></p>
<pre><code class="lang-plaintext"> terraform apply -auto-approve
</code></pre>
</li>
<li><p><strong>Verify</strong></p>
<ul>
<li><p>Go to your AWS console</p>
</li>
<li><p>Find the EC2 instance</p>
</li>
<li><p>Check the <strong>tags</strong> → you’ll see the <code>username</code> pulled straight from Vault 🎉</p>
</li>
</ul>
</li>
</ol>
<hr />
<h2 id="heading-security-lessons-learned">🔒 Security Lessons Learned</h2>
<ul>
<li><p>🔑 <strong>AppRole beats static tokens</strong>: Instead of keeping one token forever, AppRole allows Vault to generate scoped, revocable credentials for Terraform.</p>
</li>
<li><p>🚫 <strong>No secrets in code</strong>: Notice that <code>username</code> never exists in <code>.tf</code> files, only in Vault.</p>
</li>
<li><p>🔐 <strong>Vault + Terraform = IaC done right</strong>: This approach keeps infra automation both secure and repeatable.</p>
</li>
</ul>
<hr />
<h2 id="heading-wrapping-up">🎯 Wrapping Up</h2>
<p>This was a small experiment, but it showed me how powerful <strong>Vault + Terraform integration</strong> can be.<br />Instead of worrying about leaking secrets in GitHub, I can confidently manage them in Vault while still automating infrastructure with Terraform.</p>
<p>👉 Check out the project on GitHub:<br /><a target="_blank" href="https://github.com/Harshalv21/Terraform-Projects/tree/main/vault">Terraform Vault Integration</a></p>
<p>🙋 Author: <strong>Harshal Vernekar</strong><br />GitHub: <a target="_blank" href="https://github.com/Harshalv21">@Harshalv21</a></p>
]]></content:encoded></item></channel></rss>