Bài này chúng ta sẽ cài đặt redis trên AWS thông qua terraform.
Mình thao khảo bài viết bên dưới.
https://viblo.asia/p/aws-elasticache-provisioning-with-terraform-x7Z4D6xoLnX
Chúng ta bắt tay tôi
1) VPC and anything is related to redis
Đầu tiên bạn cần tạo VPC nếu bạn chưa có VPC và subnet
và bạn cũng phải tạo cho nó một subnet group

>>>>>>>>>>>
>>>>vpc.tf
>>>>>>>>>>>>
# Create VPC Terraform Module
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "3.11.0"
#version = "~> 3.11"
# VPC Basic Details
name = local.redis_cluster_name
cidr = var.vpc_cidr_block
azs = data.aws_availability_zones.available.names
public_subnets = var.vpc_public_subnets
private_subnets = var.vpc_private_subnets
#Elasticache Subnets
elasticache_subnets = var.vpc_elasticache_subnets
create_elasticache_subnet_group = var.vpc_create_elasticache_subnet_group
create_elasticache_subnet_route_table = var.vpc_create_elasticache_subnet_route_table
# create_elasticache_internet_gateway_route = true
# create_elasticache_nat_gateway_route = true
# NAT Gateways - Outbound Communication
enable_nat_gateway = var.vpc_enable_nat_gateway
single_nat_gateway = var.vpc_single_nat_gateway
# VPC DNS Parameters
enable_dns_hostnames = true
enable_dns_support = true
tags = local.common_tags
vpc_tags = local.common_tags
# Additional Tags to Subnets
public_subnet_tags = {
Type = "Public Subnets"
"kubernetes.io/role/elb" = 1
"kubernetes.io/cluster/${local.redis_cluster_name}" = "shared"
}
private_subnet_tags = {
Type = "private-subnets"
"kubernetes.io/role/internal-elb" = 1
"kubernetes.io/cluster/${local.redis_cluster_name}" = "shared"
}
elasticache_subnet_tags = {
Type = "database-subnets"
}
}
data "aws_availability_zones" "available" {}
resource "aws_elasticache_subnet_group" "redis" {
name = "nimtechnology-cache-subnet"
subnet_ids = module.vpc.elasticache_subnets
}
Hình trên AWS:


Tạo security Group:
#
# Security group resources
#
resource "aws_security_group" "redis" {
vpc_id = module.vpc.vpc_id
ingress {
from_port = 6379
to_port = 6379
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
#https://stackoverflow.com/questions/43980946/define-tags-in-central-section-in-terraform
tags = merge(
local.common_tags,
tomap({
"Name" = "sgCacheCluster" ##look into
})
)
# the "map" function was deprecated in Terraform v0.12
# tags = merge(
# local.common_tags,
# map(
# "Name", "sgCacheCluster",
# "Project", var.project,
# )
# )
lifecycle {
create_before_destroy = true
}
}

2) Amazon ElastiCache
2.1) Redis Cluster Mode Disabled
Đa số trong thực tế thì mình hay gặp con redis Redis Cluster Mode Disabled trên nhiều môi trường
Ở dạng deploy này, ta sẽ có một Redis Cluster chỉ có 1 Shard với số lượng node của Shard là 1 tới 6 node.


