Manage HCP Vault Dedicated with Terraform
Challenge
In the previous tutorial, you create an HCP Vault Dedicated cluster using Terraform. There are several post deployment tasks you may also need to perform to get the most from your Vault Dedicated cluster.
Solution
By following the principles covered in Deploy HCP Vault Dedicated with Terraform, you can use Terraform to manage manage the lifecycle of your Vault Dedicated cluster - from initial creation, scaling up to a larger tier and size of the cluster, adding performance replication, and scaling the cluster down to a smaller tier.
One of the many benefits of Vault Dedicated is you can accomplish this with no downtime by making just a few changes to your existing Terraform configuration.
Scenario introduction
After successfully migrating your self-hosted cluster to HCP, your organization wants to increase its utilization of Vault. Based on your current application load, it is necessary to scale your Vault Dedicated cluster to multi-region configuration during peak business hours. After peak business hours you want to scale down your cluster to manage cost.
In this lab you will:
Use your existing Terraform configuration to scale your cluster from the dev tier to the plus tier to support increased Vault Dedicated utilization.
After scaling your Vault Dedicated cluster, you will add performance replication in a new HCP region to support workloads in multiple locations.
Once performance replication has been set up, you will delete the replica and scale the cluster back down to a lower tier.
Prerequisites
A macOS or Linux development host from which you perform most of the tasks; (this lab was lasted tested on macOS 11.6.5)
Existing Vault Dedicated cluster based on this sample Terraform configuration.
Note
To successfully follow along with this lab, you must enable the public interface on your HCP Vault Dedicated cluster.
Personas
This scenario involves 1 persona:
- admin persona runs Vault, applies configuration with Terraform, edits configuration and environment, and applies configuration to Vault Dedicated.
HCP requirements
An HCP account where you have deployed a Vault Dedicated development tier cluster. You should use the sample Terraform configuration to deploy the Vault cluster to follow this lab.
A service principal account assigned the
admin
role in the HCP Portal.
Prepare scenario environment
You can find the Terraform configuration that you will use for this scenario in the learn-manage-codified-hcp-vault- terraform GitHub repository.
Clone the repository.
Change into the repository directory and examine the contents.
Deploy HCP Vault Dedicated cluster
To follow this lab you must have a Vault Dedicated dev tier cluster available using the provided sample Terraform configuration.
Please expand and complete each section below if you do not already have an Vault Dedicated cluster available.
A service principal account allows Terraform to authenticate with HCP using its associated client and secret key.
Launch the HCP Portal and login.
Click Access control (IAM) in the left navigation menu, then click Service principals.
Click Create service principal.
Enter sp-terraform in the Name field, set the role to Admin and click Save. Check the HCP Vault Dedicated Permissions document for more information.
From the sp-terraform page, click Create service principal key.
Copy the Client ID then, in a terminal, set the
HCP_CLIENT_ID
environment variable to the copied value.Switch back to the HCP Portal and copy the Client Secret then, in a terminal, set the
HCP_CLIENT_SECRET
environment variable to the copied value.
Terraform is now able to authenticate with HCP.
Using the Terraform configuration you previously cloned, create a new Vault Dedicated cluster.
From the
learn-manage-codified-hcp-vault-terraform
directory, initialize Terraform.Apply the Terraform configuration to deploy the Vault Dedicated cluster.
Example output:
Add HCP Vault Dedicated environment variables
Setup your local environment to authenticate with Vault Dedicated. By using the example Terraform configuration, you can get the required values from the Terraform output.
Export
VAULT_ADDR
environment variable for the Vault Dedicated public URL.Export
VAULT_TOKEN
environment variable to contain the initial admin token.Export
VAULT_NAMESPACE
environment variable to contain the top level Vault Dedicated namespace.Check the Vault server status.
Example output:
Validate that your admin token is working to provide correct access to Vault.
You are ready to proceed with managing your Vault Dedicated cluster.
If you deployed a new Vault Dedicated cluster and did not follow the migration lab, apply a sample Vault configuration.
When the cluster creation completes, apply the sample configuration from migration portion of the lab.
Initialize Terraform.
Apply the sample Terraform configuration.
Example output:
Switch back to the
learn-manage-codified-hcp-vault-terraform
directory.
You are now ready to proceed with the lab.
Review Terraform configuration
Examine the current Terraform configuration.
This is a sample Terraform configuration that can be used to deploy an Vault Dedicated cluster.
Note
The configuration included in the part1-config directory was supplied for those who did not attend the Migrating codified self-hosted Vault to HCP Vault Dedicated portion of this lab and want to be able to follow this lab step-by-step.
Examine the main Terraform configuration.
This file defines the HCP provider and the version of the provider that should be used.
main.tfExamine the variables used to define your Vault Dedicated cluster.
The
variables.tf
file is used by other Terraform configurations to gather required inputs for different resources. This is useful because you can reference these values where needed, without having to account for them multiple times in different resources or configuration files.For example, you can define the tier for your Vault Dedicated cluster and when you want to scale the cluster up to another size, you can make the change in one place.
variables.tfExamine the Vault Dedicated configuration.
There are 2 resources required to create an Vault Dedicated cluster - a HashiCorp Virtual Network (HVN), represented in the
hcp_hvn
resource, and the Vault cluster represented in thehcp_vault_cluster
resource.The region you create your HVN in will determine where your Vault cluster is created. Refer to the
variables.tf
file to review the values that will be used.The
time_sleep
andhcp_vault_cluster_admin_token
resources are not required, but provided here as an example for how you might retrieve the initial admin token after provisioning a new Vault Dedicated cluster.hcpvault.tfExamine the Terraform outputs.
Outputs are a useful way to collect and use information from one Terraform configuration and use it in another configuration or to use the information as a reference.
In this example, you may need to know the public URL of your Vault cluster to manually access it or to have another Terraform configuration applied after it is available.
outputs.tf
Review HCP Vault Dedicated deployment
Because you deployed your Vault Dedicated cluster using Terraform, you can easily review details
about the deployment with the show
command.
Run
terraform show
.Example output:
Observe that the cluster tier is currently
DEV
.
Scale up the HCP Vault Dedicated cluster
After migrating your self-hosted Vault cluster to HCP, you need to increase the capacity to support additional applications accessing Vault.
If you needed to scale up a self hosted cluster on your own, you might need to spend weeks or even months planning. For example you might need to validate your on-premises compute resources have enough resources to support larger Vault instances or verify the configuration has a valid CPU and memory configuration that is supported by the underlying infrastructure.
With Vault Dedicated, you can scale your Vault cluster with a change to your variables.tf
file and re-applying the configuration.
The underlying infrastructure and associated managed of that infrastructure is handled by HashiCorp.
Change the tier from
dev
toplus_small
in thevariables.tf
file.Note
Once you scale a cluster is scaled above the
dev
tier, it cannot be scaled back down to thedev
tier.Verify the file file has been updated.
Re-apply the configuration to change the tier.
Example output:
The example output shows that the cluster will be updated in-place. The cluster does not need to be replaced or stopped to change the tier or size of the cluster.
Enter
yes
at the prompt to proceed with scaling the cluster.
Access the HCP Vault Dedicated cluster
One of the many benefits of running your Vault cluster on HCP is most of the infrastructure related maintenance is managed by HashiCorp. While your cluster is scaling, applications that rely on Vault can still access Vault resources.
Log into the HCP Portal and navigate to the Vault clusters list while Terraform is upgrading the cluster.
Observe that your cluster is an
Updating
state.Click
vault-cluster-primary
to access details about the cluster.Information about the cluster is still available while the upgrade is in process.
Click the Public link under Cluster URLs to copy the URL.
Open a new browser tab and navigate to the copied URL. Vault is still accessible even during the upgrade.
Click the Method pull down menu and select Username.
Log in as the
admin
user that was migrated from the self-hosted Vault instance to Vault Dedicated with the passwordsuperS3cret!
.Navigate to Secrets and click api-credentials.
Even though the cluster is in the process of being upgraded, Vault is still available and accessible for incoming requests.
Add performance replication
As the Vault admin, you have successfully scaled your cluster up from the dev tier to the plus tier to support increased Vault utilization. To meet the demands of your applications, you now need to enable performance replication in a different region.
Note
Performance replication is available on all Plus tier HCP Vault Dedicated clusters.
Adding performance replication to an existing cluster involves just a few steps and can be performed on-demand with no downtime. Similar to creating a new Vault Dedicated cluster using Terraform, you will need to define the parameters for your new cluster and associate the new cluster with your primary cluster.
Add the required values for the new replica cluster to the
variables.tf
file.Review the
variables.tf
file.The
variables.tf
file now contains the necessary information for the new cluster.variables.tfUpdate
hcpvault.tf
to add the required resource blocks for the new cluster.Review the
hcpvault.tf
file.The Terraform configuration for Vault Dedicated now includes the resources to create the new cluster. Performance replication is enabled by using the
primary_link
parameter to associate the new cluster with the primary cluster.hcpvault.tfAdd an output for the new clusters URL.
Create the new cluster by re-running
terraform apply
.Example output:
Log into the HCP Portal and navigate to the Vault clusters list. Both the primary and secondary cluster are available.
Unset the
VAULT_TOKEN
environment variable used to authenticate with the primary cluster.Log into the secondary cluster using the Vault CLI.
Verify the status of the secondary Vault cluster.
Example output:
You can verify this is the secondary cluster because the IP address is part of the address space defined for the secondary cluster HVN.
View available secrets engines.
Example output:
All secrets engines were replicated from the primary cluster.
Add a path filter
Now that you have deployed performance replication, you have been informed that the transit
secrets engine
should not have been replicated to the secondary cluster.
You can solve for this by adding a path filter that denies a specific path from being replicated to the secondary cluster.
Add the
paths_filter
parameter to the secondary clusters resource block inhcpvault.tf
.Re-apply the Terraform configuration to prevent the
transit
secrets engine from being replicated to the secondary cluster.Verify that the
transit
secrets engine is no longer available on the secondary cluster.Example output:
The
transit
secrets engine is no longer listed.
Disable performance replication and scale down the cluster
You have successfully scaled your Vault Dedicated cluster from the dev
tier to the plus
tier and enabled
performance replication. During peak business hours your applications have the necessary access to Vault Dedicated.
To help manage cost, you have been asked to scale down your cluster and remove the secondary cluster during non-peak hours.
Removing performance replication requires removing the resources block for the secondary cluster. Similarly,
scaling the cluster down to a smaller tier requires changing the tier value in variables.tf
.
Remove the secondary cluster resources from
hcpvault.tf
.Remove the secondary cluster URL from outputs.tf
Change the tier from
plus_small
tostandard_small
.Re-apply the Terraform configuration.
Example output:
Log into the HCP Portal and navigate to the Vault clusters list. The secondary cluster has been removed and the cluster scaled down from
plus_small
tostandard_small
.
Summary
By using simple, modular, and composable infrastructure as code with Terraform, you can easily manage the lifecycle of your Vault Dedicated clusters.
Clean up
Here are the steps you need to clean up the scenario content from your local environment.
Delete the Vault Dedicated cluster instance and HVN.
Unset the environment variables.