Deploying Consul using Terraform
HashiCorp offers a set of official Terraform(opens in new tab) modules to simplify the deployment of a Consul Enterprise environment that align with the requirements and standards outlined in this HashiCorp Validated Design (HVD).
Tip
HashiCorp Professional Services or one of our trusted partners can help accelerate your Consul Enterprise deployment with confidence. For more information, reach out to your HashiCorp account team.Platform-specific guidance
Select the tab below for your cloud for further guidance.
HashiCorp provides an official HVD module to deploy Consul Enterprise on AWS EC2(opens in new tab).
By using this module, you can deploy a complete, end-to-end Consul Enterprise environment in your cloud account using Terraform.
Note
The steps provided offer a detailed, step-by-step method for installing Consul using this Terraform module. This method is intended for testing the module’s functionality and familiarizing yourself with the design. We strongly recommend adapting these steps into a more controlled deployment process, such as using HCP Terraform, Terraform Enterprise, or integrating them into your existing CI/CD pipeline.Warning
NEVER store Terraform state in a version control system or any other unprotected location. Terraform state files contain sensitive information and must be secured. For more information, refer to the Terraform documentation.While we have provided prescriptive best practices throughout this guide, we understand that each organization has unique requirements and constraints for infrastructure deployment. We have included different considerations for deploying Consul in your cloud environment within the context of this module. If the default variables don’t fully meet your needs, the module offers additional capabilities you may want to explore.
Deployment sequence overview
- Create the TLS certificate files.
- Obtain the Consul Enterprise license file.
- Download the Consul CLI.
- Download the Terraform CLI.
- Set up your prerequisite resources.
- Configure your cloud credentials.
- Initialize your Terraform workspace.
- Enter the required variables into the module for deployment.
- Create a Terraform plan.
- Apply the Terraform plan.
- Verify that the cluster has been successfully created and is accessible.
Preparation
Create the TLS certificate files
Generate a standard X.509 certificate to be installed on the Consul servers. Follow your organization’s process for creating a certificate that corresponds to the DNS record users will use to access Consul.
You will need the following three files.
- The certificate:
consul-crt.pem
- The certificate’s private key:
consul-key.pem
- The CA bundle file from the certificate authority:
ca.pem
Keep these files accessible, as they will be required later in the installation process.
Obtain the Consul Enterprise license file
Obtain the Consul Enterprise license file from your HashiCorp account team. This file includes a unique license key for your environment and will typically be named something like consul.hclic
.
Keep this file accessible, as it will be required later in the installation process.
Download and install the Consul CLI
Note
This guide has been tested with Consul 1.19.2+ent.Install the Consul CLI for your platform. You can do it manually by downloading the appropiate binary for your operating system, or using your preferred package manager. You can find instructions for the different options in here(opens in new tab).
Download and install the Terraform CLI
Note
This guide has been tested with Terraform 1.9.6+ent.Install the Terraform CLI for your platform. You can do it manually by downloading the appropiate binary for your operating system, or using your preferred package manager. You can find instructions for the different options in here(opens in new tab).
Configure your cloud credentials.
Once you have downloaded the module, navigate to the examples/default/ directory. Use this as the base working directory during the installation process.
- Credentials file: Typically located at
$HOME/.aws/credentials
(or%UserProfile%\.aws\credentials
on Windows). - Environment variables:
AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY
AWS_SESSION_TOKEN
(if using an IAM role or temporary credentials)AWS_DEFAULT_REGION
For detailed instructions on setting up AWS credentials for Terraform, refer to the Terraform AWS provider documentation(opens in new tab).
Make sure the credentials you use have the necessary permissions to perform the required actions in Terraform.
Installation
Set up your prerequisite resources
This section outlines the necessary infrastructure resources required to successfully execute the Terraform modules. These prerequisites include both infrastructure components and the appropriate storage locations for certificates, credentials, and licenses.
For more details about the required input variables, refer to the specific module's documentation.
HashiCorp provides an official HVD module to deploy Consul Enterprise on AWS EC2(opens in new tab).
Before deployment, you need to set up the required infrastructure in AWS.
- A functional VPC with at least three public and three private subnets.
- A Consul license stored in the AWS Secrets Manager.
- Store the content of the license file in a secret, using a name such as
consul_license_text
. The ARN of the secret will be used as an input to the Terraform module.
- Store the content of the license file in a secret, using a name such as
- A TLS private key and certificate, valid for the fully qualified domain name intended for Consul, stored in the AWS Secrets Manager.
- Store the TLS certificate, private key, and CA bundle as secrets using names like
consul_agent_cert
,consul_agent_key
, andconsul_ca_cert.
The ARNs of these secrets will be used as input variables for the Terraform module.
- Store the TLS certificate, private key, and CA bundle as secrets using names like
- An S3 Bucket for snapshots.
- Ensure the S3 bucket is not publicly accessible and is encrypted.
- SSH key name, already registered in AWS, to use for instance access.
- An gossip encryption key for Consul.
- You can use the Consul CLI’s
consul keygen command
to generate a gossip encryption key.
- You can use the Consul CLI’s
- An initial management token in the UUID format.
- You can generate an initial management token using the Linux/UNIX command
uuidgen | tr "[:upper:]" "[:lower:]"
.
- You can generate an initial management token using the Linux/UNIX command
Initialize your Terraform workspace
Run terraform init
to initialize your Terraform workspace. Review the output to confirm that all providers and modules have been successfully downloaded and that no errors are present before proceeding.
Configure variables for the deployment
Note
You can configure variables in the installation module’s terraform.tfvars file only after all prerequisite resources are set up. Values from the prerequisites will need to be supplied to the Consul module.Create a file named terraform.tfvars
in the examples/amazonlinux-internal-nlb-consul-primary/
directory. Refer to the terraform.tfvars.example
file for explanations of each variable, then populate your own file with the relevant values for your environment.
Create and apply the Terraform plan
In the example directory, generate a Terraform plan using the following command:
terraform plan
Review the output to understand the changes that will be made, then apply the changes with:
terraform apply
When prompted, type yes
to confirm and proceed with the changes.
Verify that the cluster has been successfully created and is accessible
Once the Terraform run completes successfully, you can find the IP addresses of your Consul servers through the AWS console or by using the following command, specifying the name of the created autoscaling group, such as production-consul-0.0.1
.
aws ec2 describe-instances --instance-ids \
$(aws autoscaling describe-auto-scaling-groups \
--auto-scaling-group-name production-consul-0.0.1 \
--query "AutoScalingGroups[].Instances[].InstanceId" \
--output text) \
--query "Reservations[].Instances[].[InstanceId, PrivateIpAddress, PublicIpAddress]" \
--output table
To connect to an EC2 instance using SSH, replace the IP address with the IP of one of the instances returned from the previous command.
ssh ec2-user@<IP-ADDRESS>
To configure the necessary environment variables for Consul, set the following values within your SSH session on the EC2 instance.
export CONSUL_CACERT=/etc/consul.d/tls/consul-ca.pem
export CONSUL_HTTP_ADDR=127.0.0.1:8501
export CONSUL_HTTP_SSL=true
export CONSUL_HTTP_TOKEN=<aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee>
To check the status of the raft peers in your Consul cluster and confirm the setup, run the following command.
consul operator raft list-peers
With the recommended configuration, this command will display the status of the 6 Consul nodes in the cluster.