How To Create Redis Cluster in AWS Using Terraform

In this article we are going to cover How To Create ElastiCache Redis Cluster in AWS Using Terraform | How To Create Redis Cluster in AWS Using Terraform.

In this guide, you’ll discover how to set up a Amazon ElastiCache Redis Cluster using Terraform. The best part is, all the steps we’ll cover can be done without incurring any extra costs because they’re eligible for the Free Tier.

Amazon ElastiCache?


Amazon ElastiCache is a Caching-as-a-Service from Amazon Web Services. AWS simplifies setting up, managing, and scaling a distributed in-memory cache environment in the cloud. It provides a high-performance, scalable, and cost-effective caching solution. AWS removes the complexity associated with deploying and managing a distributed cache environment.

Amazon ElastiCache is a fully managed, in-memory data store service provided by Amazon Web Services (AWS). It is designed to enhance the performance and scalability of applications by providing a managed caching solution. ElastiCache supports two popular in-memory caching engines: Memcached and Redis. Here’s an overview of Amazon ElastiCache and its key features:

Key Features of Amazon ElastiCache:

  1. Managed Caching: ElastiCache handles the complex tasks of setting up, configuring, and managing caching infrastructure. It allows you to focus on your application’s logic without worrying about cache management.
  2. In-Memory Data Stores: ElastiCache supports two popular in-memory data store engines: Memcached: Memcached is a simple and fast caching engine that supports simple key-value data storage and retrieval. It is ideal for use cases where you need high-speed caching with minimal complexity. Redis: Redis is a highly versatile, open-source, in-memory data store that supports more advanced data structures, such as sorted sets, hashes, and lists. It is often used for caching, real-time analytics, and as a message broker.
  3. Scalability: ElastiCache allows you to easily scale your cache cluster vertically by selecting different instance types and horizontally by adding or removing cache nodes. This ensures your cache can handle increased workloads as your application grows.
  4. Automatic Failover: Redis clusters in ElastiCache can be configured with automatic failover. If a primary node fails, ElastiCache automatically promotes a read replica to the primary role, minimizing downtime.
  5. Backups and Restores: ElastiCache provides the ability to take manual snapshots of your cache clusters. You can use these snapshots to restore your cache data or clone cache clusters.

What is ElastiCache Redis in AWS?

Imagine you have a computer program or a website that needs to remember things quickly. It could be information like usernames, passwords, or any data that needs to be accessed rapidly. Now, sometimes, storing and retrieving this information from a regular computer’s memory can take time.
Amazon ElastiCache Redis is a fully managed, in-memory data store service provided by Amazon Web Services (AWS). It is based on the popular open-source Redis database and is designed to deliver fast and scalable performance for real-time applications.

  • A Redis cluster consists of multiple nodes, which are individual instances of the Redis engine. These nodes work together to distribute the data and requests across the cluster.
  • Nodes can have different roles, such as master or replica. Master nodes store the actual data, while replica nodes replicate that data to provide high availability and fault tolerance.
  • Redis is an in-memory data store, meaning that it stores and retrieves data entirely in RAM (Random Access Memory). This results in very low-latency data access, making it suitable for use cases that require quick response times.
  • Redis is built on the popular open-source Redis database. Redis is known for its simplicity, flexibility, and versatility. It supports a variety of data structures, including strings, hashes, lists, sets, and more. This makes it suitable for a wide range of use cases, such as caching, real-time analytics, session storage, and pub/sub messaging.
  • One of the primary use cases for Redis is caching. By storing frequently accessed data in-memory, applications can retrieve it quickly, reducing the need to fetch the same data from slower, disk-based databases. This can significantly improve the overall performance of applications.

Differences between Redis Cache & MemCache?

  1. Data Structures and Features:
    • Redis: Redis is more than just a simple key-value store; it is often referred to as a data structure server. It supports various data types such as strings, hashes, lists, sets, and more. Redis also provides features like transactions, pub/sub (publish/subscribe) messaging, and Lua scripting.
    • Memcached: Memcached, on the other hand, is a straightforward key-value store without the additional data types and features provided by Redis. It is primarily designed for caching purposes.
  2. Persistence:
    • Redis: Redis offers persistence options, allowing you to persist data to disk. This makes Redis suitable for use cases where durability of data is important.
    • Memcached: Memcached is purely an in-memory caching solution and does not provide built-in persistence. This means that if the server is restarted, all data in Memcached is lost.
  3. Use Cases:
    • Redis: Due to its rich feature set, Redis is often used in scenarios that require advanced data structures and operations. It’s commonly used for caching, real-time analytics, leaderboards, and more.
    • Memcached: Memcached is generally used when the primary requirement is a simple key-value store for caching purposes. It is well-suited for scenarios where speed and simplicity are more critical than advanced data structures.


Before you begin, you should have the following prerequisites in place:

  • You’ll need an Amazon Web Services (AWS) account. If you don’t have one, you can sign up for a free AWS account.
  • Basic knowledge of AWS services and concepts.
  • Visual Studio on your system.
  • Hashicorp Terraform Extension. You can download it from the official Terraform website.

How To Create Redis Cluster in AWS Using Terraform

Steps for implementation to this project

  • Let’s create the following organizational structure as shown below.
    • Create a Folder – terraform-redisdb
    • Create 4 Files in our terraform-redisdb – 1)  2) terraform.tfvars 3) and 4)
