Deploying IAC with your secrets in Terraform Vault
Tags: tutorial, cloud, security, devops
Published: 2022-03-21 02:32:34

## DAY 23 - Deploying IAC with your secrets in Terraform Vault  - Day Twenty three

[![Image tweet](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jfjzxg64r7q0i8ra6b8t.png)![Image COVER](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hoxop4oqy3ccv3mocgiy.png)](https://twitter.com/intent/tweet?text=How%20to%20Deploying%20IAC%20with%20your%20secrets%20in%20Terraform%20Vault%2C%20a%20blog%20by%20%40anuvindhs%0A%23tutorial%20%23cloud%20%23security%20%23devops%0A%0Ahttps%3A%2F%2Fdev.to%2Faws-builders%2Fdeploying-iac-with-your-secrets-in-terraform-vault-4ggc%20%0A)


[100 days of Cloud on GitHub](https://github.com/anuvindhs/100daysofcloud) - [Read On iCTPro.co.nz](https://ictpro.co.nz) - [Read on Dev.to](https://dev.to/anuvindhs)

------

## What is Vault?


Securely accesses secrets , HashiCorp explains as _Vault comes with various pluggable components called secrets engines and authentication methods allowing you to integrate with external systems_. Vault issues  temporary tokens to access the resources.
![Image vault image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/61g90a90b8tw2jmdg7rw.png)

- In the blog I will be demonstrating how to setup a vault
- Accessing secrets from Vault to Deploy your infrastructure into a AWS environment.
 
## Prerequisite

[Install AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html) and Configure with IAM credentials 

![Image click](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vaafdnc2n5f2xvjdi29b.gif)
 


## Install Vault

Goto this [link to install Vault](https://learn.hashicorp.com/tutorials/vault/getting-started-install) 
I Am using a WSL Linux on windows

- Getting GPG key
```bash
curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
```
- Adding HashiCorp Linux repo
```bash
sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
```
- install 

```bash
sudo apt-get update && sudo apt-get install vault
```

## Setup Vault Project folder

-  Lets create a project folder named Vault and cd into it

 

## Setup Vault Server (Dev environment)

```bash
vault server -dev -dev-root-token-id="environment"
```
![Image vault server](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vo91v6od6w8303vbhp6k.png)

Note down the **Vault Address, Unsealkey & Root token**.

## Sign into vault server

![Image vault login](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b58t0mdsbexq3w6xthfq.png)
Enter token as "environment"  
 
- Select **Secret** & Click **Create Secret**.
- Enter your IAM **Programmatic access keys** & **Save**. 

![Image AWS CLI](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oncw0t82yr3yaio6ipt0.png)

## Deploying with Vault
 
 
- Create a main.tf file and copy paste this command 
```bash
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "3.58.0"
    }
  }
}

data "vault_generic_secret" "aws_creds" {
	path = "secret/aws"
}

provider "aws" {
  region  = data.vault_generic_secret.aws_creds.data["region"]
	access_key = data.vault_generic_secret.aws_creds.data["aws_access_key_id"]
	secret_key = data.vault_generic_secret.aws_creds.data["aws_secret_access_key"]
}


resource "aws_instance" "my_server" {
  ami           = "ami-059af0b76ba105e7e"
  instance_type = "t2.nano"
	tags = {
		Name = "Vault-Server"
	}
}
```
You have to **change ami** incase your region is **not on ap-southeast-2**

- Initialize Terraform

```bash
terraform init
```

- Plan terraform

```bash
terraform plan
```
You will be prompted to enter the **vault url**, in this case it is **http://127.0.0.1:8200/**

![Image url vault](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ar34w0qg3lgvo1ugduhl.png)

 
- Deploy infrastructure

```bash
terraform apply -auto-approve
```
 - Enter url of vault when its prompted.


![Image result](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/otxs4qvtgwvenxe8jh42.png)

- Teardown your infrastructure

  if you are happy with the deployment , you can tear down the deployed resources

```bash
terraform apply -auto-approve -destroy
```

😀🎉Congratulations🎉 you have successfully deployed IAC with Terraform Vault

![Image congratulations](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xqv3ckl9agr0djcu80l9.gif)
  ----------

**✅Connect with me on [Twitter](https://twitter.com/intent/follow?ref_src=anuvindhs&screen_name=anuvindhs)
 🤝🏽Connect with me on [Linkedin](https://www.linkedin.com/in/anuvindhs)
🧑🏼‍🤝‍🧑🏻 Read more post on [dev.to](https://dev.to/anuvindhs) or  [iCTPro.co.nz](https://ictpro.co.nz)
💻 Connect with me on [GitHub](https://github.com/anuvindhs)**


{% user anuvindhs %}