Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored redis module #24

Merged
merged 11 commits into from
Feb 22, 2024
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ AWS services that you can create:
- **SecretsManager**
- **RDS**
- **Elasticsearch** (Opensearch)
- **Redis** (Elasticache)

## Steps to init a new project

Expand Down
16 changes: 14 additions & 2 deletions clients/sample/sample.tfvars
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ projects = [
// apply_immediately = true
//}
]
elasticsearch = []
redis = []
},
{
project_name = "frontend"
Expand All @@ -41,6 +41,18 @@ projects = [
ebs_enabled = true
volume_size = 40
}
redis = [
{
identifier = "frontend-cluster"
engine = "redis"
engine_version = "7.0"
node_type = "cache.t4g.micro"
num_cache_nodes = 1
parameter_group = "default.redis7"
apply_immediately = true
prevent_destroy = true
}
]
}]
}]
}
]
1 change: 1 addition & 0 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ module "projects" {
repository_branch = each.value.repository_branch
databases = each.value.databases
elasticsearch = each.value.elasticsearch
redis = each.value.redis

# Common
owner = var.owner
Expand Down
18 changes: 18 additions & 0 deletions modules/projects/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -247,4 +247,22 @@ module "elasticsearch" {
environment = var.environment
project = var.project_name
owner = var.owner
}
module "redis" {
source = "../../modules/redis"
for_each = {for cluster in var.redis: cluster.identifier => cluster}

identifier = each.value.identifier
engine = each.value.engine
engine_version = each.value.engine_version
project = var.project_name
owner = var.owner
aws_vpc_id = var.aws_vpc_id
environment = var.environment
node_type = each.value.node_type
num_cache_nodes = each.value.num_cache_nodes
parameter_group = each.value.parameter_group
subnet_ids = var.aws_private_subnets
sg_ids = [aws_security_group.app.id, aws_security_group.codebuild.id]
apply_immediately = each.value.apply_immediately
}
1 change: 1 addition & 0 deletions modules/projects/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ variable repository_url {}
variable repository_branch {}
variable databases {}
variable elasticsearch {}
variable redis {}

# Common
variable owner {}
Expand Down
4 changes: 2 additions & 2 deletions modules/rds/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ resource "aws_db_subnet_group" "sg" {

resource "aws_rds_cluster_instance" "instances" {
count = 1
identifier = format("%s-%s-%s-instance%s", "${var.owner}", "${var.project}", "${var.environment}", "${count.index}")
identifier = format("%s-%s", "${var.identifier}", "${count.index}")
cluster_identifier = aws_rds_cluster.cluster.id
instance_class = var.instance_class
engine = aws_rds_cluster.cluster.engine
Expand All @@ -32,7 +32,7 @@ resource "aws_rds_cluster_instance" "instances" {
}

resource "aws_rds_cluster" "cluster" {
cluster_identifier = format("%s-%s-%s-cluster", "${var.owner}", "${var.project}", "${var.environment}")
cluster_identifier = var.identifier
engine = var.engine
engine_version = var.engine_version
database_name = var.project
Expand Down
2 changes: 1 addition & 1 deletion modules/rds/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ variable "master_password" {}
variable "master_username" {}
variable "skip_final_snapshot" {}
variable "instance_class" {}
variable "apply_immediately" {}
variable "apply_immediately" {}
46 changes: 46 additions & 0 deletions modules/redis/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
resource "aws_security_group" "redis" {
name = format("%s-%s-%s-redis", "${var.owner}", "${var.project}", "${var.environment}")
vpc_id = var.aws_vpc_id
}

resource "aws_security_group_rule" "app_inbound" {
type = "ingress"
from_port = 6379
to_port = 6379
protocol = "tcp"
security_group_id = aws_security_group.redis.id
source_security_group_id = var.sg_ids[0]
description = "APP to Redis"
}

resource "aws_security_group_rule" "codebuild_inbound" {
type = "ingress"
from_port = 6379
to_port = 6379
protocol = "tcp"
security_group_id = aws_security_group.redis.id
source_security_group_id = var.sg_ids[1]
description = "Codebuild to Redis"
}

resource "aws_elasticache_subnet_group" "redis" {
name = format("%s-%s-redis-subnets", "${var.project}", "${var.environment}")
subnet_ids = [var.subnet_ids[0], var.subnet_ids[1], var.subnet_ids[2]]
}

resource "aws_elasticache_cluster" "redis" {
cluster_id = var.identifier
engine = var.engine
node_type = var.node_type
num_cache_nodes = var.num_cache_nodes
parameter_group_name = var.parameter_group
engine_version = var.engine_version
port = 6379
security_group_ids = [ aws_security_group.redis.id ]
subnet_group_name = aws_elasticache_subnet_group.redis.name
apply_immediately = var.apply_immediately

lifecycle {
prevent_destroy = true
}
}
13 changes: 13 additions & 0 deletions modules/redis/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
variable "identifier" {}
variable "owner" {}
variable "project" {}
variable "environment" {}
variable "engine" {}
variable "engine_version" {}
variable "node_type" {}
variable "num_cache_nodes" {}
variable "parameter_group" {}
variable "subnet_ids" {}
variable "sg_ids" {}
variable "aws_vpc_id" {}
variable "apply_immediately" {}
10 changes: 10 additions & 0 deletions vars.tf
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,18 @@ variable projects {
ebs_enabled = bool
volume_size = number
})
redis = list(object({
identifier = string
engine = string
engine_version = string
node_type = string
num_cache_nodes = number
parameter_group = string
apply_immediately = bool
prevent_destroy = bool
}))
}))
}))
}

variable "owner" {
Expand Down