terraform mariadb files 1
  • In Terraform, the file is used to declare and define variables that can be referenced in your Terraform configuration files (typically files with a .tf extension). These variables act as parameters that allow you to customize your Terraform configuration and provide values dynamically. The file is where you define the variables, their types, and, in some cases, their default values.
  • Variable Declaration: This is where you define your variables using the variable keyword, followed by the variable name. For example:
  • Using file: It helps in making your Terraform configuration more modular and adaptable. By centralizing variable definitions, you can easily manage and update configuration values without having to modify multiple places in your code. It also promotes reusability and clarity in your Terraform projects.
variable "example_variable" {
  type        = string
  description = "An example variable"
  • terraform.tfvars In Terraform, the terraform.tfvars file is used to store input variables and their values in a key-value pair format. This file allows you to set values for variables used in your Terraform configuration without hardcoding them directly in the Terraform files (e.g., .tf files). The name terraform.tfvars is the default filename, but you can also use other filenames and specify them with the -var-file option when running Terraform commands. This separation of variable values makes it easier to manage and share configurations with different values for different environments.
  •– In Terraform, is a default and conventional name for the main configuration file. This file is where you define the infrastructure resources and their configurations using HashiCorp Configuration Language (HCL). The file is essentially the entry point for Terraform to understand how to create, update, and manage your infrastructure. Key points about
    • is a naming convention, not a strict requirement. You can name your main configuration file differently if you prefer, but is widely used and recognized.
    • Inside, you specify the resources you want to create (e.g., AWS instances, databases, networks) and their configurations using Terraform syntax. This includes resource types, attributes, and any other settings required.
    • can also include declarations for variables and outputs. Variables allow you to parameterize your configuration, making it more reusable, while outputs provide a way to expose certain values to be easily queried or used by other systems.
    • Here’s a basic example of what a simple file might look like:

provider "aws" {
  region = "us-east-1"

resource "aws_elasticache_cluster" "example" {
  cluster_id                = "cluster-example"
  engine                     = "redis"
  node_type               = "cache.m4.large"
  num_cache_nodes  = 1
  parameter_group_name = "default.redis3.2"
  engine_version       = "7.1.0"
  port                         = 6379
  tags = {
    Name = "mynewcluster"
  •– In Terraform, the file is used to define output values that can be displayed after the execution of the Terraform configuration. This file allows you to expose specific information about the resources you’ve created or modified during the Terraform run. Outputs are useful for providing useful information or data to users or external systems.
    • In the file, you define one or more output blocks. Each block specifies a named output and the value you want to expose.
    • Using enhances the usability and interoperability of your Terraform configurations by making key information easily accessible. It’s a way to communicate important data between different parts of your infrastructure or share it with users and systems consuming the Terraform outputs.
    • In this example, an output named “example_output” is defined, and its value is set to the ID of an aws_elasticache_cluster created in the Terraform configuration.
output "example_output" {
  value =

Create a file in terraform-redisdb folder

  • Enter the below code
  • Create a file.
variable "access_key" {
    description = "Access key to AWS console"
variable "secret_key" {
    description = "Secret key to AWS console"
variable "region" {
    description = "AWS region"
variables 1

Create a terraform.tfvars file in terraform-redisdb folder

  • Create a terraform.tfvars file.
  • You are defining the dynamic values for the variables declared in file.
region = "us-east-1"
terraform.tfvar 1

Create a file in terraform-redisdb folder

  • Create a file.
#defining the provider as aws
provider "aws" {
    region     = "${var.region}"
    access_key = "${var.access_key}"
    secret_key = "${var.secret_key}"

#create a ElastiCache Redis Cluster
resource "aws_elasticache_cluster" "redis_db" {
  cluster_id           = "redis-db"
  engine               = "redis"
  node_type            = "cache.m4.large"
  num_cache_nodes      = 1
  engine_version       = "7.1.0"
  port                 = 6379
  tags = {
    Name = "myrediscluster"

Create a file in terraform-redisdb folder

  • Create an file.
  • Outputs the ElastiCache Redis endpoint to confirm that they are created.

output "elasticache_cluster_endpoint" {
  value       = aws_elasticache_cluster.redisdb.arn

Initialize and Apply Terraform Configuration

  • Open a terminal in the directory containing your file and run the following commands:
terraform init
  • By running terraform plan, you get a detailed list of these three categories— Additions, Modifications, and Deletions. It’s a way for you to review and confirm that Terraform understands your intentions correctly.
terraform plan
  • Once you’re satisfied with the plan, you can proceed to apply those changes using terraform apply.
  • This two-step process—terraform plan followed by terraform apply—helps prevent unintended or accidental changes to your infrastructure. It gives you a chance to review and approve the proposed changes before Terraform makes any modifications to your environment.
terraform apply -auto-approve
redisapply new
  • Wait for 5-6 minutes till all the resources have been created.
  • Go to ElastiCache Console, Click on the Redis , You can see ElastiCache Redis Cluster created successfully.

Delete AWS Redis Cache Cluster Using Terraform

  • Run this below command to destroy your resources.
terraform destroy


In this article, we’ve learned How to Create Elasticache Redis Cluster in AWS using Terraform | How To Create Redis Cluster in AWS Using Terraform and destroyed it later not to incur charges.


About Sohail Behlim

Hey, I am Sohail Behlim, I am an aspiring DevOps and Cloud enthusiast who is eager to embark on a journey into the world of DevOps and Cloud. With a strong passion for technology and a keen interest in DevOps and Cloud based solutions, I am driven to learn and contribute to the ever-evolving field of DevOps and Cloud.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link