Skip to content

Commit 9e802bd

Browse files
committed
initial public commit
0 parents  commit 9e802bd

7 files changed

+234
-0
lines changed

.gitignore

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Local dev things
2+
.idea
3+
4+
# Local .terraform directories
5+
**/.terraform/*
6+
7+
# .tfstate files
8+
*.tfstate
9+
*.tfstate.*
10+
11+
# Crash log files
12+
crash.log
13+
14+
# Ignore any .tfvars files that are generated automatically for each Terraform run. Most
15+
# .tfvars files are managed as part of configuration and so should be included in
16+
# version control.
17+
#
18+
# example.tfvars
19+
20+
# Ignore override files as they are usually used to override resources locally and so
21+
# are not checked in
22+
override.tf
23+
override.tf.json
24+
*_override.tf
25+
*_override.tf.json
26+
27+
# Include override files you do wish to add to version control using negated pattern
28+
#
29+
# !example_override.tf

.gitlab-ci.yml

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
image:
2+
name: hashicorp/terraform:light
3+
entrypoint:
4+
- '/usr/bin/env'
5+
- 'PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin'
6+
7+
cache:
8+
paths:
9+
- .terraform
10+
11+
before_script:
12+
- terraform --version
13+
14+
stages:
15+
- format
16+
17+
format:
18+
stage: format
19+
script:
20+
- terraform fmt -check
21+

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 Lorenzo Aiello
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

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Terraform Module for AWS ALB Cloudwatch Alarms
2+
3+
This Terraform module manages Cloudwatch Alarms for an ALB in the region. It does NOT create or manage Load Balancers, only Metric Alarms.
4+
5+
**Requires**:
6+
- AWS Provider
7+
- Terraform 0.12
8+
9+
## Alarms Created
10+
11+
Alarms Always Created:
12+
- Any 5xx errors from the target group
13+
- Any 5xx errors from the load balancer
14+
- Unacceptably high average response times
15+
16+
**Estimated Operating Cost**: $ 0.30 / month
17+
18+
- $ 0.10 / month for Metric Alarms (3x)
19+
20+
## Example
21+
22+
```hcl-terraform
23+
module "aws-efs-alarms" {
24+
source = "lorenzoaiello/nat-alarms/aws"
25+
version = "x.y.z"
26+
}
27+
28+
```
29+
30+
## Variables
31+
32+
| Name | Description | Type | Default | Required |
33+
|------|-------------|------|---------|:-----:|
34+
| actions\_alarm | A list of actions to take when alarms are triggered. Will likely be an SNS topic for event distribution. | `list` | `[]` | no |
35+
| actions\_ok | A list of actions to take when alarms are cleared. Will likely be an SNS topic for event distribution. | `list` | `[]` | no |
36+
| evaluation\_period | The evaluation period over which to use when triggering alarms. | `string` | `"5"` | no |
37+
| load\_balancer\_id | ALB ID | `string` | n/a | yes |
38+
| prefix | Alarm Name Prefix | `string` | `""` | no |
39+
| response\_time\_threshold | The average number of milliseconds that requests should complete within. | `string` | `"50"` | no |
40+
| statistic\_period | The number of seconds that make each statistic period. | `string` | `"60"` | no |
41+
| target\_group\_id | Target Group ID | `string` | n/a | yes |
42+
43+
## Outputs
44+
45+
| Name | Description |
46+
|------|-------------|
47+
| alarm\_httpcode\_lb\_5xx\_count | The CloudWatch Metric Alarm resource block for 5xx errors on the load balancer |
48+
| alarm\_httpcode\_target\_5xx\_counts | The CloudWatch Metric Alarm resource block for 5xx errors on the target group |
49+
| alarm\_target\_response\_time\_average | The CloudWatch Metric Alarm resource block for unacceptably high response time averages |

main.tf

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
resource "aws_cloudwatch_metric_alarm" "httpcode_target_5xx_count" {
2+
alarm_name = "${var.prefix}alb-tg-${var.target_group_id}-high5XXCount"
3+
comparison_operator = "GreaterThanThreshold"
4+
evaluation_periods = var.evaluation_period
5+
metric_name = "HTTPCode_Target_5XX_Count"
6+
namespace = "AWS/ApplicationELB"
7+
period = var.statistic_period
8+
statistic = "Sum"
9+
threshold = "0"
10+
alarm_description = "Average API 5XX target group error code count is too high"
11+
alarm_actions = var.actions_alarm
12+
ok_actions = var.actions_ok
13+
14+
dimensions = {
15+
"TargetGroup" = var.target_group_id
16+
"LoadBalancer" = var.load_balancer_id
17+
}
18+
}
19+
20+
resource "aws_cloudwatch_metric_alarm" "httpcode_lb_5xx_count" {
21+
alarm_name = "${var.prefix}alb-${var.load_balancer_id}-high5XXCount"
22+
comparison_operator = "GreaterThanThreshold"
23+
evaluation_periods = var.evaluation_period
24+
metric_name = "HTTPCode_ELB_5XX_Count"
25+
namespace = "AWS/ApplicationELB"
26+
period = var.statistic_period
27+
statistic = "Sum"
28+
threshold = "0"
29+
alarm_description = "Average API 5XX load balancer error code count is too high"
30+
alarm_actions = var.actions_alarm
31+
ok_actions = var.actions_ok
32+
33+
dimensions = {
34+
"LoadBalancer" = var.load_balancer_id
35+
}
36+
}
37+
38+
resource "aws_cloudwatch_metric_alarm" "target_response_time_average" {
39+
alarm_name = "${var.prefix}alb-tg-${var.target_group_id}-highResponseTime"
40+
comparison_operator = "GreaterThanThreshold"
41+
evaluation_periods = var.evaluation_period
42+
metric_name = "TargetResponseTime"
43+
namespace = "AWS/ApplicationELB"
44+
period = var.statistic_period
45+
statistic = "Average"
46+
threshold = var.response_time_threshold
47+
alarm_description = "Average API response time is too high"
48+
alarm_actions = var.actions_alarm
49+
ok_actions = var.actions_ok
50+
51+
dimensions = {
52+
"TargetGroup" = var.target_group_id
53+
"LoadBalancer" = var.load_balancer_id
54+
}
55+
}

outputs.tf

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
output "alarm_httpcode_target_5xx_counts" {
2+
value = aws_cloudwatch_metric_alarm.httpcode_target_5xx_count
3+
description = "The CloudWatch Metric Alarm resource block for 5xx errors on the target group"
4+
}
5+
6+
output "alarm_httpcode_lb_5xx_count" {
7+
value = aws_cloudwatch_metric_alarm.httpcode_lb_5xx_count
8+
description = "The CloudWatch Metric Alarm resource block for 5xx errors on the load balancer"
9+
}
10+
11+
output "alarm_target_response_time_average" {
12+
value = aws_cloudwatch_metric_alarm.target_response_time_average
13+
description = "The CloudWatch Metric Alarm resource block for unacceptably high response time averages"
14+
}

variables.tf

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
variable "load_balancer_id" {
2+
type = "string"
3+
description = "ALB ID"
4+
}
5+
6+
variable "target_group_id" {
7+
type = "string"
8+
description = "Target Group ID"
9+
}
10+
11+
variable "prefix" {
12+
type = "string"
13+
default = ""
14+
description = "Alarm Name Prefix"
15+
}
16+
17+
variable "response_time_threshold" {
18+
type = "string"
19+
default = "50"
20+
description = "The average number of milliseconds that requests should complete within."
21+
}
22+
23+
variable "evaluation_period" {
24+
type = "string"
25+
default = "5"
26+
description = "The evaluation period over which to use when triggering alarms."
27+
}
28+
29+
variable "statistic_period" {
30+
type = "string"
31+
default = "60"
32+
description = "The number of seconds that make each statistic period."
33+
}
34+
35+
variable "actions_alarm" {
36+
type = "list"
37+
default = []
38+
description = "A list of actions to take when alarms are triggered. Will likely be an SNS topic for event distribution."
39+
}
40+
41+
variable "actions_ok" {
42+
type = "list"
43+
default = []
44+
description = "A list of actions to take when alarms are cleared. Will likely be an SNS topic for event distribution."
45+
}

0 commit comments

Comments
 (0)