Learn to use the Vault Terraform provider
In addition to the CLI and the API, Vault's capabilities are accessible using the Vault provider for Terraform. The Vault provider uses the Vault HTTP API to interact with Vault using a series of files called a configuration. This configuration and the provider manage the resources that Terraform creates in Vault.
Scenario
Oliver and the operations team manage Vault at HashiCups. Part of Oliver's job is to create logins and passwords for developers at HashCups to login to Vault.
Danielle and the development teams needs to login to Vault to create secrets used by services at HashiCups. Oliver will enable the userpass
auth method, create a user, set a password, and create and attach a policy to the user. The development team needs a secrets engine, which Oliver will create.
Danielle will then log into Vault using the userpass auth method, and create a secret.
A new standard at HashiCups requires teams to manage infrastructure with Terraform. Danielle and Oliver are also starting to use Vault, and want to use the advantages of Infrastructure as Code (IAC) to manage the Vault.
Prerequisites
To complete this tutorial, you need the following:
Set up the lab
Open a terminal and start a Vault dev server with the literal string
root
as the root token value, and enable TLS.The dev server listens on the loopback interface at 127.0.0.1 on TCP port 8200 with TLS enabled. At runtime, the dev server also automatically unseals, and prints the unseal key and initial root token values to the standard output.
Root tokens
The dev mode server starts with an initial root token value set.
Root token use should be extremely guarded in production environments because they provide full access to the Vault server.
The root token is used here for convenience and to keep the tutorial steps focused on what you'll learn.
In a new terminal, export the
VAULT_ADDR
andVAULT_CACERT
environment variables using the commands suggested in your Vault dev server output.Copy each command (without the
$
) from the server output, and paste it into the new terminal session.Example:
Example:
Remember to use your dev server's values, not the examples shown here.
Export an environment variable for the
vault
CLI to authenticate with the Vault server.The Vault server is ready.
Codify infrastructure
HashiCups requires infrastructure management with Terraform, but Oliver and Danielle are new to Terraform and are creating configuration for the first time.
As demonstrated during the What is Vault tutorial, Vault supports both human and machine auth methods. Danielle will use the userpass auth method to authenticate to Vault which would return a Vault token. Danielle can use that token for future communication with Vault.
The Vault Terraform provider supports authentication with userpass. Danielle can log into Vault with the userpass
auth method, and Terraform will execute the configuration against Vault with the capabilities defined in the policy attached to the token.
Vault Terraform provider
(Persona: operations)
Oliver can use the Vault Terraform provider to create infrastructure. During initial set up, Oliver will install providers and plugins, and check the plan before creating anything.
Use the terminal in which you just exported the environment variables to clone the GitHub repository for learn-vault-terrraform.
Move to the directory for Oliver's work.
Initialize the Terraform configuration.
Example output:
Make sure that the plan reflects the correct number of added resources.
The plan looks good, go ahead and apply the changes to Vault.
What command is used to initialize a Terraform configuration before it can be run?
terraform init
is used to initialize the Terraform configuration.
Auth method resource
(Persona: operations)
Oliver needs to provide a way for developers like Danielle to authenticate with Vault, so they choose to enable the human-friendly userpass auth method.
Now that Vault is initialized, Oliver can enable and configure auth methods. Oliver enables userpass authentication with a Terraform configuration.
To enable the userpass auth method with Terraform, you use the following resource:
Use this resource to enable any auth method plugin by specifying the
type
attribute.There is not a specific structure available to create a user in userpass. Instead, use the
vault_generic_endpoint
resource. You can use this resource to make HTTP API calls not covered by other resources or data types.You can use the
vault_generic_endpoint
resource when no other resource or data type is available.This user has a
token_policy
nameddeveloper-vault-policy
. Vault provides thedefault
policy, and assigns it to the user unless you specify otherwise. Go ahead create an ACL policy nameddeveloper-vault-policy
. Use thevault_policy
resource create the policy.An important aspect of the Vault Terraform provider is that with whatever capabilities it is run with it should include the ability to create a child token. The last capability at
path "auth/token/create"
is required for Terraform to be able to create the child tokens.To confirm, list the Vault policies. Confirm that
developer-vault-policy
is present.
Static secrets
(Persona: operations)
The developers need a secrets engine to keep API keys and other secrets. It is Oliver's job to create a static secrets engine for the team to use.
The policy developer-vault-policy
lets the developer use a secrets engine on the dev-secrets/*data*
path. Starting the Vault server in development mode creates a key/value version 2 secrets engine at secrets/
, which is not needed here. Key/Value version 2 allows key versioning, so Oliver has to create one.
The
vault_mount
resource creates a new secrets engine atdev-secrets
.There are two types of key/value (kv) secrets engines. Version 1 does not version secrets, but version 2 does. This will be a kv version 2 secrets engine.
Confirm creation of
dev-secrets
and list all the active secrets engines.
Developer configuration
(Persona: developer)
Oliver has created the login for Danielle, and set up a secrets engine. Danielle uses the login and the resulting credentials in the provider
resource to connect to Vault. They can then create a static secret.
Open a new terminal in the root directory of the GitHub repository
learn-vault-foundations
.Navigate to the directory Danielle works in.
Use the same values as Oliver, set some environment variables.
In this new directory, initialize the Terraform configuration.
Example output:
Danielle's login is already hardcoded in
provider.tf
.While the login is hard-coded, when you run
terraform plan
you will have to manually put in the password: pass.Make sure that the plan adds the correct number of resources.
Look for the following at the end of the plan.
Example output:
The plan looks good, go ahead and set up Vault.
Terraform will prompt for a password. Once more enter the value pass.
Example output:
Developer's credentials
(Persona: developer)
Look at
provider.tf
. The following structure uses the login and password fordanielle-vault-user
and retrieves theclient-token
needed to run the Terraform configuration.Once supplied with a login and password, the userpass auth method will supply a token directly to the Vault Terraform provider. Calls to Vault will be using Danielle's token, and will interact with Vault as they are the user
danielle-vault-user
and have the capabilities defined by the policydeveloper-vault-policy
.In
main.tf
there is avault_kv_secret_v2
resource that creates a secret namedcreds
in thedev-secrets
secret engine. Thecreds
has a key namedpassword
with a valuemy-long-password
.Now examine the secret through the CLI.
Clean up
Use
CTRL+C
to stop the server process in the terminal window where you started the server, or use this command to kill the server process from any local terminal session:In the Oliver's terminal, unset the
VAULT_TOKEN
,VAULT_ADDR
environment variables.Run the same command in Danielle's terminal.
Summary
Terraform supports Vault from deployment to on-going configuration. Operations teams can write Terraform configurations for repeatable deployments. You can share Terraform configurations with other teams so they can create environments that match production for development and test. Ops teams such as DevOps or SecOps follow IAC best practices such as GitOps for all updates to Vaults configuration.