Building an Elasticache cluster on AWS using Terraform Modules
This article is a practical implementation of Terraform Modules for building an ElastiCache cluster on AWS. Building on the previous article, I had written as an introduction to Terraform Modules.
To demonstrate how modules work in real life, we’ll be building an ElastiCache cluster for multiple environments like dev, staging and production. To create the same using modules, we would have to write a separate dev, stage and production configuration in Terraform. Although this would be a laborious activity with a likelihood of discrepancies in the configurations of the three environments, using Terraform modules would ensure that the base configuration for the cluster would always be the same, thereby enabling us to maintain dev-prod parity.
Defining the Folder Structure
In building the ElastiCache module, we need to define the folder and file structure where we will store our module files. For instance, the folder structure would look something like the one given below:
The folder named elasticache
will be the place we’ll use to store the main.tf
and variables.tf
files comprising the configuration for the elasticache
cluster. The environments
folder has the folders corresponding to the environment names. The main.tf
file will use the elasticache
module and update the properties based on the need of the environment in question. The environments will be used as environments in the root main.tf
file. Finally, the cloud provider for our project is defined using the provider.tf
, which happens to be aws
in our case.
Writing the Reusable Module
To use a module between multiple environments, first, we need to write a reusable terraform elasticache
module.
Before we define the elasticache module, we need to write the variables that will be used by the elasticache
module, using the following configuration in the elasticache/variables.tf
file:
The elasticache/main.tf
file contains the configuration for the elasticache module as defined below:
Writing the Modules for Environments
Now that we have defined our elasticache module, we can create the modules for the dev, stage and production environments. We have previously described a folder environment for the same, and under the folder, we have created folders with the names of the environments. In each of the folders, we’ll have a main.tf
file that uses the elasticache
module and later, we will use these modules in the main.tf
file present in the root.
First, we will define the dev elasticache
cluster in the environment/dev
folder in a main.tf
file, with the following configuration:
We have named the module dev-elasticache
, the source
points to the elasticache
folder, and we set the values for the environment
, node_count
, node_type
, and avaiability_zones
as per the requirements of the dev environment.
Next up, we’ll define the staging elasticache
cluster in the environment/staging
folder in a main.tf
file, with the following configuration:
We have named the module staging-elasticache. The source points to the elasticache folder. The environment
, node_count
, node_type
, and avaiability_zones
are values set at the staging environment’s requirements.
Next up, we’ll define the production elasticache
cluster in the environment/production
folder in a main.tf
file, with the following configuration:
We have named the module production-elasticache
, the source points to the elasticache
folder, and we set the values of the environment
, node_count
, node_type
, and avaiability_zones
as per the requirements of the production environment.
Using the Modules in the root main.tf
We have defined the modules, and now we can use the modules in our main.tf
file located at the directory’s root. We can define the main.tf
file as given below:
Finally, we need to define the provider in our providers.tf
file with the following configuration:
With a little effort, we can restructure Terraform code from the beginning so that a growing code base won’t bring growing pains. Writing reusable and configurable modules are one of the basic building blocks of clean, readable and scalable Terraform code. Writing modules would help us better manage a growing code base and reduce useless repetition in code.
You can find the codebase supporting this article on Github AWS ElastiCache Terraform Module.
Dive Deeper: Recommended Reads
Expand your knowledge of Infrastructure as Code and Terraform with our insightful collection of articles! Dive into a range of topics that will help you master the art of managing infrastructure:
- Terraform Best Practices: Learn the most effective ways to use Terraform in your projects.
- Managing environments through Terraform Workspaces: Discover how to manage multiple environments with ease.
- Building highly available VMSS on Azure using Terraform Modules: Create scalable and highly available virtual machine scale sets on Azure.
- Demystifying Terraform Modules: Understand the ins and outs of Terraform modules.
- Building an Nginx web server on Azure using Terraform: Deploy a reliable Nginx web server on Azure.
- Building an Nginx web server on AWS using Terraform: Set up an Nginx web server on AWS with Terraform.
- Introduction to Infrastructure as Code (IaC): Get started with Infrastructure as Code and grasp the fundamentals.
- Deploying an Azure Kubernetes Service (AKS) Cluster with Terraform: Deploy an Azure Kubernetes Service (AKS) cluster seamlessly with Terraform’s infrastructure management capabilities.
- Building an EKS Cluster on AWS with Terraform: A Step-by-Step Guide: Spin an Amazon EKS cluster effortlessly using Terraform, following our detailed step-by-step guide.
- Create a GKE Cluster on Google Cloud Platform using Terraform: Create and manage a GKE cluster on Google Cloud Platform with ease using Terraform’s automation features.
Embrace the power of Terraform and Infrastructure as Code with this comprehensive collection of articles, and enhance your skills in deploying, managing, and maintaining your infrastructure.
Subscribe to Faizan Bashir
Get the latest posts delivered right to your inbox