Integrate your AWS ECS services into Consul service mesh
Consul's multi-platform capabilities allow you to manage and connect your services in a consistent manner across different clouds, platforms, runtimes, and networks.
There are two ways to integrate your ECS tasks with Consul: the Consul ECS Terraform module method and the manual task definition method. This tutorial uses the Consul ECS Terraform module method, which lets you use your existing ECS task definitions so you can quickly integrate them into your Consul service mesh.
In this tutorial, you will extend your Consul service mesh to services running in ECS. In the process, you will learn how using Consul with ECS lets you simplify scaling your services, support different runtimes, and reduce operational overhead.
Scenario overview
HashiCups is a coffee shop demo application. It has a microservices architecture and uses Consul service mesh to securely connect the services. At the beginning of this tutorial, the HashiCups frontend (nginx
, frontend
, and public-api
) will be in the Consul service mesh on AWS EKS. The HashiCups backend (product-api
, product-db
, and payments
) will initially be outside of the Consul service mesh on AWS ECS.
You will connect the HashiCups backend services running on AWS ECS into the service mesh, leveraging Consul's multi-platform capabilities to run HashiCups across two distinct platforms. This enables secure service to service communication no matter where your application workload is running or what platform it runs on.
In this tutorial, you will:
- Deploy the following resources with Terraform:
- Elastic Kubernetes Service (EKS) cluster
- Elastic Container Service (ECS) cluster
- A managed Consul datacenter on HashiCorp Cloud Platform (HCP)
- Investigate demo environment state
- Review Consul ECS Terraform module
- Integrate ECS tasks into Consul service mesh
- Verify all services are in the mesh
Prerequisites
For this tutorial, you will need:
- An AWS account configured for use with Terraform
- An HCP account configured for use with Terraform
- aws-cli >= 2.0
- terraform >= 1.0
- consul >= 1.16.0
- git >= 2.0
- kubectl <= 1.24
Clone GitHub repository
Clone the GitHub repository containing the configuration files and resources.
Change into the directory that contains the complete configuration files for this tutorial.
Deploy infrastructure and demo application
With these Terraform configuration files, you are ready to deploy your infrastructure.
Issue the terraform init
command from your working directory to download the necessary providers and initialize the backend.
Then, deploy the resources. Confirm the run by entering yes
.
Note
The Terraform deployment could take up to 15 minutes to complete. Feel free to explore the next sections of this tutorial while waiting for the environment to complete initialization.
Connect to your infrastructure
Kubernetes stores cluster connection information in a file called kubeconfig
. Retrieve the Kubernetes configuration settings for your EKS cluster and merge them into your local kubeconfig
file by issuing the following command:
Configure your CLI to interact with Consul datacenter
In this section, you will set environment variables in your terminal so your Consul CLI can interact with your Consul datacenter. The Consul CLI reads these environment variables for behavior defaults and will reference these values when you run consul
commands.
Tokens are artifacts in the ACL system used to authenticate users, services, and Consul agents. Since ACLs are enabled in this Consul datacenter, entities requesting access to a resource must include a token that is linked with a policy, service identity, or node identity that grants permission to the resource. The ACL system checks the token and grants or denies access to resources based on the associated permissions. A bootstrap token has unrestricted privileges to all resources and APIs.
Set the Consul ACL token as an environment variable.
Set the Consul destination address. By default, Consul runs on port 8500
for http
and 8501
for https
.
Investigate demo environment state
In this section, you will investigate the current state of your demo environment.
Retrieve the current list of services in the Consul catalog. Notice that only the HashiCups frontend services (nginx
, frontend
, and public-api
) have a corresponding sidecar proxy, indicating they are in the Consul service mesh.
Retrieve the current list of services in ECS. Notice that the HashiCups backend services (product-api
, payments
, and product-db
) are running in ECS, but are not currently integrated into the Consul service mesh.
Retrieve the Consul API Gateway public DNS address.
Open the Consul API Gateway's URL in your browser and notice that only the frontend HashiCups services are available.
This current state is expected since the backend HashiCups services within AWS ECS are not inside the Consul service mesh, but with the native Consul ECS integration you can integrate these ECS services into the mesh.
Review Consul ECS Terraform modules
The native Consul ECS integration allows you to use your existing ECS task container definitions within a Consul ECS Terraform module for simple integration into your Consul service mesh. Feel free to check out the Consul ECS Terraform documentation to learn more.
In this tutorial environment, you will deploy the existing HashiCups services integrated with the Consul ECS Terraform module. Each HashiCups service task definition will be wrapped within its own Consul ECS Terraform module.
Your ECS tasks integrated with Consul communicate with the Consul control plane's gRPC API to receive Envoy configuration information. They also communicate with the Consul control plane's HTTP API, which is the primary interface to all functionality available in Consul. These ports can be specified in your Consul ECS Terraform submodule definitions using the grpc_config
and http_config
inputs. See the HCP Consul Dedicated ports page for port defaults.
Review the Consul ECS Terraform module for the product-api
service. Notice that the module contains both your standard ECS task definition parameters and Consul specific parameters.
In addition to each of these modules, you must deploy the controller
module. The controller provisions secure ACL tokens for all of your Consul service mesh tasks.
Register ECS tasks into Consul service mesh
Replace the contents of ecs-services-and-tasks.tf
with your ECS tasks and services integrated with Consul.
Now that your ECS tasks utilize Consul ECS modules, run terraform init
to initialize these modules.
Run terraform apply
to deploy the HashiCups ECS tasks and services that are now integrated with Consul.Confirm the run by entering yes
.
Verify all services are in the mesh
Retrieve the current list of services in ECS. Notice that the HashiCups backend services (product-api
, payments
, and product-db
) as well as the consul-ecs-controller
are running in ECS and integrated with Consul.
Retrieve the current list of services in the Consul catalog to verify all HashiCups services are now in the Consul service mesh. Notice that all HashiCups services include a sidecar proxy, which indicates that these services are integrated into the Consul service mesh.
Retrieve the Consul API Gateway public DNS address.
Open the Consul API Gateway's URL in your browser and notice that HashiCups is now fully functional.
Clean up resources
Destroy the Terraform resources to clean up your environment. Confirm the destroy operation by inputting yes
.
Note
Due to race conditions with the various cloud resources created in this tutorial, it may be necessary to run the destroy
operation twice to ensure all resources have been properly removed.
Next steps
In this tutorial, you integrated your AWS ECS services into the Consul service mesh. This integration offers simplified application scalability, development platform flexibility, and reduced operational overhead.
For more information about the topics covered in this tutorial, refer to the following resources: