Skip to content

Commit 9f22e13

Browse files
authored
Merge pull request #14 from SPHTech-Platform/main-sync-to-valkey
Main branch synt to valkey
2 parents 2e041f7 + 6d30333 commit 9f22e13

File tree

6 files changed

+262
-16
lines changed

6 files changed

+262
-16
lines changed

README.md

+25-4
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@
44
| Name | Version |
55
|------|---------|
66
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.0 |
7+
78
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 5.73.0 |
9+
| <a name="requirement_awscc"></a> [awscc](#requirement\_awscc) | >= 0.67.0 |
10+
811

912
## Providers
1013

1114
| Name | Version |
1215
|------|---------|
13-
| <a name="provider_aws"></a> [aws](#provider\_aws) | 5.82.2 |
16+
| <a name="provider_aws"></a> [aws](#provider\_aws) | 5.82.0 |
17+
| <a name="provider_awscc"></a> [awscc](#provider\_awscc) | 1.24.0 |
1418

1519
## Modules
1620

@@ -22,46 +26,63 @@ No modules.
2226
|------|------|
2327
| [aws_cloudwatch_metric_alarm.cache_cpu](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_metric_alarm) | resource |
2428
| [aws_cloudwatch_metric_alarm.cache_memory](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_metric_alarm) | resource |
29+
| [aws_cloudwatch_metric_alarm.cache_serverless_data](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_metric_alarm) | resource |
30+
| [aws_cloudwatch_metric_alarm.cache_serverless_ecpu](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_metric_alarm) | resource |
31+
| [aws_cloudwatch_metric_alarm.cache_serverless_throttled_commands](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_metric_alarm) | resource |
2532
| [aws_elasticache_parameter_group.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/elasticache_parameter_group) | resource |
2633
| [aws_elasticache_replication_group.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/elasticache_replication_group) | resource |
2734
| [aws_elasticache_subnet_group.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/elasticache_subnet_group) | resource |
35+
| [awscc_elasticache_serverless_cache.this](https://registry.terraform.io/providers/hashicorp/awscc/latest/docs/resources/elasticache_serverless_cache) | resource |
2836

2937
## Inputs
3038

3139
| Name | Description | Type | Default | Required |
3240
|------|-------------|------|---------|:--------:|
3341
| <a name="input_alarm_actions"></a> [alarm\_actions](#input\_alarm\_actions) | The list of actions to execute when this alarm transitions into an ALARM state from any other state. | `list(string)` | `[]` | no |
3442
| <a name="input_alarm_cpu_threshold_percent"></a> [alarm\_cpu\_threshold\_percent](#input\_alarm\_cpu\_threshold\_percent) | CPU threshold alarm level | `number` | `75` | no |
43+
| <a name="input_alarm_data_threshold_percent"></a> [alarm\_data\_threshold\_percent](#input\_alarm\_data\_threshold\_percent) | Data threshold alarm level for elasticache serverless | `number` | `75` | no |
44+
| <a name="input_alarm_ecpu_threshold_percent"></a> [alarm\_ecpu\_threshold\_percent](#input\_alarm\_ecpu\_threshold\_percent) | ECPU threshold alarm level for elasticache serverless | `number` | `75` | no |
3545
| <a name="input_alarm_memory_threshold_bytes"></a> [alarm\_memory\_threshold\_bytes](#input\_alarm\_memory\_threshold\_bytes) | Alarm memory threshold bytes | `number` | `10000000` | no |
3646
| <a name="input_apply_immediately"></a> [apply\_immediately](#input\_apply\_immediately) | Specifies whether any database modifications are applied immediately, or during the next maintenance window | `bool` | `true` | no |
47+
| <a name="input_at_rest_encryption_enabled"></a> [at\_rest\_encryption\_enabled](#input\_at\_rest\_encryption\_enabled) | Specifies whether the encryption at rest is enabled | `bool` | `true` | no |
3748
| <a name="input_auth_token"></a> [auth\_token](#input\_auth\_token) | Password used to access a password protected server. Can be specified only if `transit_encryption_enabled = true` | `string` | `null` | no |
3849
| <a name="input_cluster_id"></a> [cluster\_id](#input\_cluster\_id) | Cluster ID | `string` | `null` | no |
3950
| <a name="input_cluster_mode_enabled"></a> [cluster\_mode\_enabled](#input\_cluster\_mode\_enabled) | Set to false to diable cluster module | `bool` | `false` | no |
4051
| <a name="input_cluster_size"></a> [cluster\_size](#input\_cluster\_size) | Cluster size | `number` | `1` | no |
4152
| <a name="input_create_elasticache_subnet_group"></a> [create\_elasticache\_subnet\_group](#input\_create\_elasticache\_subnet\_group) | Create Elasticache Subnet Group | `bool` | `true` | no |
53+
| <a name="input_daily_snapshot_time"></a> [daily\_snapshot\_time](#input\_daily\_snapshot\_time) | The daily time range (in UTC) during which the service takes automatic snapshot of the Serverless Cache | `string` | `"18:00"` | no |
4254
| <a name="input_elasticache_parameter_group_family"></a> [elasticache\_parameter\_group\_family](#input\_elasticache\_parameter\_group\_family) | ElastiCache parameter group family | `string` | `"redis7"` | no |
4355
| <a name="input_enabled"></a> [enabled](#input\_enabled) | Set to false to prevent the module from creating any resources | `bool` | `true` | no |
4456
| <a name="input_engine"></a> [engine](#input\_engine) | Engine of the elasticache (valkey or redis) | `string` | `"redis"` | no |
4557
| <a name="input_engine_version"></a> [engine\_version](#input\_engine\_version) | Engine version. https://docs.aws.amazon.com/AmazonElastiCache/latest/red-ug/supported-engine-versions.html | `string` | `"7.0"` | no |
4658
| <a name="input_instance_type"></a> [instance\_type](#input\_instance\_type) | Elastic cache instance type | `string` | `"cache.t2.micro"` | no |
4759
| <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. If not supplied, uses service managed encryption. Can be specified only if `at_rest_encryption_enabled = true` | `string` | `null` | no |
4860
| <a name="input_maintenance_window"></a> [maintenance\_window](#input\_maintenance\_window) | Maintenance window | `string` | `"wed:03:00-wed:04:00"` | no |
61+
| <a name="input_max_data_storage"></a> [max\_data\_storage](#input\_max\_data\_storage) | The maximun cached data capacity of the Serverless Cache in GB | `number` | `10` | no |
62+
| <a name="input_max_ecpu_per_second"></a> [max\_ecpu\_per\_second](#input\_max\_ecpu\_per\_second) | The maximum ECPU per second of the Serverless Cache | `number` | `1000` | no |
4963
| <a name="input_name"></a> [name](#input\_name) | Name of the application | `string` | `"value"` | no |
5064
| <a name="input_notification_topic_arn"></a> [notification\_topic\_arn](#input\_notification\_topic\_arn) | ARN of an SNS topic to send ElastiCache notifications | `string` | `""` | no |
5165
| <a name="input_num_node_groups"></a> [num\_node\_groups](#input\_num\_node\_groups) | Number of node groups (shards) for this Redis replication group. Changing this number will trigger an online resizing operation before other settings modifications. Required unless `global_replication_group_id` is set | `number` | `2` | no |
5266
| <a name="input_ok_actions"></a> [ok\_actions](#input\_ok\_actions) | The list of actions to execute when this alarm transitions into an OK state from any other state. | `list(string)` | `[]` | no |
53-
| <a name="input_parameter_group_name"></a> [parameter\_group\_name](#input\_parameter\_group\_name) | Excisting Parameter Group name | `string` | `""` | no |
54-
| <a name="input_parameters"></a> [parameters](#input\_parameters) | A list of Redis parameters to apply. Note that parameters may differ from one Redis family to another | <pre>list(object({<br> name = string<br> value = string<br> }))</pre> | `[]` | no |
67+
| <a name="input_parameter_group_name"></a> [parameter\_group\_name](#input\_parameter\_group\_name) | Existing Parameter Group name | `string` | `""` | no |
68+
| <a name="input_parameters"></a> [parameters](#input\_parameters) | A list of Redis parameters to apply. Note that parameters may differ from one Redis family to another | <pre>list(object({<br/> name = string<br/> value = string<br/> }))</pre> | `[]` | no |
5569
| <a name="input_port"></a> [port](#input\_port) | Redis port | `number` | `6379` | no |
56-
| <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. The order of the availability zones in the list is considered. The first item in the list will be the primary node. Ignored when updating | `list(string)` | <pre>[<br> "ap-southeast-1a",<br> "ap-southeast-1b"<br>]</pre> | no |
70+
| <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. The order of the availability zones in the list is considered. The first item in the list will be the primary node. Ignored when updating | `list(string)` | <pre>[<br/> "ap-southeast-1a",<br/> "ap-southeast-1b"<br/>]</pre> | no |
5771
| <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. Valid values are 0 to 5. Changing this number will trigger an online resizing operation before other settings modifications. | `number` | `1` | no |
5872
| <a name="input_replication_enabled"></a> [replication\_enabled](#input\_replication\_enabled) | Set to false to diable replication in redis cluster | `bool` | `false` | no |
5973
| <a name="input_replication_group_id"></a> [replication\_group\_id](#input\_replication\_group\_id) | ElastiCache replication\_group\_id | `string` | `""` | no |
6074
| <a name="input_security_groups"></a> [security\_groups](#input\_security\_groups) | List of Security Group IDs to place the cluster into | `list(string)` | `[]` | no |
75+
| <a name="input_snapshot_arns"></a> [snapshot\_arns](#input\_snapshot\_arns) | The ARN of the snapshot from which to restore data into the new node group (shard) | `list(string)` | `[]` | no |
76+
| <a name="input_snapshot_arns_to_restore"></a> [snapshot\_arns\_to\_restore](#input\_snapshot\_arns\_to\_restore) | The ARN's of snapshot to restore Serverless Cache | `list(string)` | `[]` | no |
77+
| <a name="input_snapshot_name"></a> [snapshot\_name](#input\_snapshot\_name) | The name of the snapshot from which to restore data into the new node group (shard) | `string` | `""` | no |
6178
| <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. 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 snapshot\_retention\_limit is set to zero (0), backups are turned off. Please note that setting a snapshot\_retention\_limit is not supported on cache.t1.micro cache nodes | `number` | `5` | no |
79+
| <a name="input_snapshot_window"></a> [snapshot\_window](#input\_snapshot\_window) | The daily time range (in UTC) during which ElastiCache begins taking a daily snapshot of the node group (shard) specified by SnapshottingClusterId | `string` | `"00:00-01:00"` | no |
6280
| <a name="input_subnet_group_name"></a> [subnet\_group\_name](#input\_subnet\_group\_name) | Subnet group name for the ElastiCache instance | `string` | `""` | no |
6381
| <a name="input_subnets"></a> [subnets](#input\_subnets) | AWS subnet ids | `list(string)` | `[]` | no |
6482
| <a name="input_tags"></a> [tags](#input\_tags) | Additional tags (\_e.g.\_ map("BusinessUnit","ABC") | `map(string)` | `{}` | no |
83+
| <a name="input_transit_encryption_enabled"></a> [transit\_encryption\_enabled](#input\_transit\_encryption\_enabled) | Specifies whether the encryption at transit is enabled | `bool` | `true` | no |
84+
| <a name="input_use_serverless"></a> [use\_serverless](#input\_use\_serverless) | Use serverless ElastiCache service | `bool` | `false` | no |
85+
| <a name="input_user_group_id"></a> [user\_group\_id](#input\_user\_group\_id) | The ID of the user group Elasticache | `string` | `""` | no |
6586

6687
## Outputs
6788

alarms.tf

+95-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
resource "aws_cloudwatch_metric_alarm" "cache_cpu" {
2-
count = var.enabled ? local.num_nodes : 0
2+
count = var.enabled && !var.use_serverless ? local.num_nodes : 0
33

44
alarm_name = "${tolist(aws_elasticache_replication_group.this[0].member_clusters)[count.index]}-cpu-utilization"
55
alarm_description = "${var.engine} cluster CPU utilization"
@@ -30,7 +30,7 @@ resource "aws_cloudwatch_metric_alarm" "cache_cpu" {
3030
}
3131

3232
resource "aws_cloudwatch_metric_alarm" "cache_memory" {
33-
count = var.enabled ? local.num_nodes : 0
33+
count = var.enabled && !var.use_serverless ? local.num_nodes : 0
3434

3535
alarm_name = "${tolist(aws_elasticache_replication_group.this[0].member_clusters)[count.index]}-freeable-memory"
3636
alarm_description = "${var.engine} cluster freeable memory"
@@ -59,3 +59,96 @@ resource "aws_cloudwatch_metric_alarm" "cache_memory" {
5959
aws_elasticache_replication_group.this
6060
]
6161
}
62+
63+
# ElastiCache Serverless
64+
resource "aws_cloudwatch_metric_alarm" "cache_serverless_ecpu" {
65+
count = var.enabled && var.use_serverless ? 1 : 0
66+
67+
alarm_name = "${awscc_elasticache_serverless_cache.this[0].serverless_cache_name}-ecpu-utilization"
68+
alarm_description = "Redis serverless ECPU utilization"
69+
70+
comparison_operator = "GreaterThanThreshold"
71+
evaluation_periods = 1
72+
73+
metric_name = "ElastiCacheProcessingUnits"
74+
namespace = "AWS/ElastiCache"
75+
76+
period = 300
77+
statistic = "Average"
78+
79+
tags = var.tags
80+
81+
threshold = ceil(var.max_ecpu_per_second * var.alarm_ecpu_threshold_percent / 100)
82+
83+
dimensions = {
84+
CacheClusterId = awscc_elasticache_serverless_cache.this[0].serverless_cache_name
85+
}
86+
87+
alarm_actions = var.alarm_actions
88+
ok_actions = var.ok_actions
89+
90+
depends_on = [
91+
awscc_elasticache_serverless_cache.this
92+
]
93+
}
94+
95+
resource "aws_cloudwatch_metric_alarm" "cache_serverless_data" {
96+
count = var.enabled && var.use_serverless ? 1 : 0
97+
98+
alarm_name = "${awscc_elasticache_serverless_cache.this[0].serverless_cache_name}-data-storage"
99+
alarm_description = "Redis serverless data storage"
100+
101+
comparison_operator = "GreaterThanThreshold"
102+
evaluation_periods = 1
103+
104+
metric_name = "BytesUsedForCache"
105+
namespace = "AWS/ElastiCache"
106+
107+
period = 60
108+
statistic = "Average"
109+
110+
threshold = ceil((var.max_data_storage * 1000 * 1000 * 1000) * var.alarm_data_threshold_percent / 100)
111+
112+
tags = var.tags
113+
114+
dimensions = {
115+
CacheClusterId = awscc_elasticache_serverless_cache.this[0].serverless_cache_name
116+
}
117+
118+
alarm_actions = var.alarm_actions
119+
ok_actions = var.ok_actions
120+
121+
depends_on = [
122+
aws_elasticache_replication_group.this
123+
]
124+
}
125+
126+
resource "aws_cloudwatch_metric_alarm" "cache_serverless_throttled_commands" {
127+
count = var.enabled && var.use_serverless ? 1 : 0
128+
129+
alarm_name = "${awscc_elasticache_serverless_cache.this[0].serverless_cache_name}-throttled-commands"
130+
alarm_description = "Redis serverless throttled commands"
131+
132+
comparison_operator = "GreaterThanThreshold"
133+
evaluation_periods = 1
134+
135+
metric_name = "ThrottledCmds"
136+
namespace = "AWS/ElastiCache"
137+
138+
period = 60
139+
statistic = "Average"
140+
141+
threshold = 0
142+
143+
tags = var.tags
144+
dimensions = {
145+
CacheClusterId = awscc_elasticache_serverless_cache.this[0].serverless_cache_name
146+
}
147+
148+
alarm_actions = var.alarm_actions
149+
ok_actions = var.ok_actions
150+
151+
depends_on = [
152+
awscc_elasticache_serverless_cache.this
153+
]
154+
}

main.tf

+51-6
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ locals {
1515
}
1616

1717
resource "aws_elasticache_parameter_group" "this" {
18-
count = var.enabled && var.parameter_group_name == "" || var.parameter_group_name == null ? 1 : 0
18+
count = var.enabled && var.parameter_group_name == "" && !var.use_serverless || var.parameter_group_name == null ? 1 : 0
1919

2020
name = var.name
2121
family = var.elasticache_parameter_group_family
@@ -38,7 +38,7 @@ resource "aws_elasticache_subnet_group" "this" {
3838
}
3939

4040
resource "aws_elasticache_replication_group" "this" {
41-
count = var.enabled ? 1 : 0
41+
count = var.enabled && !var.use_serverless ? 1 : 0
4242

4343
replication_group_id = var.replication_group_id == "" ? local.cluster_id : var.replication_group_id
4444
description = "${var.engine} Cluster Rep"
@@ -56,20 +56,65 @@ resource "aws_elasticache_replication_group" "this" {
5656
subnet_group_name = try(aws_elasticache_subnet_group.this[0].name, var.subnet_group_name)
5757
security_group_ids = var.security_groups
5858

59-
multi_az_enabled = var.replication_enabled ? true : false
60-
at_rest_encryption_enabled = true
59+
multi_az_enabled = var.replication_enabled ? true : false
60+
61+
at_rest_encryption_enabled = var.at_rest_encryption_enabled
6162
transit_encryption_enabled = var.transit_encryption_enabled
6263
automatic_failover_enabled = var.replication_enabled ? true : false
6364

6465
notification_topic_arn = var.notification_topic_arn
6566

6667
apply_immediately = var.apply_immediately
6768

68-
auth_token = var.auth_token
69-
kms_key_id = var.kms_key_id
69+
auth_token = var.transit_encryption_enabled ? var.auth_token : null
70+
kms_key_id = var.at_rest_encryption_enabled ? var.kms_key_id : null
7071

7172
num_node_groups = var.cluster_mode_enabled ? var.num_node_groups : null
7273
replicas_per_node_group = var.cluster_mode_enabled ? var.replicas_per_node_group : null
7374

75+
user_group_ids = [var.user_group_id]
76+
77+
snapshot_retention_limit = var.instance_type != "cache.t1.micro" ? var.snapshot_retention_limit : 0
78+
snapshot_window = var.snapshot_window
79+
snapshot_arns = var.snapshot_arns
80+
snapshot_name = var.snapshot_name
81+
7482
tags = var.tags
7583
}
84+
85+
resource "awscc_elasticache_serverless_cache" "this" {
86+
count = var.enabled && var.use_serverless ? 1 : 0
87+
88+
serverless_cache_name = var.name
89+
description = "${var.name} ElastiCache Redis Serverless"
90+
engine = "redis"
91+
major_engine_version = var.engine_version
92+
93+
cache_usage_limits = {
94+
data_storage = {
95+
maximum = var.max_data_storage
96+
unit = "GB"
97+
}
98+
ecpu_per_second = {
99+
maximum = var.max_ecpu_per_second
100+
}
101+
}
102+
103+
user_group_id = var.user_group_id
104+
105+
final_snapshot_name = "${var.name}-elasticache-serverless-final-snapshot"
106+
kms_key_id = var.kms_key_id
107+
security_group_ids = var.security_groups
108+
subnet_ids = var.subnets
109+
110+
daily_snapshot_time = var.daily_snapshot_time
111+
snapshot_arns_to_restore = var.snapshot_arns_to_restore
112+
snapshot_retention_limit = var.snapshot_retention_limit
113+
114+
tags = [
115+
for key, value in var.tags : {
116+
key = key
117+
value = value
118+
}
119+
]
120+
}

outputs.tf

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
output "endpoint" {
2-
description = "primary or configuration endpoint, whichever is appropriate for the given cluster mode"
3-
value = try(aws_elasticache_replication_group.this[0].primary_endpoint_address, null)
2+
description = "Redis primary or configuration endpoint, whichever is appropriate for the given cluster mode"
3+
value = var.use_serverless ? try(awscc_elasticache_serverless_cache.this[0].endpoint.address, null) : try(aws_elasticache_replication_group.this[0].primary_endpoint_address, null)
44
}
55

66
output "reader_endpoint_address" {
@@ -15,7 +15,7 @@ output "member_clusters" {
1515

1616
output "arn" {
1717
description = "Elasticache Replication Group ARN"
18-
value = try(aws_elasticache_replication_group.this[0].arn, null)
18+
value = var.use_serverless ? try(awscc_elasticache_serverless_cache.this[0].arn, null) : try(aws_elasticache_replication_group.this[0].arn, null)
1919
}
2020

2121
output "cluster_enabled" {

0 commit comments

Comments
 (0)