Skip to content

Commit

Permalink
feat: Add support for serverless Redis (#5)
Browse files Browse the repository at this point in the history
* feat: Add support for serverless Redis

* feat: Improve README

* feat: Improve README

---------

Co-authored-by: Leonardo Jardim <[email protected]>
  • Loading branch information
leojaardim and Leonardo Jardim committed Mar 13, 2024
1 parent 4989fb5 commit ad877d5
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 8 deletions.
59 changes: 53 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,37 @@ module "elasticache" {
user_group_id = "app"
}
```
## Serverless

```
module "elastic_serverlesss" {
source = "osgurisdosre/elasticache/aws"
create_elasticache_serveless = true
serveless_name = "redis-serverless"
description_serveless = "Serveless"
enabled_limits = true
cache_usage_limits = {
data_storage = {
maximum = 1000
unit = "GB"
}
ecpu_per_second = {
maximum = 1000
}
}
daily_snapshot_time = "09:00"
kms_key_id = aws_kms_key.test.arn
major_engine_version = "7"
snapshot_retention_limit = 1
security_group_ids = ["sg-0000000000000", "sg-111111111111111"]
subnet_ids = ["subnet-123456789", "subnet-987654321", "subnet-123321123"]
user_group_id = module.user_group.elasticache_user_group_id
tags = {
"Environment" = "prd"
}
}
```
## Conditional creation

```
Expand All @@ -69,10 +100,16 @@ module "elasticache" {
create_elasticache_parameter_group = true
# Enable creation of user
create_elasticache_user = true
create_elasticache_user = true
# Enable creation of user group
create_elasticache_user_group = true
create_elasticache_user_group = true
# Enable creation of serveless
create_elasticache_serveless = true
#Enable modification in cache_usage_limits
enabled_limits = true
}
```

Expand All @@ -84,21 +121,22 @@ To-do:

1. This module does not create Elasticache security group. Use [terraform-aws-security-group](https://github.com/terraform-aws-modules/terraform-aws-security-group) module for this.
2. By default, the variable `create_db_password` is set to true. Therefore, even if the user provides a password, it will not be read. The `create_db_password` variable should be set to false and the `passwords` variable should have a non-null value to be read and used.
3. By default, the `create_elasticache_serveless` variable is set false

<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
## Requirements

| Name | Version |
|------|---------|
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.4 |
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 4.0 |
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 5.40 |
| <a name="requirement_random"></a> [random](#requirement\_random) | >= 3.1 |

## Providers

| Name | Version |
|------|---------|
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 4.0 |
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 5.40 |
| <a name="provider_random"></a> [random](#provider\_random) | >= 3.1 |

## Modules
Expand All @@ -115,6 +153,7 @@ To-do:
| Name | Type |
|------|------|
| [aws_elasticache_replication_group.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/elasticache_replication_group) | resource |
| [aws_elasticache_serverless_cache.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/elasticache_serverless_cache) | resource |
| [random_password.master_password](https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/password) | resource |

## Inputs
Expand All @@ -128,18 +167,23 @@ To-do:
| <a name="input_authentication_mode"></a> [authentication\_mode](#input\_authentication\_mode) | Denotes the user's authentication properties. Possible of type options are: password, no-password-required or iam. | <pre>list(object({<br> type = optional(string)<br> passwords = optional(string)<br> }))</pre> | <pre>[<br> {<br> "passwords": null,<br> "type": null<br> }<br>]</pre> | no |
| <a name="input_auto_minor_version_upgrade"></a> [auto\_minor\_version\_upgrade](#input\_auto\_minor\_version\_upgrade) | Specifies whether minor version engine upgrades will be applied automatically to the underlying | `bool` | `false` | no |
| <a name="input_automatic_failover_enabled"></a> [automatic\_failover\_enabled](#input\_automatic\_failover\_enabled) | Specifies whether a read-only replica will be automatically promoted to read/write primary if the existing primary fails. | `bool` | `true` | no |
| <a name="input_cache_usage_limits"></a> [cache\_usage\_limits](#input\_cache\_usage\_limits) | Sets the cache usage limits for storage and ElastiCache Processing Units for the cache.(data\_storage,ecpu\_per\_second) | `any` | `{}` | no |
| <a name="input_create"></a> [create](#input\_create) | Create the resource? | `bool` | `true` | no |
| <a name="input_create_cache"></a> [create\_cache](#input\_create\_cache) | Create the elasticache Redis? | `bool` | `false` | no |
| <a name="input_create_cache_password"></a> [create\_cache\_password](#input\_create\_cache\_password) | Create the password? | `bool` | `false` | no |
| <a name="input_create_elasticache_parameter_group"></a> [create\_elasticache\_parameter\_group](#input\_create\_elasticache\_parameter\_group) | Create the parameter group? | `bool` | `false` | no |
| <a name="input_create_elasticache_replication_group"></a> [create\_elasticache\_replication\_group](#input\_create\_elasticache\_replication\_group) | Create the replication group? | `bool` | `false` | no |
| <a name="input_create_elasticache_serveless"></a> [create\_elasticache\_serveless](#input\_create\_elasticache\_serveless) | Create the elasticache Redis Serveless? | `bool` | `false` | no |
| <a name="input_create_elasticache_subnet_group"></a> [create\_elasticache\_subnet\_group](#input\_create\_elasticache\_subnet\_group) | Create the subnet group? | `bool` | `false` | no |
| <a name="input_create_elasticache_user"></a> [create\_elasticache\_user](#input\_create\_elasticache\_user) | Create the user? | `bool` | `false` | no |
| <a name="input_create_elasticache_user_group"></a> [create\_elasticache\_user\_group](#input\_create\_elasticache\_user\_group) | Create the user group? | `bool` | `false` | no |
| <a name="input_daily_snapshot_time"></a> [daily\_snapshot\_time](#input\_daily\_snapshot\_time) | The daily time that snapshots will be created from the new serverless cache.(Only for the Serveless module) | `string` | `null` | no |
| <a name="input_data_tiering_enabled"></a> [data\_tiering\_enabled](#input\_data\_tiering\_enabled) | Enables data tiering | `bool` | `false` | no |
| <a name="input_description_parameter"></a> [description\_parameter](#input\_description\_parameter) | The description of the ElastiCache parameter group. | `string` | `null` | no |
| <a name="input_description_redis"></a> [description\_redis](#input\_description\_redis) | User-created description for the replication group. Must not be empty | `string` | `""` | no |
| <a name="input_description_serverless"></a> [description\_serverless](#input\_description\_serverless) | User-created description for the serverless. Must not be empty | `string` | `""` | no |
| <a name="input_description_subnet"></a> [description\_subnet](#input\_description\_subnet) | The Description of the ElastiCache Subnet Group. | `string` | `null` | no |
| <a name="input_enabled_limits"></a> [enabled\_limits](#input\_enabled\_limits) | bla | `bool` | `false` | no |
| <a name="input_engine"></a> [engine](#input\_engine) | Name of the cache engine to be used for the clusters in this replication group. | `string` | `"redis"` | no |
| <a name="input_engine_version"></a> [engine\_version](#input\_engine\_version) | Version number of the cache engine to be used for the cache clusters in this replication group. | `string` | `"6.x"` | no |
| <a name="input_family"></a> [family](#input\_family) | The family of the ElastiCache parameter group. | `string` | `"redis6.x"` | no |
Expand All @@ -148,8 +192,10 @@ To-do:
| <a name="input_kms_key_id"></a> [kms\_key\_id](#input\_kms\_key\_id) | The ARN of the key that you wish to use if encrypting at rest. | `string` | `null` | no |
| <a name="input_log_delivery_configuration"></a> [log\_delivery\_configuration](#input\_log\_delivery\_configuration) | block allows the streaming of Redis SLOWLOG or Redis Engine Log to CloudWatch Log | `list(map(string))` | `[]` | no |
| <a name="input_maintenance_window"></a> [maintenance\_window](#input\_maintenance\_window) | Specifies the weekly time range for when maintenance on the cache cluster is performed. | `string` | `"sat:03:00-sat:06:00"` | no |
| <a name="input_major_engine_version"></a> [major\_engine\_version](#input\_major\_engine\_version) | The version of the cache engine that will be used to create the serverless cache.(Only for the Serveless module) | `string` | `"7"` | no |
| <a name="input_multi_az_enabled"></a> [multi\_az\_enabled](#input\_multi\_az\_enabled) | Specifies whether to enable Multi-AZ Support for the replication group. | `bool` | `false` | no |
| <a name="input_node_type"></a> [node\_type](#input\_node\_type) | Instance class to be used. | `string` | n/a | yes |
| <a name="input_name_serverless"></a> [name\_serverless](#input\_name\_serverless) | Name Redis Serverless. This parameter is stored as a lowercase string. | `string` | `null` | no |
| <a name="input_node_type"></a> [node\_type](#input\_node\_type) | Instance class to be used. | `string` | `null` | no |
| <a name="input_notification_topic_arn"></a> [notification\_topic\_arn](#input\_notification\_topic\_arn) | ARN of an SNS topic to send ElastiCache notifications to | `string` | `null` | no |
| <a name="input_num_cache_clusters"></a> [num\_cache\_clusters](#input\_num\_cache\_clusters) | Number of cache clusters (primary and replicas) this replication group will have. | `number` | `3` | no |
| <a name="input_num_node_groups"></a> [num\_node\_groups](#input\_num\_node\_groups) | Number of node groups (shards) for this Redis replication group. | `number` | `null` | no |
Expand All @@ -161,9 +207,10 @@ To-do:
| <a name="input_preferred_cache_cluster_azs"></a> [preferred\_cache\_cluster\_azs](#input\_preferred\_cache\_cluster\_azs) | List of EC2 availability zones in which the replication group's cache clusters will be created. | `list(string)` | `null` | no |
| <a name="input_random_password_length"></a> [random\_password\_length](#input\_random\_password\_length) | The length of the string desired. | `number` | `16` | no |
| <a name="input_replicas_per_node_group"></a> [replicas\_per\_node\_group](#input\_replicas\_per\_node\_group) | Number of replica nodes in each node group. | `number` | `null` | no |
| <a name="input_replication_group_id"></a> [replication\_group\_id](#input\_replication\_group\_id) | Replication group identifier. This parameter is stored as a lowercase string. | `string` | n/a | yes |
| <a name="input_replication_group_id"></a> [replication\_group\_id](#input\_replication\_group\_id) | Replication group identifier. This parameter is stored as a lowercase string. | `string` | `null` | no |
| <a name="input_security_group_ids"></a> [security\_group\_ids](#input\_security\_group\_ids) | One or more Amazon VPC security groups associated with this replication group. | `list(string)` | `[]` | no |
| <a name="input_security_group_names"></a> [security\_group\_names](#input\_security\_group\_names) | List of cache security group names to associate with this replication group. | `list(string)` | `null` | no |
| <a name="input_snapshot_arns_to_restore"></a> [snapshot\_arns\_to\_restore](#input\_snapshot\_arns\_to\_restore) | The list of ARN(s) of the snapshot that the new serverless cache will be created from(Only for the Serveless module) | `list(string)` | `null` | no |
| <a name="input_snapshot_name"></a> [snapshot\_name](#input\_snapshot\_name) | Name of a snapshot from which to restore data into the new node group. | `string` | `null` | no |
| <a name="input_snapshot_retention_limit"></a> [snapshot\_retention\_limit](#input\_snapshot\_retention\_limit) | Number of days for which ElastiCache will retain automatic cache cluster snapshots before deleting them. | `number` | `7` | no |
| <a name="input_snapshot_window"></a> [snapshot\_window](#input\_snapshot\_window) | Daily time range (in UTC) during which ElastiCache will begin taking a daily snapshot of your cache cluster. | `string` | `"00:00-02:00"` | no |
Expand Down
34 changes: 34 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,37 @@ module "elasticache_user_group" {
user_ids = [var.user_id]
tags = var.tags
}
resource "aws_elasticache_serverless_cache" "this" {
count = var.create_elasticache_serveless ? 1 : 0

engine = var.engine
name = lower(var.name_serverless)
description = var.description_serverless
daily_snapshot_time = var.daily_snapshot_time
kms_key_id = var.kms_key_id
major_engine_version = var.major_engine_version
snapshot_retention_limit = var.snapshot_retention_limit
snapshot_arns_to_restore = var.snapshot_arns_to_restore
security_group_ids = var.security_group_ids
subnet_ids = var.subnet_ids
user_group_id = var.user_group_id
dynamic "cache_usage_limits" {
for_each = var.enabled_limits ? [1] : []
content {
dynamic "data_storage" {
for_each = try(var.cache_usage_limits.data_storage, null) != null ? [1] : []
content {
maximum = var.cache_usage_limits.data_storage.maximum
unit = var.cache_usage_limits.data_storage.unit
}
}
dynamic "ecpu_per_second" {
for_each = try(var.cache_usage_limits.ecpu_per_second, null) != null ? [1] : []
content {
maximum = var.cache_usage_limits.ecpu_per_second.maximum
}
}
}
}
tags = var.tags
}
50 changes: 49 additions & 1 deletion variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ variable "create_cache" {
default = false
description = "Create the elasticache Redis?"
}

variable "create_elasticache_serveless" {
type = bool
default = false
description = "Create the elasticache Redis Serveless?"
}
variable "create_elasticache_replication_group" {
type = bool
default = false
Expand Down Expand Up @@ -46,6 +50,48 @@ variable "create_cache_password" {
description = "Create the password?"
}
##############################################################################################
#Redis Serveless
variable "name_serverless" {
type = string
default = null
description = "Name Redis Serverless. This parameter is stored as a lowercase string."
}

variable "daily_snapshot_time" {
type = string
default = null
description = " The daily time that snapshots will be created from the new serverless cache.(Only for the Serveless module)"
}

variable "snapshot_arns_to_restore" {
type = list(string)
default = null
description = "The list of ARN(s) of the snapshot that the new serverless cache will be created from(Only for the Serveless module)"
}

variable "cache_usage_limits" {
type = any
default = {}
description = "Sets the cache usage limits for storage and ElastiCache Processing Units for the cache.(data_storage,ecpu_per_second)"
}

variable "enabled_limits" {
type = bool
default = false
description = "bla"
}

variable "major_engine_version" {
type = string
default = "7"
description = "The version of the cache engine that will be used to create the serverless cache.(Only for the Serveless module)"
}
variable "description_serverless" {
type = string
default = ""
description = "User-created description for the serverless. Must not be empty"
}
##############################################################################################
#Replication Group
variable "engine" {
type = string
Expand All @@ -55,6 +101,7 @@ variable "engine" {

variable "replication_group_id" {
type = string
default = null
description = "Replication group identifier. This parameter is stored as a lowercase string."
}

Expand All @@ -72,6 +119,7 @@ variable "automatic_failover_enabled" {

variable "node_type" {
type = string
default = null
description = "Instance class to be used."
}
variable "num_cache_clusters" {
Expand Down
2 changes: 1 addition & 1 deletion versions.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = ">= 4.0"
version = ">= 5.40"
}

random = {
Expand Down

0 comments on commit ad877d5

Please sign in to comment.