Deploy HCP Consul Dedicated with EC2 using Terraform
In the previous tutorial, you learned how to deploy a new HCP Consul Dedicated cluster and to deploy your workload in an EKS run time created in the same operation with Terraform.
In this tutorial, you will learn how to deploy a new HCP Consul Dedicated cluster and deploy your workload, a demo application, in an EC2 instance, created in the same operation with Terraform. The Terraform module will use Nomad to automate the application deployment.
Prerequisites
To complete this tutorial you will need the following.
Basic command line access
Terraform v1.0.0+ CLI installed
Git installed
Admin access to the HashiCorp Cloud Platform (HCP) Consul portal
Note
HCP
Admin
access is necessary to create the Service Principal credentials used by Terraform to interact with HCP. If you already have a Service Principal key and client id provided by your admin, you only requireContributor
access. If you are anAdmin
and would like to create a Service Principal, check Deploy HCP Consul Dedicated with Terraform tutorial for instructions on how to create a Service Principal.An AWS account and AWS Access Credentials configured locally.
You can configure the AWS credentials using environment variables.
Generate Terraform template
You can generate a Terraform template for this example directly from the Overview page in your HCP organization.
Authenticate Terraform to HCP
To authenticate Terraform to HCP you need a Service Principal with Contributor
permissions. If you are logged with an Admin
account you can create one during
this step.
In the Authenticate Terraform to HCP section click on the Generate Service Principal and Key.
HCP will generate a new set of credentials for you and you can copy them using the Copy code button and export them in your terminal.
Note
If you are not an Admin
on HCP you should contact your
administrator and obtain valid Service Principal credentials before proceeding
with the tutorial.
Get Terraform code
Once you have filled in all the options in the bottom side of the page you will find the generated Terraform code.
Click on Copy code to copy it to your clipboard and save it in a file named main.tf
.
Note
Content should resemble the example below. This example is not guaranteed to be up to date. Always refer to the template file provided by HCP UI after the configuration.
Locals
The values you provided in the UI during the creation are used as local variables in the generated Terraform code.
vpc_region
- This is the region where you deployed your VPC.hvn_region
- The HashiCorp Virtual Network (HVN) region.cluster_id
- The HCP Consul Dedicated cluster ID. Use a unique name to identify your HCP Consul Dedicated cluster. HCP will pre-populate it with a name following the patternconsul-quickstart-<unique-ID>
.vpc_id
- Since you are using an existing VPC you need to instruct Terraform on the VPC ID to be able to use it.public_route_table_id
- A route table contains a set of rules, called routes, that are used to determine where network traffic from your subnet or gateway is directed.public_subnet1
- A subnet is a range of IP addresses in your VPC. You can launch AWS resources into a specific subnet.
Note
The HCP Consul Dedicated UI helps you fill in these fields and locate the values you need from your AWS console. Click on the Where can I find this? links in the UI to get help in locating the right values.
Run terraform
With the Terraform manifest files and your custom credentials file, you are now ready to deploy your infrastructure.
Check that the following setup is complete before executing the terraform init
step:
- Your AWS credentials are populated as environment variables and Terraform install is complete (refer to prerequisites)
- You have exported the HCP credentials from the UI as environment variables
- If you are deploying into an existing VPC, ensure the subnet has internet connectivity.
Issue the terraform init
command from your working directory to download the
necessary providers and initialize the backend.
Once Terraform has been initialized, you can verify the resources that will
be created using the plan
command.
Finally, you can deploy the resources using the apply
command.
Remember to confirm the run by entering yes
.
Once you confirm, it will take a few minutes to complete the deploy
. Terraform will print the following output if the deployment is successful.
Examine Terraform output
At the end of the execution Terraform will output the following lines:
As you can notice the consul_root_token
is not showed since is a sensitive value.
You can retrieve it using:
Verify created resources
Consul UI
Visit the Consul UI using the consul_url
link in the output values.
Login in Consul using the token retrieved in the previous step and verify the services are all present in your Consul datacenter UI
Consul CLI configuration
Using the Terraform output values you can setup your Consul CLI to connect to the datacenter you created.
Setup environment variables:
Verify Consul can connect to the datacenter:
Example output:
HashiCups application
The Terraform code deployed an application that exposes a web UI accessible
using the hashicups_url
URL.
You can access the configurations of the deployed Hashicups app services here.
Nomad
From the output you can notice the presence of a nomad_ec2_id
element.
This is because the deploy of the application is performed using Nomad on an EC2
node with Docker installed.
The usage of Nomad is also evident on the Services tab of your Consul UI where the
services show 3 instances of service nomad
, representing the Nomad servers,
and one instance of service nomad-client
.
You can access the Nomad UI using the hashicups_url
and changing the port to
8081
.
So in the example output provided you will use http://ec2-3-9-13-130.eu-west-2.compute.amazonaws.com:8081
as URL.
To login into the Nomad instance use nomad
as username and the consul_root_token
as
the password.
Cleanup environment
Use the terraform destroy
command to clean up the resources you created.
Remember to confirm by entering yes
.
Once you confirm, it will take a few minutes to complete the removal. Terraform will print the following output if the command is successful.
Next steps
In this tutorial you learned how to use Terraform to deploy a demo application on AWS EC2 instances using HCP Consul Dedicated as your service mesh.
In the next tutorial you will use Terraform to deploy a demo application on AWS ECS instances using HCP Consul Dedicated as your service mesh.
If you encounter any issues, please contact the HCP team at support.hashicorp.com.