Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SMS (SNS) stored queries from CWI into Terraform / SMS (pinpoint) new queries #1574

Merged
merged 17 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 118 additions & 0 deletions aws/common/cloudwatch_queries.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
resource "aws_cloudwatch_query_definition" "sms-sns-blocked-as-spam" {
count = var.cloudwatch_enabled ? 1 : 0
name = "SMS (SNS) / Block as spam"

log_group_names = [
aws_cloudwatch_log_group.sns_deliveries_failures[0].name
]

query_string = <<QUERY
fields @timestamp as Timestamp, delivery.phoneCarrier as Carrier, delivery.providerResponse as `Provider response`, delivery.destination as `Destination phone number`
| filter delivery.providerResponse like 'spam'
| sort Timestamp desc
| limit 100
}
QUERY
}

resource "aws_cloudwatch_query_definition" "sms-sns-carrier-dwell-times" {
count = var.cloudwatch_enabled ? 1 : 0
name = "SMS (SNS) / Carrier dwell times"

log_group_names = [
aws_cloudwatch_log_group.sns_deliveries[0].name,
aws_cloudwatch_log_group.sns_deliveries_failures[0].name
]

query_string = <<QUERY
stats avg(delivery.dwellTimeMsUntilDeviceAck / 1000 / 60) as Avg_carrier_time_minutes,
| count(*) as Number by delivery.phoneCarrier as Carrier
QUERY

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The count(*) as Number should be on a new line for better readability and consistency with other queries.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is on a new line.

}

resource "aws_cloudwatch_query_definition" "sms-sns-get-failures" {
count = var.cloudwatch_enabled ? 1 : 0
name = "SMS (SNS) / Get failures"

log_group_names = [
aws_cloudwatch_log_group.sns_deliveries_failures[0].name
]

query_string = <<QUERY
fields @timestamp as Timestamp, status, delivery.phoneCarrier as Carrier, delivery.providerResponse as `Provider response`, delivery.destination as `Destination phone number`, notification.messageId as messageId, @message
| filter status = 'FAILURE'
| sort Timestamp desc
| limit 200
QUERY
}

resource "aws_cloudwatch_query_definition" "sms-sns-get-sms-logs-by-phone-number" {
count = var.cloudwatch_enabled ? 1 : 0
name = "SMS (SNS) / Get SMS logs by phone number"

log_group_names = [
aws_cloudwatch_log_group.sns_deliveries[0].name,
aws_cloudwatch_log_group.sns_deliveries_failures[0].name
]

query_string = <<QUERY
fields @timestamp as Timestamp, status as Status, notification.messageId as `Message ID`,
delivery.destination as `Destination phone number`, delivery.providerResponse as `Provider response`,
delivery.smsType as `Message type`
| filter delivery.destination like '1416xxxxxxx'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider parameterizing the phone number filter to avoid hardcoding '1416xxxxxxx'. This will make the query more flexible and reusable.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we can use parameters in these unfortunately. If that was a query in a dashboard, yeah but that is not the case.

| sort Timestamp desc
| limit 100
QUERY
}

resource "aws_cloudwatch_query_definition" "sms-sns-international-sending-status" {
count = var.cloudwatch_enabled ? 1 : 0
name = "SMS (SNS) / International sending status"

log_group_names = [
aws_cloudwatch_log_group.sns_deliveries[0].name,
aws_cloudwatch_log_group.sns_deliveries_failures[0].name
]

query_string = <<QUERY
fields @timestamp, @message, delivery.mcc as CountryCode, status
| stats count(*) as Event_Count by CountryCode, status
| display CountryCode, status, Event_Count
| sort CountryCode asc
| limit 200
QUERY
}

