Amazon ElastiCache with Terraform

Nidhi Ashtikar
2 min readAug 19, 2024

--

Caches are in-memory databases with really high performance, low latency.

ElastiCache is to be managed by Redis or Memcached.

Using ElastiCache involves heavy application code changes

  • Aurora: 5432 (if PostgreSQL compatible) or 3306 (if MySQL compatible)

In-Memory Storage: Redis operates entirely in memory, which allows it to deliver extremely fast read and write operations compared to traditional disk-based databases. This makes it ideal for scenarios where performance is critical.

Data Persistence: Although Redis is primarily an in-memory data store, it provides options for data persistence. Data can be periodically saved to disk or written to a log to ensure durability. This balances the speed of in-memory storage with the reliability of persistent storage.

Data Structures: Redis supports a wide range of data types, including:

  • Strings: Basic key-value pairs.
  • Hashes: A collection of key-value pairs, similar to a JSON object.
  • Lists: Ordered collections of strings.
  • Sets: Unordered collections of unique strings.
  • Sorted Sets: Like sets, but each element has a score to define the order.
  • Bitmaps, HyperLogLogs, Streams, and more.

Advanced Features: Redis offers advanced functionalities like pub/sub messaging, transactions, Lua scripting, and geospatial indexing. These features allow Redis to be more than just a simple key-value store, making it versatile for complex applications.

High Availability and Scalability: Redis can be set up in highly available clusters using Redis Sentinel or Redis Cluster. These tools help manage replication, failover, and partitioning to ensure that Redis remains highly available and scalable as workloads grow.

Create Redis Cluster :

resource "aws_elasticache_cluster" "ec" {
cluster_id = "cluster"
engine = "redis"
engine_version = "7.1"
port = 6379
parameter_group_name = aws_elasticache_parameter_group.pg.id
node_type = "cache.m4.large"
num_cache_nodes = 1

security_group_ids = [aws_security_group.sg.id]
}

Create Parameter group for Redis

resource "aws_elasticache_parameter_group" "pg" {
name = "cache-para"
family = "redis2.8"
description = "This is Parameter Group"
parameter {
name = "activerehashing"
value = "yes"
}
}

Security group with 6379 port


resource "aws_security_group" "sg" {
name = "redis-sg"

ingress {
from_port = 6379
to_port = 6379
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"] # Restrict this in production
}

egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}

If you found this guide helpful then do click on 👏 the button.

Follow for more Learning like this 😊

If there’s a specific topic you’re curious about, feel free to drop a personal note or comment. I’m here to help you explore whatever interests you!

Thanks for spending your valuable time learning to enhance your knowledge!

--

--

Nidhi Ashtikar
Nidhi Ashtikar

Written by Nidhi Ashtikar

Experienced AWS DevOps professional with a passion for writing insightful articles.

Responses (2)