Skip to content

Commit 00546ca

Browse files
authored
Merge pull request #6 from oozou/feat/support-alarm
Feat/support alarm
2 parents c5cd5f3 + 9506e12 commit 00546ca

File tree

8 files changed

+221
-1
lines changed

8 files changed

+221
-1
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@
22

33
All notable changes to this module will be documented in this file.
44

5+
## [v1.0.2] - 2022-12-22
6+
7+
### Added
8+
9+
- Add alarm.tf with default and custom elasticache alarms
10+
- Add following vars
11+
- is_enable_default_alarms
12+
- default_alarm_actions
13+
- default_ok_actions
14+
- custom_elasticache_alarms_configure
15+
516
## [1.0.1] - 2022-05-24
617

718
Here we would have the update steps for 1.0.1 for people to follow.

README.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,67 @@ module "elasticache_cluster" {
5858
multi_az_enabled = var.multi_az_enabled
5959
}
6060
```
61+
62+
<!-- BEGIN_TF_DOCS -->
63+
## Requirements
64+
65+
| Name | Version |
66+
|------|---------|
67+
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.1.1 |
68+
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 4.0.0 |
69+
70+
## Providers
71+
72+
| Name | Version |
73+
|------|---------|
74+
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 4.0.0 |
75+
76+
## Modules
77+
78+
| Name | Source | Version |
79+
|------|--------|---------|
80+
| <a name="module_custom_elasticache_alarms"></a> [custom\_elasticache\_alarms](#module\_custom\_elasticache\_alarms) | oozou/cloudwatch-alarm/aws | 1.0.0 |
81+
82+
## Resources
83+
84+
| Name | Type |
85+
|------|------|
86+
| [aws_cloudwatch_metric_alarm.redis_cpu_alarm](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_metric_alarm) | resource |
87+
| [aws_cloudwatch_metric_alarm.redis_memory_alarm](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_metric_alarm) | resource |
88+
| [aws_elasticache_replication_group.elasticache](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/elasticache_replication_group) | resource |
89+
| [aws_elasticache_subnet_group.elasticache](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/elasticache_subnet_group) | resource |
90+
| [aws_security_group.client](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group) | resource |
91+
| [aws_security_group.elasticache](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group) | resource |
92+
| [aws_security_group_rule.additional_cluster_ingress](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group_rule) | resource |
93+
| [aws_security_group_rule.egress](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group_rule) | resource |
94+
| [aws_security_group_rule.ingress](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group_rule) | resource |
95+
| [aws_subnet.subnets](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/subnet) | data source |
96+
97+
## Inputs
98+
99+
| Name | Description | Type | Default | Required |
100+
|------|-------------|------|---------|:--------:|
101+
| <a name="input_additional_cluster_security_group_ingress_rules"></a> [additional\_cluster\_security\_group\_ingress\_rules](#input\_additional\_cluster\_security\_group\_ingress\_rules) | Additional ingress rule for cluster security group. | <pre>list(object({<br> from_port = number<br> to_port = number<br> protocol = string<br> cidr_blocks = list(string)<br> source_security_group_id = string<br> description = string<br> }))</pre> | `[]` | no |
102+
| <a name="input_auth_token"></a> [auth\_token](#input\_auth\_token) | Auth token for the Elasticache redis auth. Reference: https://docs.aws.amazon.com/AmazonElastiCache/latest/red-ug/auth.html | `string` | n/a | yes |
103+
| <a name="input_custom_elasticache_alarms_configure"></a> [custom\_elasticache\_alarms\_configure](#input\_custom\_elasticache\_alarms\_configure) | custom\_elasticache\_alarms\_configure = {<br> cpu\_utilization\_too\_high = {<br> metric\_name = "EngineCPUUtilization"<br> statistic = "Average"<br> comparison\_operator = ">="<br> threshold = "85"<br> period = "300"<br> evaluation\_periods = "1"<br> alarm\_actions = [sns\_topic\_arn]<br> ok\_actions = [sns\_topic\_arn]<br> }<br> } | `any` | `{}` | no |
104+
| <a name="input_default_alarm_actions"></a> [default\_alarm\_actions](#input\_default\_alarm\_actions) | The list of actions to execute when this alarm transitions into an ALARM state from any other state. Each action is specified as an Amazon Resource Name (ARN). | `list(string)` | `[]` | no |
105+
| <a name="input_default_ok_actions"></a> [default\_ok\_actions](#input\_default\_ok\_actions) | The list of actions to execute when this alarm transitions into an OK state from any other state. Each action is specified as an Amazon Resource Name (ARN). | `list(string)` | `[]` | no |
106+
| <a name="input_environment"></a> [environment](#input\_environment) | Environment Variable used as a prefix | `string` | n/a | yes |
107+
| <a name="input_is_enable_default_alarms"></a> [is\_enable\_default\_alarms](#input\_is\_enable\_default\_alarms) | if enable the default alarms | `bool` | `false` | no |
108+
| <a name="input_maintenance_window"></a> [maintenance\_window](#input\_maintenance\_window) | Snapshot Retention Limit | `string` | `"mon:00:00-mon:03:00"` | no |
109+
| <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` | n/a | yes |
110+
| <a name="input_name"></a> [name](#input\_name) | Name of the ECS cluster to create | `string` | n/a | yes |
111+
| <a name="input_prefix"></a> [prefix](#input\_prefix) | The prefix name of customer to be displayed in AWS console and resource | `string` | n/a | yes |
112+
| <a name="input_redis_cluster_config"></a> [redis\_cluster\_config](#input\_redis\_cluster\_config) | Configuration for redis cluster | <pre>object({<br> port = number<br> instance_type = string<br> engine_version = string<br> node_count = number<br> })</pre> | n/a | yes |
113+
| <a name="input_snapshot_config"></a> [snapshot\_config](#input\_snapshot\_config) | (optional) Snapshot config to retain and create backup | <pre>object({<br> snapshot_window = string<br> snapshot_retention_limit = number<br> })</pre> | <pre>{<br> "snapshot_retention_limit": 3,<br> "snapshot_window": "03:00-05:00"<br>}</pre> | no |
114+
| <a name="input_tags"></a> [tags](#input\_tags) | Custom tags which can be passed on to the AWS resources. They should be key value pairs having distinct keys. | `map(string)` | `{}` | no |
115+
| <a name="input_vpc_config"></a> [vpc\_config](#input\_vpc\_config) | VPC ID and private subnets for ElastiCache cluster | <pre>object({<br> vpc_id = string<br> private_subnets = list(string)<br> })</pre> | n/a | yes |
116+
117+
## Outputs
118+
119+
| Name | Description |
120+
|------|-------------|
121+
| <a name="output_client_security_group_id"></a> [client\_security\_group\_id](#output\_client\_security\_group\_id) | Security group id for the elasticache clients. |
122+
| <a name="output_redis_host"></a> [redis\_host](#output\_redis\_host) | Hostname of the redis host in the replication group. |
123+
| <a name="output_security_group_id"></a> [security\_group\_id](#output\_security\_group\_id) | Security group id for the elasticache. |
124+
<!-- END_TF_DOCS -->

alarms.tf

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
module "custom_elasticache_alarms" {
2+
source = "oozou/cloudwatch-alarm/aws"
3+
version = "1.0.0"
4+
5+
for_each = var.custom_elasticache_alarms_configure
6+
depends_on = [aws_elasticache_replication_group.elasticache]
7+
8+
prefix = var.prefix
9+
environment = var.environment
10+
name = format("%s-%s-alarm", local.service_name, each.key)
11+
12+
alarm_description = format(
13+
"%s's %s %s %s in period %ss with %s datapoint",
14+
lookup(each.value, "metric_name", null),
15+
lookup(each.value, "statistic", "Average"),
16+
lookup(each.value, "comparison_operator", null),
17+
lookup(each.value, "threshold", null),
18+
lookup(each.value, "period", 600),
19+
lookup(each.value, "evaluation_periods", 1)
20+
)
21+
22+
comparison_operator = local.comparison_operators[lookup(each.value, "comparison_operator", null)]
23+
evaluation_periods = lookup(each.value, "evaluation_periods", 1)
24+
metric_name = lookup(each.value, "metric_name", null)
25+
namespace = "AWS/ElastiCache"
26+
period = lookup(each.value, "period", 600)
27+
statistic = lookup(each.value, "statistic", "Average")
28+
threshold = lookup(each.value, "threshold", null)
29+
30+
dimensions = {
31+
CacheClusterId = aws_elasticache_replication_group.elasticache.global_replication_group_id
32+
}
33+
34+
alarm_actions = lookup(each.value, "alarm_actions", null)
35+
ok_actions = lookup(each.value, "ok_actions", null)
36+
37+
tags = local.tags
38+
}
39+
40+
resource "aws_cloudwatch_metric_alarm" "redis_cpu_alarm" {
41+
count = var.is_enable_default_alarms ? 1 : 0
42+
alarm_name = format("%s-%s-alarm", local.service_name, "redis_high_CPU")
43+
comparison_operator = "GreaterThanThreshold"
44+
evaluation_periods = "1"
45+
metric_name = "CPUUtilization"
46+
namespace = "AWS/ElastiCache"
47+
period = "600"
48+
statistic = "Average"
49+
threshold = "80"
50+
alarm_description = "This alarm will trigger if the Redis cluster's cpu usage is too high"
51+
alarm_actions = var.default_alarm_actions
52+
ok_actions = var.default_ok_actions
53+
54+
dimensions = {
55+
CacheClusterId = aws_elasticache_replication_group.elasticache.global_replication_group_id
56+
}
57+
depends_on = [aws_elasticache_replication_group.elasticache]
58+
}
59+
60+
resource "aws_cloudwatch_metric_alarm" "redis_memory_alarm" {
61+
count = var.is_enable_default_alarms ? 1 : 0
62+
alarm_name = format("%s-%s-alarm", local.service_name, "redis_high_memory")
63+
comparison_operator = "GreaterThanThreshold"
64+
evaluation_periods = "1"
65+
metric_name = "DatabaseMemoryUsagePercentage"
66+
namespace = "AWS/ElastiCache"
67+
period = "600"
68+
statistic = "Average"
69+
threshold = "80"
70+
alarm_description = "This alarm will trigger if the Redis cluster's memory usage is too high"
71+
alarm_actions = var.default_alarm_actions
72+
ok_actions = var.default_ok_actions
73+
74+
dimensions = {
75+
CacheClusterId = aws_elasticache_replication_group.elasticache.global_replication_group_id
76+
}
77+
depends_on = [aws_elasticache_replication_group.elasticache]
78+
}
79+

examples/complete/example.auto.tfvars

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ environment = "example"
33
custom_tags = {
44
"Remark" = "terraform-aws-elasticache-example"
55
}
6+

examples/complete/main.tf

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,17 @@ module "redis" {
3939
}
4040

4141
tags = var.custom_tags
42+
43+
is_enable_default_alarms = true
44+
45+
custom_elasticache_alarms_configure = {
46+
cpu_utilization_too_high = {
47+
metric_name = "EngineCPUUtilization"
48+
statistic = "Average"
49+
comparison_operator = ">="
50+
threshold = "85"
51+
period = "300"
52+
evaluation_periods = "1"
53+
}
54+
}
4255
}

examples/complete/vpc.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module "vpc" {
66
private_subnets = ["10.105.60.0/22", "10.105.64.0/22", "10.105.68.0/22"]
77
public_subnets = ["10.105.0.0/24", "10.105.1.0/24", "10.105.2.0/24"]
88
database_subnets = ["10.105.20.0/23", "10.105.22.0/23", "10.105.24.0/23"]
9-
availability_zone = ["ap-southeast-1a", "ap-southeast-1b", "ap-southeast-1c"]
9+
availability_zone = ["us-east-2a", "us-east-2b", "us-east-2c"]
1010
is_enable_dns_hostnames = true
1111
is_enable_dns_support = true
1212
is_create_nat_gateway = true

locals.tf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,14 @@ locals {
1313
automatic_failover_enabled = var.redis_cluster_config.node_count > 1
1414
# To enable, automatic_failover_enabled must also be enabled
1515
multi_az_enabled = var.multi_az_enabled == true && local.automatic_failover_enabled == true
16+
17+
/* -------------------------------------------------------------------------- */
18+
/* Alarms */
19+
/* -------------------------------------------------------------------------- */
20+
comparison_operators = {
21+
">=" = "GreaterThanOrEqualToThreshold",
22+
">" = "GreaterThanThreshold",
23+
"<" = "LessThanThreshold",
24+
"<=" = "LessThanOrEqualToThreshold",
25+
}
1626
}

variables.tf

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,45 @@ variable "additional_cluster_security_group_ingress_rules" {
8484
description = "Additional ingress rule for cluster security group."
8585
default = []
8686
}
87+
88+
/* -------------------------------------------------------------------------- */
89+
/* alarms */
90+
/* -------------------------------------------------------------------------- */
91+
92+
variable "is_enable_default_alarms" {
93+
description = "if enable the default alarms"
94+
type = bool
95+
default = false
96+
}
97+
98+
variable "default_alarm_actions" {
99+
description = "The list of actions to execute when this alarm transitions into an ALARM state from any other state. Each action is specified as an Amazon Resource Name (ARN)."
100+
type = list(string)
101+
default = []
102+
}
103+
104+
variable "default_ok_actions" {
105+
description = "The list of actions to execute when this alarm transitions into an OK state from any other state. Each action is specified as an Amazon Resource Name (ARN)."
106+
type = list(string)
107+
default = []
108+
}
109+
110+
111+
variable "custom_elasticache_alarms_configure" {
112+
description = <<EOF
113+
custom_elasticache_alarms_configure = {
114+
cpu_utilization_too_high = {
115+
metric_name = "EngineCPUUtilization"
116+
statistic = "Average"
117+
comparison_operator = ">="
118+
threshold = "85"
119+
period = "300"
120+
evaluation_periods = "1"
121+
alarm_actions = [sns_topic_arn]
122+
ok_actions = [sns_topic_arn]
123+
}
124+
}
125+
EOF
126+
type = any
127+
default = {}
128+
}

0 commit comments

Comments
 (0)