resource "aws_cloudwatch_query_definition" "sms-sns-success-vs-unreachable" {
count = var.cloudwatch_enabled ? 1 : 0
name = "SMS (SNS) / Success vs Unreachable"

log_group_names = [
aws_cloudwatch_log_group.sns_deliveries[0].name,
aws_cloudwatch_log_group.sns_deliveries_failures[0].name
]

query_string = <<QUERY
fields @timestamp, delivery.providerResponse
| parse delivery.providerResponse "Phone is currently unreachable/*" as @unavailable
| parse delivery.providerResponse "Message has been * by phone" as @available
| sort @timestamp desc
| stats count(@unavailable), count(@available), count(*) by bin(1h)
QUERY
}

resource "aws_cloudwatch_query_definition" "sms-sns-unreachable-phone-numbers" {
count = var.cloudwatch_enabled ? 1 : 0
name = "SMS (SNS) / Unreachable phone numbers"

log_group_names = [
aws_cloudwatch_log_group.sns_deliveries_failures[0].name
]

query_string = <<QUERY
fields @timestamp, delivery.providerResponse
| filter delivery.providerResponse like "Phone is currently unreachable/unavailable"
| sort @timestamp desc
| limit 20
QUERY
}
189 changes: 189 additions & 0 deletions aws/pinpoint_to_sqs_sms_callbacks/cloudwatch_alarms.tf
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,195 @@ resource "aws_cloudwatch_metric_alarm" "total-sms-spending-critical" {
}
}

resource "aws_cloudwatch_metric_alarm" "pinpoint-sms-failures-bell-warning" {
count = var.cloudwatch_enabled ? 1 : 0
alarm_name = "pinpoint-sms-failures-bell-warning"
alarm_description = "Pinpoint SMS failures are more than 50 for Bell Cellular Inc. / Aliant Telecom."
comparison_operator = "GreaterThanThreshold"
evaluation_periods = "1"
metric_name = aws_cloudwatch_log_metric_filter.pinpoint-sms-failures-carriers[0].metric_transformation[0].name
namespace = "LogMetrics"
period = "300"
statistic = "Sum"
threshold = "50" # 50 over a period of 5 minutes
treat_missing_data = "notBreaching"

dimensions = {
Carrier = "Bell Cellular Inc. / Aliant Telecom"
}

alarm_actions = [var.sns_alert_warning_arn]
ok_actions = [var.sns_alert_warning_arn]
}

resource "aws_cloudwatch_metric_alarm" "pinpoint-sms-failures-bragg-warning" {
count = var.cloudwatch_enabled ? 1 : 0
alarm_name = "pinpoint-sms-failures-bragg-warning"
alarm_description = "Pinpoint SMS failures are more than 50 for BRAGG Communications INC."
comparison_operator = "GreaterThanThreshold"
evaluation_periods = "1"
metric_name = aws_cloudwatch_log_metric_filter.pinpoint-sms-failures-carriers[0].metric_transformation[0].name
namespace = "LogMetrics"
period = "300"
statistic = "Sum"
threshold = "50" # 50 over a period of 5 minutes
treat_missing_data = "notBreaching"

dimensions = {
Carrier = "BRAGG Communications INC."
}

alarm_actions = [var.sns_alert_warning_arn]
ok_actions = [var.sns_alert_warning_arn]
}

resource "aws_cloudwatch_metric_alarm" "pinpoint-sms-failures-freedom-warning" {
count = var.cloudwatch_enabled ? 1 : 0
alarm_name = "pinpoint-sms-failures-freedom-warning"
alarm_description = "Pinpoint SMS failures are more than 50 for Freedom Mobile Inc."
comparison_operator = "GreaterThanThreshold"
evaluation_periods = "1"
metric_name = aws_cloudwatch_log_metric_filter.pinpoint-sms-failures-carriers[0].metric_transformation[0].name
namespace = "LogMetrics"
period = "300"
statistic = "Sum"
threshold = "50" # 50 over a period of 5 minutes
treat_missing_data = "notBreaching"

dimensions = {
Carrier = "Freedom Mobile Inc."
}

alarm_actions = [var.sns_alert_warning_arn]
ok_actions = [var.sns_alert_warning_arn]
}

