Skip to content

Commit 8a92dac

Browse files
authored
Merge pull request #1 from SPHTech-Platform/init
adding initial changes of the redis module
2 parents 5ad62b1 + 0ad26c0 commit 8a92dac

File tree

10 files changed

+548
-0
lines changed

10 files changed

+548
-0
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2022-present SPH Media
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,83 @@
11
# Terraform Modules Template
2+
3+
<!-- BEGIN_TF_DOCS -->
4+
## Requirements
5+
6+
| Name | Version |
7+
|------|---------|
8+
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.0 |
9+
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 4.0 |
10+
11+
## Providers
12+
13+
| Name | Version |
14+
|------|---------|
15+
| <a name="provider_aws"></a> [aws](#provider\_aws) | 4.18.0 |
16+
17+
## Modules
18+
19+
No modules.
20+
21+
## Resources
22+
23+
| Name | Type |
24+
|------|------|
25+
| [aws_cloudwatch_metric_alarm.cache_cpu](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_metric_alarm) | resource |
26+
| [aws_cloudwatch_metric_alarm.cache_memory](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_metric_alarm) | resource |
27+
| [aws_elasticache_parameter_group.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/elasticache_parameter_group) | resource |
28+
| [aws_elasticache_replication_group.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/elasticache_replication_group) | resource |
29+
| [aws_elasticache_subnet_group.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/elasticache_subnet_group) | resource |
30+
31+
## Inputs
32+
33+
| Name | Description | Type | Default | Required |
34+
|------|-------------|------|---------|:--------:|
35+
| <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 |
36+
| <a name="input_alarm_cpu_threshold_percent"></a> [alarm\_cpu\_threshold\_percent](#input\_alarm\_cpu\_threshold\_percent) | CPU threshold alarm level | `number` | `75` | no |
37+
| <a name="input_alarm_memory_threshold_bytes"></a> [alarm\_memory\_threshold\_bytes](#input\_alarm\_memory\_threshold\_bytes) | Alarm memory threshold bytes | `number` | `10000000` | no |
38+
| <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 |
39+
| <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 |
40+
| <a name="input_cluster_id"></a> [cluster\_id](#input\_cluster\_id) | Cluster ID | `string` | `null` | no |
41+
| <a name="input_cluster_mode_enabled"></a> [cluster\_mode\_enabled](#input\_cluster\_mode\_enabled) | Set to false to diable cluster module | `bool` | `false` | no |
42+
| <a name="input_cluster_size"></a> [cluster\_size](#input\_cluster\_size) | Cluster size | `number` | `1` | no |
43+
| <a name="input_create_elasticache_subnet_group"></a> [create\_elasticache\_subnet\_group](#input\_create\_elasticache\_subnet\_group) | Create Elasticache Subnet Group | `bool` | `false` | no |
44+
| <a name="input_elasticache_parameter_group_family"></a> [elasticache\_parameter\_group\_family](#input\_elasticache\_parameter\_group\_family) | ElastiCache parameter group family | `string` | `"memcached1.6"` | no |
45+
| <a name="input_enabled"></a> [enabled](#input\_enabled) | Set to false to prevent the module from creating any resources | `bool` | `true` | no |
46+
| <a name="input_engine_version"></a> [engine\_version](#input\_engine\_version) | Memcached engine version. For more info, see https://docs.aws.amazon.com/AmazonElastiCache/latest/mem-ug/supported-engine-versions.html | `string` | `"1.6.6"` | no |
47+
| <a name="input_instance_type"></a> [instance\_type](#input\_instance\_type) | Elastic cache instance type | `string` | `"cache.t2.micro"` | no |
48+
| <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 |
49+
| <a name="input_maintenance_window"></a> [maintenance\_window](#input\_maintenance\_window) | Maintenance window | `string` | `"wed:03:00-wed:04:00"` | no |
50+
| <a name="input_name"></a> [name](#input\_name) | Name of the application | `string` | `"value"` | no |
51+
| <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 |
52+
| <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 |
53+
| <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 |
54+
| <a name="input_parameter_group_name"></a> [parameter\_group\_name](#input\_parameter\_group\_name) | Excisting Parameter Group name | `string` | `""` | no |
55+
| <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 |
56+
| <a name="input_port"></a> [port](#input\_port) | Redis port | `number` | `6379` | no |
57+
| <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 |
58+
| <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 |
59+
| <a name="input_replication_enabled"></a> [replication\_enabled](#input\_replication\_enabled) | Set to false to diable replication in redis cluster | `bool` | `false` | no |
60+
| <a name="input_replication_group_id"></a> [replication\_group\_id](#input\_replication\_group\_id) | ElastiCache replication\_group\_id | `string` | `""` | no |
61+
| <a name="input_security_groups"></a> [security\_groups](#input\_security\_groups) | List of Security Group IDs to place the cluster into | `list(string)` | `[]` | no |
62+
| <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 |
63+
| <a name="input_subnet_group_name"></a> [subnet\_group\_name](#input\_subnet\_group\_name) | Subnet group name for the ElastiCache instance | `string` | `""` | no |
64+
| <a name="input_subnets"></a> [subnets](#input\_subnets) | AWS subnet ids | `list(string)` | `[]` | no |
65+
| <a name="input_tags"></a> [tags](#input\_tags) | Additional tags (\_e.g.\_ map("BusinessUnit","ABC") | `map(string)` | `{}` | no |
66+
67+
## Outputs
68+
69+
| Name | Description |
70+
|------|-------------|
71+
| <a name="output_arn"></a> [arn](#output\_arn) | Elasticache Replication Group ARN |
72+
| <a name="output_cluster_enabled"></a> [cluster\_enabled](#output\_cluster\_enabled) | Indicates if cluster mode is enabled. |
73+
| <a name="output_configuration_endpoint_address"></a> [configuration\_endpoint\_address](#output\_configuration\_endpoint\_address) | Address of the replication group configuration endpoint when cluster mode is enabled. |
74+
| <a name="output_endpoint"></a> [endpoint](#output\_endpoint) | Redis primary or configuration endpoint, whichever is appropriate for the given cluster mode |
75+
| <a name="output_engine_version_actual"></a> [engine\_version\_actual](#output\_engine\_version\_actual) | Because ElastiCache pulls the latest minor or patch for a version, this attribute returns the running version of the cache engine. |
76+
| <a name="output_id"></a> [id](#output\_id) | ID of the ElastiCache Replication Group. |
77+
| <a name="output_member_clusters"></a> [member\_clusters](#output\_member\_clusters) | Redis cluster members |
78+
| <a name="output_parameter_group_arn"></a> [parameter\_group\_arn](#output\_parameter\_group\_arn) | The AWS ARN associated with the parameter group. |
79+
| <a name="output_parameter_group_id"></a> [parameter\_group\_id](#output\_parameter\_group\_id) | The ElastiCache parameter group name. |
80+
| <a name="output_reader_endpoint_address"></a> [reader\_endpoint\_address](#output\_reader\_endpoint\_address) | The address of the endpoint for the reader node in the replication group, if the cluster mode is disabled |
81+
| <a name="output_subnet_group_name"></a> [subnet\_group\_name](#output\_subnet\_group\_name) | The Name of the ElastiCache Subnet Group. |
82+
| <a name="output_subnet_group_subnet_ids"></a> [subnet\_group\_subnet\_ids](#output\_subnet\_group\_subnet\_ids) | The Subnet IDs of the ElastiCache Subnet Group. |
83+
<!-- END_TF_DOCS -->

alarms.tf

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
resource "aws_cloudwatch_metric_alarm" "cache_cpu" {
2+
count = var.enabled ? 1 : 0
3+
4+
alarm_name = "${local.cluster_id}-cpu-utilization"
5+
alarm_description = "Redis cluster CPU utilization"
6+
7+
comparison_operator = "GreaterThanThreshold"
8+
evaluation_periods = 1
9+
10+
metric_name = "CPUUtilization"
11+
namespace = "AWS/ElastiCache"
12+
13+
period = 300
14+
statistic = "Average"
15+
16+
tags = var.tags
17+
18+
threshold = var.alarm_cpu_threshold_percent
19+
20+
dimensions = {
21+
CacheClusterId = local.cluster_id
22+
}
23+
24+
alarm_actions = var.alarm_actions
25+
ok_actions = var.ok_actions
26+
27+
depends_on = [
28+
aws_elasticache_replication_group.this
29+
]
30+
}
31+
32+
resource "aws_cloudwatch_metric_alarm" "cache_memory" {
33+
count = var.enabled ? 1 : 0
34+
35+
alarm_name = "${local.cluster_id}-freeable-memory"
36+
alarm_description = "Redis cluster freeable memory"
37+
38+
comparison_operator = "LessThanThreshold"
39+
evaluation_periods = 1
40+
41+
metric_name = "FreeableMemory"
42+
namespace = "AWS/ElastiCache"
43+
44+
period = 60
45+
statistic = "Average"
46+
47+
threshold = var.alarm_memory_threshold_bytes
48+
49+
tags = var.tags
50+
51+
dimensions = {
52+
CacheClusterId = local.cluster_id
53+
}
54+
55+
alarm_actions = var.alarm_actions
56+
ok_actions = var.ok_actions
57+
58+
depends_on = [
59+
aws_elasticache_replication_group.this
60+
]
61+
}

examples/main.tf

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
data "aws_caller_identity" "current" {}
2+
3+
locals {
4+
vpc_id = "vpc-0123456789"
5+
6+
db_subnets = [
7+
"10.0.2.0/24",
8+
"10.0.3.0/24"
9+
]
10+
}
11+
12+
module "redis" {
13+
source = "../"
14+
15+
enabled = true
16+
cluster_mode_enabled = false
17+
create_elasticache_subnet_group = false
18+
replication_enabled = true
19+
20+
subnets = local.db_subnets
21+
22+
name = "redis-test-cluster-module"
23+
port = 6379
24+
25+
instance_type = "cache.t2.small"
26+
engine_version = "5.0.6"
27+
security_groups = ["sg-0123456789"]
28+
29+
subnet_group_name = "default"
30+
elasticache_parameter_group_family = "redis5.0"
31+
32+
cluster_size = 1
33+
34+
apply_immediately = true
35+
36+
tags = {
37+
description = "redis-test-cluster-module"
38+
}
39+
}

examples/outputs.tf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
output "account_id" {
2+
description = "Account ID number of the account that owns or contains the calling entity"
3+
value = data.aws_caller_identity.current.account_id
4+
}
5+
6+
output "caller_arn" {
7+
description = "ARN associated with the calling entity"
8+
value = data.aws_caller_identity.current.arn
9+
}
10+
11+
output "caller_user" {
12+
description = "Unique identifier of the calling entity"
13+
value = data.aws_caller_identity.current.user_id
14+
}

examples/versions.tf

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
terraform {
2+
required_version = ">= 1.0"
3+
required_providers {
4+
aws = {
5+
source = "hashicorp/aws"
6+
version = ">= 4.0"
7+
}
8+
}
9+
}

main.tf

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
locals {
2+
cluster_id = coalesce(var.cluster_id, var.name)
3+
}
4+
5+
resource "aws_elasticache_parameter_group" "this" {
6+
count = var.enabled ? 1 : 0
7+
8+
name = var.name
9+
family = var.elasticache_parameter_group_family
10+
11+
dynamic "parameter" {
12+
for_each = var.parameters
13+
14+
content {
15+
name = parameter.value.name
16+
value = parameter.value.value
17+
}
18+
}
19+
}
20+
21+
resource "aws_elasticache_subnet_group" "this" {
22+
count = var.enabled && var.create_elasticache_subnet_group ? 1 : 0
23+
24+
name = var.subnet_group_name
25+
subnet_ids = var.subnets
26+
}
27+
28+
resource "aws_elasticache_replication_group" "this" {
29+
count = var.enabled ? 1 : 0
30+
31+
replication_group_id = var.replication_group_id == "" ? local.cluster_id : var.replication_group_id
32+
description = "Redis Cluster Rep"
33+
34+
engine = "redis"
35+
engine_version = var.engine_version
36+
port = var.port
37+
38+
node_type = var.instance_type
39+
num_cache_clusters = var.replication_enabled ? var.cluster_size >= 2 ? var.cluster_size : 2 : var.cluster_size
40+
41+
preferred_cache_cluster_azs = var.replication_enabled ? var.preferred_cache_cluster_azs : null
42+
43+
parameter_group_name = var.parameter_group_name != "" ? var.parameter_group_name : aws_elasticache_parameter_group.this[0].name
44+
subnet_group_name = try(aws_elasticache_subnet_group.this[0].name, var.subnet_group_name)
45+
security_group_ids = var.security_groups
46+
47+
multi_az_enabled = var.replication_enabled ? true : false
48+
at_rest_encryption_enabled = true
49+
transit_encryption_enabled = true
50+
automatic_failover_enabled = var.replication_enabled ? true : false
51+
52+
notification_topic_arn = var.notification_topic_arn
53+
54+
apply_immediately = var.apply_immediately
55+
56+
auth_token = var.auth_token
57+
kms_key_id = var.kms_key_id
58+
59+
num_node_groups = var.cluster_mode_enabled ? var.num_node_groups : null
60+
replicas_per_node_group = var.cluster_mode_enabled ? var.replicas_per_node_group : null
61+
62+
tags = var.tags
63+
}

outputs.tf

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
output "endpoint" {
2+
description = "Redis primary or configuration endpoint, whichever is appropriate for the given cluster mode"
3+
value = aws_elasticache_replication_group.this[0].primary_endpoint_address
4+
}
5+
6+
output "reader_endpoint_address" {
7+
description = "The address of the endpoint for the reader node in the replication group, if the cluster mode is disabled"
8+
value = aws_elasticache_replication_group.this[0].reader_endpoint_address
9+
}
10+
11+
output "member_clusters" {
12+
description = "Redis cluster members"
13+
value = aws_elasticache_replication_group.this[0].member_clusters
14+
}
15+
16+
output "arn" {
17+
description = "Elasticache Replication Group ARN"
18+
value = aws_elasticache_replication_group.this[0].arn
19+
}
20+
21+
output "cluster_enabled" {
22+
description = "Indicates if cluster mode is enabled."
23+
value = aws_elasticache_replication_group.this[0].cluster_enabled
24+
}
25+
26+
output "id" {
27+
description = "ID of the ElastiCache Replication Group."
28+
value = aws_elasticache_replication_group.this[0].id
29+
}
30+
31+
output "configuration_endpoint_address" {
32+
description = "Address of the replication group configuration endpoint when cluster mode is enabled."
33+
value = aws_elasticache_replication_group.this[0].configuration_endpoint_address
34+
}
35+
36+
output "engine_version_actual" {
37+
description = "Because ElastiCache pulls the latest minor or patch for a version, this attribute returns the running version of the cache engine."
38+
value = aws_elasticache_replication_group.this[0].engine_version_actual
39+
}
40+
41+
output "subnet_group_name" {
42+
description = "The Name of the ElastiCache Subnet Group."
43+
value = aws_elasticache_subnet_group.this[0].name
44+
}
45+
46+
output "subnet_group_subnet_ids" {
47+
description = "The Subnet IDs of the ElastiCache Subnet Group."
48+
value = aws_elasticache_subnet_group.this[0].subnet_ids
49+
}
50+
51+
output "parameter_group_arn" {
52+
description = "The AWS ARN associated with the parameter group."
53+
value = aws_elasticache_parameter_group.this[0].arn
54+
}
55+
56+
output "parameter_group_id" {
57+
description = "The ElastiCache parameter group name."
58+
value = aws_elasticache_parameter_group.this[0].id
59+
}

0 commit comments

Comments
 (0)