kuttleio / terraform-aws-environment

Kuttle - Blueprint - Main
https://ktl.ai
Apache License 2.0
0 stars 0 forks source link

Feature request: add cache with ElastiCache service #64

Open dspv opened 11 months ago

dspv commented 11 months ago

Story

We want to create a simple Elasticache cluster based on an input similar to MySQL / Postgres

Input

    cache = {
      name        = "cache"
      type        = "cache"
      engine      = "redis"
      version     = "" // need to clarify
      class       = "burstable"
      instance    = "cache.t4g.micro"
      autoscaling = "disabled"
    }
dspv commented 11 months ago

Code example

locals {
  region_name_bits   = split("-", var.clp_region)
  short_region_name  = "${local.region_name_bits[0]}${substr(local.region_name_bits[1], 0, 1)}${substr(local.region_name_bits[2], 0, 1)}"
  name_prefix        = "${local.short_region_name}-${var.clp_account}"
  standard_tags      = merge(var.global_tags, var.env_tags, tomap({
    Service          = "ElastiCache"
    ServiceComponent = "Redis"
  }))

  redis_version   = "5.0.5"
  redis_node_type = "cache.t3.micro"
}

resource aws_elasticache_replication_group redis {
  replication_group_id          = "${local.name_prefix}-${var.clp_wenv}"
  replication_group_description = "ElastiCache replication group for ${local.name_prefix}-${var.clp_wenv}"
  number_cache_clusters         = var.redis_clusters
  node_type                     = local.redis_node_type
  automatic_failover_enabled    = var.redis_failover
  engine_version                = local.redis_version
  port                          = var.redis_port
  parameter_group_name          = aws_elasticache_parameter_group.redis_parameter_group.id
  subnet_group_name             = aws_elasticache_subnet_group.redis_subnet_group.id
  security_group_ids            = [data.terraform_remote_state.sg.outputs.redis_sg]
  apply_immediately             = var.apply_immediately
  maintenance_window            = var.redis_maintenance_window
  snapshot_window               = var.redis_snapshot_window
  snapshot_retention_limit      = var.redis_snapshot_retention_limit
  tags                          = local.standard_tags
}

resource aws_elasticache_parameter_group redis_parameter_group {
  name        = "${local.name_prefix}-${var.clp_wenv}"
  description = "ElastiCache parameter group for ${local.name_prefix}-${var.clp_wenv}"
  family      = "redis${replace(local.redis_version, "/\\.[\\d]+$/","")}"
  tags        = local.standard_tags
}

resource aws_elasticache_subnet_group redis_subnet_group {
  name        = "${local.name_prefix}-${var.clp_wenv}"
  subnet_ids  = data.terraform_remote_state.vpc.outputs.private_subnets
  tags        = local.standard_tags
}
dspv commented 11 months ago

Default var values

variable redis_port {
  type    = number
  default = 6379
}

variable apply_immediately {
  description = "If need to apply changes immediately, or during the next maintenance window. Default is false."
  default     = "false"
}

variable allowed_cidr {
  type        = list
  default     = ["127.0.0.1/32"]
  description = "A list of Security Group ID's to allow access to."
}

variable redis_clusters {
  type        = number
  description = "Number of nodes to create"
  default     = 1
}

variable redis_failover {
  default = false
}

variable redis_parameters {
  type        = list
  description = "Additional parameters modifyed in parameter group"
  default     = []
}

variable redis_maintenance_window {
  description = "Specifies the weekly time range for when maintenance on the cache cluster is performed. The format is ddd:hh24:mi-ddd:hh24:mi (24H Clock UTC). The minimum maintenance window is a 60 minute period"
  default     = "fri:08:00-fri:09:00"
}

variable redis_snapshot_window {
  description = "The daily time range (in UTC) during which ElastiCache will begin taking a daily snapshot of your cache cluster. The minimum snapshot window is a 60 minute period"
  default     = "06:30-07:30"
}

variable redis_snapshot_retention_limit {
  description = "The number of days for which ElastiCache will retain automatic cache cluster snapshots before deleting them. For example, if you set SnapshotRetentionLimit to 5, then a snapshot that was taken today will be retained for 5 days before being deleted. If the value of SnapshotRetentionLimit is set to zero (0), backups are turned off. Please note that setting a snapshot_retention_limit is not supported on cache.t1.micro or cache.t2.* cache nodes"
  default     = 0
}