Skip to content

Commit

Permalink
cloudwatch log groups have to stay on regardless of enable cloudwatcH (
Browse files Browse the repository at this point in the history
  • Loading branch information
ben851 authored Oct 17, 2024
1 parent 5de2cc5 commit 7e9affd
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 31 deletions.
14 changes: 6 additions & 8 deletions aws/pinpoint_to_sqs_sms_callbacks/cloudwatch_logs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#

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

resource "aws_cloudwatch_log_group" "pinpoint_deliveries_failures" {
count = var.cloudwatch_enabled ? 1 : 0
name = "sns/${var.region}/${var.account_id}/PinpointDirectPublishToPhoneNumber/Failure"
retention_in_days = var.sensitive_log_retention_period_days
tags = {
Expand Down Expand Up @@ -56,7 +54,7 @@ resource "aws_cloudwatch_log_metric_filter" "pinpoint-sms-blocked-as-spam" {
name = "pinpoint-sms-blocked-as-spam"
# See https://docs.aws.amazon.com/sms-voice/latest/userguide/configuration-sets-event-format.html
pattern = "{ $.messageStatus = \"SPAM\" }"
log_group_name = aws_cloudwatch_log_group.pinpoint_deliveries_failures[0].name
log_group_name = aws_cloudwatch_log_group.pinpoint_deliveries_failures.name

metric_transformation {
name = "pinpoint-sms-blocked-as-spam"
Expand All @@ -71,7 +69,7 @@ resource "aws_cloudwatch_log_metric_filter" "pinpoint-sms-phone-carrier-unavaila
name = "pinpoint-sms-phone-carrier-unavailable"
# See https://docs.aws.amazon.com/sms-voice/latest/userguide/configuration-sets-event-format.html
pattern = "{ $.messageStatus = \"CARRIER_UNREACHABLE\" }"
log_group_name = aws_cloudwatch_log_group.pinpoint_deliveries_failures[0].name
log_group_name = aws_cloudwatch_log_group.pinpoint_deliveries_failures.name

metric_transformation {
name = "pinpoint-sms-phone-carrier-unavailable"
Expand All @@ -87,7 +85,7 @@ resource "aws_cloudwatch_log_metric_filter" "pinpoint-sms-rate-exceeded" {
# https://docs.aws.amazon.com/sns/latest/dg/channels-sms-originating-identities-long-codes.html
# Canadian long code numbers are limited at 1 SMS per second/number
pattern = "{ $.messageStatusDescription = \"Rate exceeded.\" }"
log_group_name = aws_cloudwatch_log_group.pinpoint_deliveries_failures[0].name
log_group_name = aws_cloudwatch_log_group.pinpoint_deliveries_failures.name

metric_transformation {
name = "pinpoint-sms-rate-exceeded"
Expand All @@ -101,7 +99,7 @@ resource "aws_cloudwatch_log_metric_filter" "pinpoint-sms-successes" {
count = var.cloudwatch_enabled ? 1 : 0
name = "pinpoint-sms-successes"
pattern = "{ ($.isFinal IS TRUE) && ( ($.messageStatus = \"SUCCESSFUL\") || ($.messageStatus = \"DELIVERED\") ) }"
log_group_name = aws_cloudwatch_log_group.pinpoint_deliveries[0].name
log_group_name = aws_cloudwatch_log_group.pinpoint_deliveries.name

metric_transformation {
name = "pinpoint-sms-successes"
Expand All @@ -115,7 +113,7 @@ resource "aws_cloudwatch_log_metric_filter" "pinpoint-sms-failures" {
count = var.cloudwatch_enabled ? 1 : 0
name = "pinpoint-sms-failures"
pattern = "{ ($.isFinal IS TRUE) && ( ($.messageStatus != \"SUCCESSFUL\") && ($.messageStatus != \"DELIVERED\") ) }"
log_group_name = aws_cloudwatch_log_group.pinpoint_deliveries_failures[0].name
log_group_name = aws_cloudwatch_log_group.pinpoint_deliveries_failures.name

metric_transformation {
name = "pinpoint-sms-failures"
Expand All @@ -127,7 +125,7 @@ resource "aws_cloudwatch_log_metric_filter" "pinpoint-sms-failures" {

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[0].name
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\") )) }"
Expand Down
32 changes: 16 additions & 16 deletions aws/pinpoint_to_sqs_sms_callbacks/cloudwatch_queries.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ resource "aws_cloudwatch_query_definition" "pinpoint-sms-blocked-as-spam" {
name = "SMS (Pinpoint) / Block as spam"

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

query_string = <<QUERY
Expand All @@ -24,8 +24,8 @@ resource "aws_cloudwatch_query_definition" "pinpoint-sms-carrier-dwell-times" {
name = "SMS (Pinpoint) / Carrier dwell times"

log_group_names = [
aws_cloudwatch_log_group.pinpoint_deliveries[0].name,
aws_cloudwatch_log_group.pinpoint_deliveries_failures[0].name,
aws_cloudwatch_log_group.pinpoint_deliveries.name,
aws_cloudwatch_log_group.pinpoint_deliveries_failures.name,
]

query_string = <<QUERY
Expand All @@ -42,8 +42,8 @@ resource "aws_cloudwatch_query_definition" "pinpoint-sms-failures-by-carrier" {
name = "SMS (Pinpoint) / Failures by carrier"

log_group_names = [
aws_cloudwatch_log_group.pinpoint_deliveries[0].name,
aws_cloudwatch_log_group.pinpoint_deliveries_failures[0].name,
aws_cloudwatch_log_group.pinpoint_deliveries.name,
aws_cloudwatch_log_group.pinpoint_deliveries_failures.name,
]

query_string = <<QUERY
Expand All @@ -59,7 +59,7 @@ resource "aws_cloudwatch_query_definition" "pinpoint-sms-get-failures" {
name = "SMS (Pinpoint) / Get failures"

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

query_string = <<QUERY
Expand All @@ -78,8 +78,8 @@ resource "aws_cloudwatch_query_definition" "pinpoint-sms-international-sending-s
name = "SMS (Pinpoint) / International sending status"

log_group_names = [
aws_cloudwatch_log_group.pinpoint_deliveries[0].name,
aws_cloudwatch_log_group.pinpoint_deliveries_failures[0].name
aws_cloudwatch_log_group.pinpoint_deliveries.name,
aws_cloudwatch_log_group.pinpoint_deliveries_failures.name
]

query_string = <<QUERY
Expand All @@ -100,8 +100,8 @@ resource "aws_cloudwatch_query_definition" "pinpoint-sms-get-sms-logs-by-dest-ph
name = "SMS (Pinpoint) / Get SMS logs by destination phone number"

log_group_names = [
aws_cloudwatch_log_group.pinpoint_deliveries[0].name,
aws_cloudwatch_log_group.pinpoint_deliveries_failures[0].name
aws_cloudwatch_log_group.pinpoint_deliveries.name,
aws_cloudwatch_log_group.pinpoint_deliveries_failures.name
]

query_string = <<QUERY
Expand All @@ -120,8 +120,8 @@ resource "aws_cloudwatch_query_definition" "pinpoint-sms-get-sms-logs-by-orig-ph
name = "SMS (Pinpoint) / Get SMS logs by origination phone number"

log_group_names = [
aws_cloudwatch_log_group.pinpoint_deliveries[0].name,
aws_cloudwatch_log_group.pinpoint_deliveries_failures[0].name
aws_cloudwatch_log_group.pinpoint_deliveries.name,
aws_cloudwatch_log_group.pinpoint_deliveries_failures.name
]

query_string = <<QUERY
Expand All @@ -140,8 +140,8 @@ resource "aws_cloudwatch_query_definition" "pinpoint-sms-get-logs" {
name = "SMS (Pinpoint) / Logs"

log_group_names = [
aws_cloudwatch_log_group.pinpoint_deliveries[0].name,
aws_cloudwatch_log_group.pinpoint_deliveries_failures[0].name
aws_cloudwatch_log_group.pinpoint_deliveries.name,
aws_cloudwatch_log_group.pinpoint_deliveries_failures.name
]

query_string = <<QUERY
Expand All @@ -160,8 +160,8 @@ resource "aws_cloudwatch_query_definition" "pinpoint-sms-success-vs-unreachable"
name = "SMS (Pinpoint) / Success vs Unreachable"

log_group_names = [
aws_cloudwatch_log_group.pinpoint_deliveries[0].name,
aws_cloudwatch_log_group.pinpoint_deliveries_failures[0].name
aws_cloudwatch_log_group.pinpoint_deliveries.name,
aws_cloudwatch_log_group.pinpoint_deliveries_failures.name
]

query_string = <<QUERY
Expand Down
4 changes: 2 additions & 2 deletions aws/pinpoint_to_sqs_sms_callbacks/iam.tf
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ data "aws_iam_policy_document" "pinpoint_logs" {
"logs:PutLogEvents"
]
resources = [
"${aws_cloudwatch_log_group.pinpoint_deliveries[0].arn}:*",
"${aws_cloudwatch_log_group.pinpoint_deliveries_failures[0].arn}:*"
"${aws_cloudwatch_log_group.pinpoint_deliveries.arn}:*",
"${aws_cloudwatch_log_group.pinpoint_deliveries_failures.arn}:*"
]
}
}
8 changes: 4 additions & 4 deletions aws/pinpoint_to_sqs_sms_callbacks/lambda.tf
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ resource "aws_lambda_permission" "allow_cloudwatch_logs_pinpoint_successes" {
action = "lambda:InvokeFunction"
function_name = module.pinpoint_to_sqs_sms_callbacks.function_name
principal = "logs.${var.region}.amazonaws.com"
source_arn = "${aws_cloudwatch_log_group.pinpoint_deliveries[0].arn}:*"
source_arn = "${aws_cloudwatch_log_group.pinpoint_deliveries.arn}:*"
}

resource "aws_cloudwatch_log_subscription_filter" "pinpoint_deliveries_ca_central_to_lambda" {
count = var.cloudwatch_enabled ? 1 : 0
name = "pinpoint_deliveries_ca_central"
log_group_name = aws_cloudwatch_log_group.pinpoint_deliveries[0].name
log_group_name = aws_cloudwatch_log_group.pinpoint_deliveries.name
filter_pattern = ""
destination_arn = module.pinpoint_to_sqs_sms_callbacks.function_arn
}
Expand All @@ -46,13 +46,13 @@ resource "aws_lambda_permission" "allow_cloudwatch_logs_pinpoint_failures" {
action = "lambda:InvokeFunction"
function_name = module.pinpoint_to_sqs_sms_callbacks.function_name
principal = "logs.${var.region}.amazonaws.com"
source_arn = "${aws_cloudwatch_log_group.pinpoint_deliveries_failures[0].arn}:*"
source_arn = "${aws_cloudwatch_log_group.pinpoint_deliveries_failures.arn}:*"
}

resource "aws_cloudwatch_log_subscription_filter" "pinpoint_deliveries_failures_ca_central_to_lambda" {
count = var.cloudwatch_enabled ? 1 : 0
name = "pinpoint_deliveries_failures_ca_central"
log_group_name = aws_cloudwatch_log_group.pinpoint_deliveries_failures[0].name
log_group_name = aws_cloudwatch_log_group.pinpoint_deliveries_failures.name
filter_pattern = ""
destination_arn = module.pinpoint_to_sqs_sms_callbacks.function_arn
}
2 changes: 1 addition & 1 deletion aws/pinpoint_to_sqs_sms_callbacks/pools.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ resource "null_resource" "create_pools" {
depends_on = [aws_iam_role.pinpoint_logs, aws_cloudwatch_log_group.pinpoint_deliveries, aws_cloudwatch_log_group.pinpoint_deliveries_failures]

provisioner "local-exec" {
command = "./create_pinpoint_pools.sh ${aws_iam_role.pinpoint_logs.arn} ${aws_cloudwatch_log_group.pinpoint_deliveries[0].arn} ${aws_cloudwatch_log_group.pinpoint_deliveries_failures[0].arn}"
command = "./create_pinpoint_pools.sh ${aws_iam_role.pinpoint_logs.arn} ${aws_cloudwatch_log_group.pinpoint_deliveries.arn} ${aws_cloudwatch_log_group.pinpoint_deliveries_failures.arn}"
}
}

0 comments on commit 7e9affd

Please sign in to comment.