resource "aws_cloudwatch_metric_alarm" "pinpoint-sms-failures-iristel-warning" {
count = var.cloudwatch_enabled ? 1 : 0
alarm_name = "pinpoint-sms-failures-iristel-warning"
alarm_description = "Pinpoint SMS failures are more than 50 for Iristel Inc."
comparison_operator = "GreaterThanThreshold"
evaluation_periods = "1"
metric_name = aws_cloudwatch_log_metric_filter.pinpoint-sms-failures-carriers[0].metric_transformation[0].name
namespace = "LogMetrics"
period = "300"
statistic = "Sum"
threshold = "50" # 50 over a period of 5 minutes
treat_missing_data = "notBreaching"

dimensions = {
Carrier = "Iristel Inc."
}

alarm_actions = [var.sns_alert_warning_arn]
ok_actions = [var.sns_alert_warning_arn]
}

resource "aws_cloudwatch_metric_alarm" "pinpoint-sms-failures-maritime-warning" {
count = var.cloudwatch_enabled ? 1 : 0
alarm_name = "pinpoint-sms-failures-maritime-warning"
alarm_description = "Pinpoint SMS failures are more than 50 for Maritime Telephone & Telegraph Ltd."
comparison_operator = "GreaterThanThreshold"
evaluation_periods = "1"
metric_name = aws_cloudwatch_log_metric_filter.pinpoint-sms-failures-carriers[0].metric_transformation[0].name
namespace = "LogMetrics"
period = "300"
statistic = "Sum"
threshold = "50" # 50 over a period of 5 minutes
treat_missing_data = "notBreaching"

dimensions = {
Carrier = "Maritime Telephone & Telegraph Ltd"
}

alarm_actions = [var.sns_alert_warning_arn]
ok_actions = [var.sns_alert_warning_arn]
}

resource "aws_cloudwatch_metric_alarm" "pinpoint-sms-failures-mts-warning" {
count = var.cloudwatch_enabled ? 1 : 0
alarm_name = "pinpoint-sms-failures-mts-warning"
alarm_description = "Pinpoint SMS failures are more than 50 for MTS Communications Inc."
comparison_operator = "GreaterThanThreshold"
evaluation_periods = "1"
metric_name = aws_cloudwatch_log_metric_filter.pinpoint-sms-failures-carriers[0].metric_transformation[0].name
namespace = "LogMetrics"
period = "300"
statistic = "Sum"
threshold = "50" # 50 over a period of 5 minutes
treat_missing_data = "notBreaching"

dimensions = {
Carrier = "MTS Communications Inc."
}

alarm_actions = [var.sns_alert_warning_arn]
ok_actions = [var.sns_alert_warning_arn]
}

resource "aws_cloudwatch_metric_alarm" "pinpoint-sms-failures-rogers-warning" {
count = var.cloudwatch_enabled ? 1 : 0
alarm_name = "pinpoint-sms-failures-rogers-warning"
alarm_description = "Pinpoint SMS failures are more than 50 for Rogers Communications Canada Inc."
comparison_operator = "GreaterThanThreshold"
evaluation_periods = "1"
metric_name = aws_cloudwatch_log_metric_filter.pinpoint-sms-failures-carriers[0].metric_transformation[0].name
namespace = "LogMetrics"
period = "300"
statistic = "Sum"
threshold = "50" # 50 over a period of 5 minutes
treat_missing_data = "notBreaching"

dimensions = {
Carrier = "Rogers Communications Canada Inc."
}

alarm_actions = [var.sns_alert_warning_arn]
ok_actions = [var.sns_alert_warning_arn]
}

resource "aws_cloudwatch_metric_alarm" "pinpoint-sms-failures-telus-warning" {
count = var.cloudwatch_enabled ? 1 : 0
alarm_name = "pinpoint-sms-failures-telus-warning"
alarm_description = "Pinpoint SMS failures are more than 50 for Telus Communications"
comparison_operator = "GreaterThanThreshold"
evaluation_periods = "1"
metric_name = aws_cloudwatch_log_metric_filter.pinpoint-sms-failures-carriers[0].metric_transformation[0].name
namespace = "LogMetrics"
period = "300"
statistic = "Sum"
threshold = "50" # 50 over a period of 5 minutes
treat_missing_data = "notBreaching"

dimensions = {
Carrier = "Telus Communications"
}

alarm_actions = [var.sns_alert_warning_arn]
ok_actions = [var.sns_alert_warning_arn]
}

