Skip to content

Commit

Permalink
Merge pull request #11 from DNXLabs/feature/cloudfront-alarms
Browse files Browse the repository at this point in the history
Adding CloudFront CW alarms
  • Loading branch information
brunodasilvalenga authored Oct 7, 2021
2 parents d77ebdb + 7c7d8c6 commit 4100156
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 0 deletions.
40 changes: 40 additions & 0 deletions _variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,44 @@ variable "response_page_path" {
variable "lambda_edge" {
default = []
description = "Lambda EDGE configuration"
}

variable "default_threshold" {
description = "The default threshold for the metric."
default = 5
}

variable "default_evaluation_periods" {
description = "The default amount of evaluation periods."
default = 2
}

variable "default_period" {
description = "The default evaluation period."
default = 60
}

variable "default_comparison_operator" {
description = "The default comparison operator."
default = "GreaterThanOrEqualToThreshold"
}

variable "default_statistic" {
description = "The default statistic."
default = "Average"
}
variable "alarms" {
type = map(any)
default = {}
description = <<EOF
The keys of the map are the metric names. This list must be given as a comma-separated string.
The following arguments are supported:
- comparison_operator: GreaterThanOrEqualToThreshold, GreaterThanThreshold, LessThanThreshold, LessThanOrEqualToThreshold
- evaluation_periods: The number of periods over which data is compared to the specified threshold.
- period: The period in seconds over which the specified statistic is applied.
- statistic: The statistic to apply to the alarm's associated metric.
- threshold: The number of occurances over a given period.
- actions: The actions to execute when the alarm transitions into an ALARM state (ARN).
- ok_actions: The list of actions to execute when this alarm transitions into an OK state from any other state (ARN).
EOF
}
62 changes: 62 additions & 0 deletions cloudwatch-alarms-cloudfront.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
resource "aws_cloudwatch_metric_alarm" "cloufront_alarm" {
count = length(var.alarms)

alarm_name = format("cloudfront-%s-%s",
aws_cloudfront_distribution.default[0].id,
element(keys(var.alarms), count.index)
)

comparison_operator = lookup(
var.alarms[element(keys(var.alarms), count.index)],
"comparison_operator",
var.default_comparison_operator
)

evaluation_periods = lookup(
var.alarms[element(keys(var.alarms), count.index)],
"evaluation_periods",
var.default_evaluation_periods
)

metric_name = element(keys(var.alarms), count.index)

namespace = "AWS/CloudFront"

period = lookup(
var.alarms[element(keys(var.alarms), count.index)],
"period",
var.default_period
)

statistic = lookup(
var.alarms[element(keys(var.alarms), count.index)],
"statistic",
var.default_statistic
)

threshold = lookup(
var.alarms[element(keys(var.alarms), count.index)],
"threshold",
var.default_threshold
)

dimensions = {
DistributionId = aws_cloudfront_distribution.default[0].id
Region = "Global"
}

alarm_actions = compact(split(",", lookup(
var.alarms[element(keys(var.alarms), count.index)],
"actions",
""
)))

ok_actions = compact(split(",", lookup(
var.alarms[element(keys(var.alarms), count.index)],
"ok_actions",
""
)))

insufficient_data_actions = []
treat_missing_data = "ignore"
}
24 changes: 24 additions & 0 deletions examples/cloudfront-simple-alarm.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
resource "aws_s3_bucket" "app_frontend" {
bucket_prefix = "my-frontend-app-"
acl = "private"
}

module "app_frontend" {
source = "git::https://github.com/DNXLabs/terraform-aws-static-app.git?ref=2.0.1" # Always check the latest version

name = "my-frontend-app"
s3_bucket_id = aws_s3_bucket.app_frontend.id
hostnames = ["www.example.com"]
certificate_arn = "arn:aws:acm:us-east-1:<account-id>:certificate/<certificate-hash>" # Replace the <account-id> and <certificate-hash> from a valid certificate
hosted_zone = "example.com"
hostname_create = true

alarms = {
"TotalErrorRate" = {
threshold = 1 #(%)
}
"5xxErrorRate" = {
threshold = 5 #(%)
}
}
}

0 comments on commit 4100156

Please sign in to comment.