Hands-on:- HashiCorp Vault with Terraform
Soumyadip Chatterjee
Multi-Cloud Strategist | OCI DevOps Engineer | GenAI Aspired | Ex-TCS | Terraform ??? | Docker ?? | Kubernetes ?? | Snowflake ?? | Argo CD ?? | Helm ?? | GitLab ?? | Certified: 2x AWS ??, 1x OCI ??, 1x Commvault
In the below project we will install Vault in an EC2 instance & ensure it will configure Terraform to read the secret from Vault . Additionally it also ensure authenticate Terraform instead of default App role which has been defined in Vault web console .
sudo apt update && sudo apt install gpg
wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
gpg --no-default-keyring --keyring /usr/share/keyrings/hashicorp-archive-keyring.gpg --fingerprint
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
sudo apt update
sudo apt install vault
vault server -dev -dev-listen-address="0.0.0.0:8200"
vault auth enable approle
vault policy write terraform - <<EOF
path "secret/data/*" {
capabilities = ["create", "read", "update", "delete", "list"]
}
EOF
vault write auth/approle/role/terraform \
secret_id_ttl=10m \
token_num_uses=10 \
token_ttl=20m \
token_max_ttl=30m \
secret_id_num_uses=40 \
token_policies=terraform
领英推荐
vault read auth/approle/role/my-approle/role-id
vault write -f auth/approle/role/my-approle/secret-id
Now we will apply in our generic terraform script on where we will spin up EC2 instance . This Vault ensures to apply a tag based on the secret we create under kv (Key Value) secret engine & confirms EC2 instance tag (navigate to EC2 console in Tags )
provider "aws" {
region = "us-east-1"
}
This block configures the AWS provider for Terraform. The region parameter specifies that AWS resources will be created in the us-east-1 region.
2. Vault Provider Configuration:
provider "vault" {
address = "<>:8200"
skip_child_token = true
auth_login {
path = "auth/approle/login"
parameters = {
role_id = "<>"
secret_id = "<>"
}
}
}
This block configures the Vault provider for Terraform. The address parameter specifies the address of the Vault server. The auth_login block is used to authenticate to Vault using the AppRole authentication method .
3. Vault Secret Data Source:
data "vault_kv_secret_v2" "example" {
mount = "secret" // change it according to your mount
name = "test-secret" // change it according to your secret
}
This block retrieves a secret from Vault. The mount parameter specifies the path where the KV secrets engine is mounted. The name parameter specifies the path of the secret to retrieve.
4. AWS Instance Resource:
resource "aws_instance" "my_instance" {
ami = "ami-053b0d53c279acc90"
instance_type = "t2.micro"
tags = {
Name = "test"
Secret = data.vault_kv_secret_v2.example.data["foo"]
}
}
This block creates an AWS EC2 instance. The ami parameter specifies the Amazon Machine Image (AMI) to use for the instance. The instance_type parameter specifies the type of instance to launch. The tags block assigns metadata to the instance.
Implementation Steps :-
a> Execute terraform init :- This will authenticate terraform vault
b> Execute terraform apply :- This will apply terraform configuration and spin up EC2 instance .
Hierarchical :-
1> Secret Engines ---> Enable-new engine ---> kv ---> Secrets ---> test-secret(Name of vault)---> Then apply key (any name eg:-Username) Value as credentials (eg :password).