resource "aws_cloudwatch_metric_alarm" "pinpoint-sms-failures-videotron-warning" {
count = var.cloudwatch_enabled ? 1 : 0
alarm_name = "pinpoint-sms-failures-videotron-warning"
alarm_description = "Pinpoint SMS failures are more than 50 for Videotron Ltd."
comparison_operator = "GreaterThanThreshold"
evaluation_periods = "1"
metric_name = aws_cloudwatch_log_metric_filter.pinpoint-sms-failures-carriers[0].metric_transformation[0].name
namespace = "LogMetrics"
period = "300"
statistic = "Sum"
threshold = "50" # 50 over a period of 5 minutes
treat_missing_data = "notBreaching"

dimensions = {
Carrier = "Videotron Ltd."
}

alarm_actions = [var.sns_alert_warning_arn]
ok_actions = [var.sns_alert_warning_arn]
}

resource "aws_cloudwatch_metric_alarm" "pinpoint-sms-success-rate-warning" {
count = var.cloudwatch_enabled ? 1 : 0
alarm_name = "pinpoint-sms-success-rate-warning"
Expand Down
19 changes: 19 additions & 0 deletions aws/pinpoint_to_sqs_sms_callbacks/cloudwatch_logs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#

resource "aws_cloudwatch_log_group" "pinpoint_deliveries" {
# REVIEW: We might want the count attribute present to disable this resource

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment suggests adding a count attribute to conditionally create the resource. Consider implementing this suggestion if you foresee a need to enable or disable this resource dynamically.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ben851 @sastels Was it a mistake to not add a switch to enable/disable the resource? The equivalent sns log group has the switch, so I assume it should be over here too.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah probably a good idea to have it enabled/disabled based on cloudwatch enabled.

name = "sns/${var.region}/${var.account_id}/PinpointDirectPublishToPhoneNumber"
retention_in_days = var.sensitive_log_retention_period_days
tags = {
Expand All @@ -11,6 +12,7 @@ resource "aws_cloudwatch_log_group" "pinpoint_deliveries" {
}

resource "aws_cloudwatch_log_group" "pinpoint_deliveries_failures" {
# REVIEW: We might want the count attribute present to disable this resource

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment suggests adding a count attribute to conditionally create the resource. If this is a valid requirement, consider implementing it directly in the code rather than leaving a comment.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ben851 @sastels Was it a mistake to not add a switch to enable/disable the resource? The equivalent sns log group has the switch, so I assume it should be over here too.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto

name = "sns/${var.region}/${var.account_id}/PinpointDirectPublishToPhoneNumber/Failure"
retention_in_days = var.sensitive_log_retention_period_days
tags = {
Expand Down Expand Up @@ -122,3 +124,20 @@ resource "aws_cloudwatch_log_metric_filter" "pinpoint-sms-failures" {
default_value = "0"
}
}

resource "aws_cloudwatch_log_metric_filter" "pinpoint-sms-failures-carriers" {
count = var.cloudwatch_enabled ? 1 : 0
log_group_name = aws_cloudwatch_log_group.pinpoint_deliveries_failures.name

name = "pinpoint-sms-failures-carriers"
pattern = "{ ($.isFinal IS TRUE) && ($.carrierName != \"\" && ( ($.messageStatus != \"SUCCESSFUL\") && ($.messageStatus != \"DELIVERED\") )) }"
jimleroyer marked this conversation as resolved.
Show resolved Hide resolved

metric_transformation {
name = "pinpoint-sms-failures-carriers"
namespace = "LogMetrics"
value = "1"
dimensions = {
jimleroyer marked this conversation as resolved.
Show resolved Hide resolved
Carrier = "$.carrierName"
}
}
}
Loading
Loading