Creating an AWS ElastiCache Redis Cluster Using Terraform

Gias Uddin - Aug 27 - - Dev Community

Creating an AWS ElastiCache Redis Cluster Using Terraform

Terraform simplifies the process of managing AWS resources by treating infrastructure as code (IaC). In this guide, you'll learn how to use Terraform to create an AWS ElastiCache Redis cluster. We'll also set up a custom security group to control access to the Redis cluster.

Prerequisites

Before you start, ensure you have the following:

  • AWS Account: An active AWS account with the necessary permissions.
  • Terraform Installed: Terraform should be installed and configured on your local machine.
  • AWS Access Key and Secret Key: You'll need these to authenticate Terraform with AWS.

Steps to Create the ElastiCache Redis Cluster

1. Define the AWS Provider

Start by defining the AWS provider. This configuration tells Terraform to interact with AWS resources in the specified region using your credentials.

provider "aws" {
  region     = "ap-southeast-2"
  access_key = "your-access-key"  # Replace with your actual AWS access key
  secret_key = "your-secret-key"  # Replace with your actual AWS secret key
}
Enter fullscreen mode Exit fullscreen mode

2. Create a Security Group

Next, create a security group to control network access to your Redis cluster. This security group will allow inbound traffic on the Redis default port (6379).

resource "aws_security_group" "redis_sg" {
  name        = "redis-security-group"
  description = "Security group for Redis cluster"

  ingress {
    from_port   = 6379
    to_port     = 6379
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]  # Open to all; consider restricting to specific IPs for better security
  }
}
Enter fullscreen mode Exit fullscreen mode

3. Create the ElastiCache Redis Cluster

Now, define the ElastiCache Redis cluster. You'll specify the Redis version, instance type, number of nodes, and the security group associated with the cluster.

resource "aws_elasticache_cluster" "pte-dev-redis" {
  cluster_id           = "pte-dev"
  engine               = "redis"
  node_type            = "cache.t3.micro"  # Choose a suitable instance type based on your needs
  num_cache_nodes      = 1
  parameter_group_name = "default.redis7"  # Using Redis 7.0 parameter group
  engine_version       = "7.0"             # Specify the Redis engine version
  apply_immediately    = true
  port                 = 6379

  security_group_ids   = [aws_security_group.redis_sg.id]  # Associate the Redis cluster with the custom security group
}
Enter fullscreen mode Exit fullscreen mode

Final redis.tf file will look like-

#Create a user in aws for performing this action

provider "aws" {
  region = "ap-southeast-2"
  access_key = "xxxxxxxxxxx"
  secret_key = "xxxxx"
}

resource "aws_security_group" "redis_sg" {
  name        = "redis-security-group"
  description = "Security group for Redis cluster"

  // Define your security group rules here, e.g., allowing access from specific IP ranges
  ingress {
    from_port   = 6379
    to_port     = 6379
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]  // Open to all; customize for your needs
  }
}

resource "aws_elasticache_cluster" "pte-dev-redis" {
  cluster_id           = "pte-dev"
  engine               = "redis"
  node_type            = "cache.t3.micro"
  num_cache_nodes      = 1
  parameter_group_name = "default.redis7"
  engine_version       = "7.0"
  apply_immediately    = true
  port                 = 6379
    // Associate the Redis cluster with the custom security group
  security_group_ids       = [aws_security_group.redis_sg.id]
}
Enter fullscreen mode Exit fullscreen mode

4. Initialize and Apply the Terraform Configuration

With your Terraform configuration ready, follow these steps to deploy the Redis cluster on AWS:

  • Initialize Terraform:
  terraform init
Enter fullscreen mode Exit fullscreen mode
  • Create an Execution Plan:
  terraform plan 
Enter fullscreen mode Exit fullscreen mode
  • Apply the Plan:
  terraform apply 
Enter fullscreen mode Exit fullscreen mode

This will create the ElastiCache Redis cluster as specified in your Terraform configuration. The cluster will be secured using the custom security group, which controls access to the Redis instance.

5. Accessing the Redis Cluster

Once the cluster is up and running, you can access it via the endpoint provided in the AWS Management Console or through the Terraform output if configured. Ensure your security group is properly configured to allow access from your application or other clients.

6. Clean Up Resources

If you no longer need the Redis cluster and want to avoid incurring costs, you can destroy the resources created by Terraform:

terraform destroy
Enter fullscreen mode Exit fullscreen mode

This command will delete the Redis cluster and associated resources, such as the security group, from your AWS account.

Conclusion

Using Terraform to create an AWS ElastiCache Redis cluster streamlines the process of setting up and managing your infrastructure. By defining your cluster and its associated resources in a Terraform configuration file, you can easily recreate, modify, and destroy your infrastructure as needed. This approach ensures consistency, repeatability, and version control for your cloud resources.

For production environments, consider adding more advanced features such as Redis replication, backup configurations, and enhanced security rules. Terraform’s flexibility allows you to manage these aspects efficiently within your infrastructure-as-code workflow.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player