CloudWatch metrics & alarms are quite often the first choose when it comes to monitor AWS cloud based applications, it's always come up not only to collect metadata about the performance, but also to preemptively be alerted when things start to go sideways.
Recently I have been working extensively with AppSync to build a high available GraphQL API and have run into the need to create alarms with Terraform on top of the built-in metrics exposed by CloudWatch (4XX, 5XX and latency)
Basic syntax
To create a metric alarm with Terraform, the resource aws_cloudwatch_metric_alarm
should be used, below an example of how it should be look like:
resource "aws_cloudwatch_metric_alarm" "appync_500_error" {
alarm_name = "appsync-500-error"
alarm_description = "This metric monitors AppSync 500 errors"
comparison_operator = "GreaterThanOrEqualToThreshold"
threshold = "1"
evaluation_periods = "1"
actions_enabled = false
alarm_actions = []
tags = var.config.tags
}
More details about each attribut can be found on Terraform documentation.
CloudWatch metric alarm for AppSync syntax
It’s not complicated, this is all i need to crate an alarme on 5xx errors and send a message to an Amazon SNS topic:
resource "aws_cloudwatch_metric_alarm" "appync_500_error" {
alarm_name = "appsync-500-error"
alarm_description = "This metric monitors AppSync 500 errors"
comparison_operator = "GreaterThanOrEqualToThreshold"
threshold = "1"
evaluation_periods = "1"
actions_enabled = true
alarm_actions = [aws_sns_topic.appync_500_error_topic.arn]
metric_query {
id = "error500"
return_data = "true"
metric {
metric_name = "5XXError"
namespace = "AWS/AppSync"
stat = "Sum"
period = "300"
dimensions = {
GraphQLAPIId = "YOUR_API_ID"
}
}
}
tags = var.config.tags
}
It’s effortless and self-explanatory!
However, keep in mind that if you specify at least one metric_query
, you may not specify a metric_name
, namespace
, period
or statistic
directly on the root of the resource aws_cloudwatch_metric_alarm
.
All CloudWatch metrics for AppSync are emitted in one dimension GraphQLAPIId
. This means that you can filter a specific API like that:
dimensions = {
GraphQLAPIId = "YOUR_API_ID"
}
Top comments (0)