>>>>>>>>>>>
>>>>redis.tf
>>>>>>>>>>>>>>>>>>>>>
# Input Variables
# AWS Region
variable "aws_region" {
description = "Region in which AWS Resources to be created"
type = string
default = "us-east-1"
}
variable "env" {
description = "Environment in which AWS Resources to be created"
type = string
default = "develop"
}
variable "family" {
type = string
default = "redis5.0"
}
locals {
name = "nimtechnology"
common_tags = {
Component = "nimtechnology"
Environment = var.env
}
redis_cluster_name = "${local.name}-${var.cluster_name}"
}
variable "cluster_name" {
default = "aws-redis"
}
# VPC CIDR Block
variable "vpc_cidr_block" {
description = "VPC CIDR Block"
type = string
default = "10.0.0.0/16"
}
# VPC Database Subnets
variable "vpc_elasticache_subnets" {
description = "VPC Redis Subnets"
type = list(string)
default = ["10.0.151.0/24", "10.0.152.0/24"]
}
# VPC Create Database Subnet Group (True / False)
variable "vpc_create_elasticache_subnet_group" {
description = "VPC Create Redis Subnet Group"
type = bool
default = true
}
# VPC Create Database Subnet Route Table (True or False)
variable "vpc_create_elasticache_subnet_route_table" {
description = "VPC Create Redis Subnet Route Table"
type = bool
default = true
}
# VPC Public Subnets
variable "vpc_public_subnets" {
description = "VPC Public Subnets"
type = list(string)
default = ["10.0.101.0/24", "10.0.102.0/24"]
}
# VPC Private Subnets
variable "vpc_private_subnets" {
description = "VPC Private Subnets"
type = list(string)
default = ["10.0.1.0/24", "10.0.2.0/24"]
}
# VPC Enable NAT Gateway (True or False)
variable "vpc_enable_nat_gateway" {
description = "Enable NAT Gateways for Private Subnets Outbound Communication"
type = bool
default = true
}
# VPC Single NAT Gateway (True or False)
variable "vpc_single_nat_gateway" {
description = "Enable only single NAT Gateway in one Availability Zone to save costs during our demos"
type = bool
default = true
}
variable "cache_identifier" {
default = "redis-cluster-mode-enable-nimtechnology"
}
variable "automatic_failover_enabled" {
default = true
}
variable "multi_az_enabled" {
default = true
}
variable "alarm_cpu_threshold" {
default = "75"
}
variable "desired_clusters" {
default = "3"
}
variable "instance_type" {
default = "cache.t2.micro"
}
variable "engine_version" {
default = "5.0.6"
}
variable "maintenance_window" {
default = "sun:02:30-sun:03:30"
}
variable "at_rest_encryption_enabled" {
type = bool
default = true
description = "Enable encryption at rest"
}
variable "transit_encryption_enabled" {
type = bool
default = true
description = <<-EOT
Set `true` to enable encryption in transit. Forced `true` if `var.auth_token` is set.
If this is enabled, use the [following guide](https://docs.aws.amazon.com/AmazonElastiCache/latest/red-ug/in-transit-encryption.html#connect-tls) to access redis.
EOT
}
variable "sns_topic_name" {
type = string
default = "Unknown"
}
và cuối cùng ta file variable.tf
# Input Variables
# AWS Region
variable "aws_region" {
description = "Region in which AWS Resources to be created"
type = string
default = "us-east-1"
}
variable "env" {
description = "Environment in which AWS Resources to be created"
type = string
default = "develop"
}
variable "family" {
type = string
default = "redis5.0"
}
locals {
name = "nimtechnology"
common_tags = {
Component = "nimtechnology"
Environment = var.env
}
redis_cluster_name = "${local.name}-${var.cluster_name}"
}
variable "cluster_name" {
default = "aws-redis"
}
# VPC CIDR Block
variable "vpc_cidr_block" {
description = "VPC CIDR Block"
type = string
default = "10.0.0.0/16"
}
# VPC Database Subnets
variable "vpc_elasticache_subnets" {
description = "VPC Redis Subnets"
type = list(string)
default = ["10.0.151.0/24", "10.0.152.0/24"]
}
# VPC Create Database Subnet Group (True / False)
variable "vpc_create_elasticache_subnet_group" {
description = "VPC Create Redis Subnet Group"
type = bool
default = true
}
# VPC Create Database Subnet Route Table (True or False)
variable "vpc_create_elasticache_subnet_route_table" {
description = "VPC Create Redis Subnet Route Table"
type = bool
default = true
}
# VPC Public Subnets
variable "vpc_public_subnets" {
description = "VPC Public Subnets"
type = list(string)
default = ["10.0.101.0/24", "10.0.102.0/24"]
}
# VPC Private Subnets
variable "vpc_private_subnets" {
description = "VPC Private Subnets"
type = list(string)
default = ["10.0.1.0/24", "10.0.2.0/24"]
}
# VPC Enable NAT Gateway (True or False)
variable "vpc_enable_nat_gateway" {
description = "Enable NAT Gateways for Private Subnets Outbound Communication"
type = bool
default = true
}
# VPC Single NAT Gateway (True or False)
variable "vpc_single_nat_gateway" {
description = "Enable only single NAT Gateway in one Availability Zone to save costs during our demos"
type = bool
default = true
}
variable "cache_identifier" {
default = "redis-cluster-mode-enable-nimtechnology"
}
variable "automatic_failover_enabled" {
default = true
}
variable "multi_az_enabled" {
default = true
}
variable "alarm_cpu_threshold" {
default = "75"
}
variable "desired_clusters" {
default = "3"
}
variable "instance_type" {
default = "cache.t2.micro"
}
variable "engine_version" {
default = "5.0.6"
}
variable "maintenance_window" {
default = "sun:02:30-sun:03:30"
}
variable "at_rest_encryption_enabled" {
type = bool
default = true
description = "Enable encryption at rest"
}
variable "transit_encryption_enabled" {
type = bool
default = true
description = <<-EOT
Set `true` to enable encryption in transit. Forced `true` if `var.auth_token` is set.
If this is enabled, use the [following guide](https://docs.aws.amazon.com/AmazonElastiCache/latest/red-ug/in-transit-encryption.html#connect-tls) to access redis.
EOT
}
variable "sns_topic_name" {
type = string
default = "Unknown"
}
Và đây là thành quả.

