Skip to content
This repository has been archived by the owner on Sep 12, 2023. It is now read-only.

Commit

Permalink
feat: support config via tags (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelwittig authored May 3, 2023
1 parent e9abb00 commit bba4822
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 30 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,22 @@ terraform init
terraform apply
```

## Config via tags

You can also configure this module by tagging the SQS queue (requires v1.0.0 or higher). Tags take precedence over variables (tags override variables).

| tag key | default value | allowed values |
| ------------------------------------------------------------------ | -------------------------------------------------------------------- | ----------------------------------------------|
| `marbot` | on | on,off |
| `marbot:approximate-age-of-oldest-message` | variable `approximate_age_of_oldest_message` | static,off |
| `marbot:approximate-age-of-oldest-message:threshold` | variable `approximate_age_of_oldest_message_threshold` | >= 0 |
| `marbot:approximate-age-of-oldest-message:period` | variable `approximate_age_of_oldest_message_period` | <= 86400 and multiple of 60 |
| `marbot:approximate-age-of-oldest-message:evaluation-periods` | variable `approximate_age_of_oldest_message_evaluation_periods` | >= 1 and $period*$evaluation-periods <= 86400 |
| `marbot:approximate-number-of-messages-visible` | variable `approximate_number_of_messages_visible` | static,off |
| `marbot:approximate-number-of-messages-visible:threshold` | variable `approximate_number_of_messages_visible_threshold` | >= 0 |
| `marbot:approximate-number-of-messages-visible:period` | variable `approximate_number_of_messages_visible_period` | <= 86400 and multiple of 60 |
| `marbot:approximate-number-of-messages-visible:evaluation-periods` | variable `approximate_number_of_messages_visible_evaluation_periods` | >= 1 and $period*$evaluation-periods <= 86400 |

## Update procedure

1. Update the `version`
Expand Down
47 changes: 33 additions & 14 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,27 @@ data "aws_caller_identity" "current" {}

data "aws_region" "current" {}

data "aws_sqs_queue" "queue" {
name = var.queue_name
}

locals {
topic_arn = var.create_topic == false ? var.topic_arn : join("", aws_sns_topic.marbot.*.arn)
enabled = var.enabled && lookup(data.aws_sqs_queue.queue.tags, "marbot", "on") != "off"

approximate_age_of_oldest_message = lookup(data.aws_sqs_queue.queue.tags, "marbot:approximate-age-of-oldest-message", var.approximate_age_of_oldest_message)
approximate_age_of_oldest_message_threshold = try(tonumber(lookup(data.aws_sqs_queue.queue.tags, "marbot:approximate-age-of-oldest-message:threshold", var.approximate_age_of_oldest_message_threshold)), var.approximate_age_of_oldest_message_threshold)
approximate_age_of_oldest_message_period_raw = try(tonumber(lookup(data.aws_sqs_queue.queue.tags, "marbot:approximate-age-of-oldest-message:period", var.approximate_age_of_oldest_message_period)), var.approximate_age_of_oldest_message_period)
approximate_age_of_oldest_message_period = min(max(floor(local.approximate_age_of_oldest_message_period_raw / 60) * 60, 60), 86400)
approximate_age_of_oldest_message_evaluation_periods_raw = try(tonumber(lookup(data.aws_sqs_queue.queue.tags, "marbot:approximate-age-of-oldest-message:evaluation-periods", var.approximate_age_of_oldest_message_evaluation_periods)), var.approximate_age_of_oldest_message_evaluation_periods)
approximate_age_of_oldest_message_evaluation_periods = min(max(local.approximate_age_of_oldest_message_evaluation_periods_raw, 1), floor(86400 / local.approximate_age_of_oldest_message_period))

approximate_number_of_messages_visible = lookup(data.aws_sqs_queue.queue.tags, "marbot:approximate-number-of-messages-visible", var.approximate_number_of_messages_visible)
approximate_number_of_messages_visible_threshold = try(tonumber(lookup(data.aws_sqs_queue.queue.tags, "marbot:approximate-number-of-messages-visible:threshold", var.approximate_number_of_messages_visible_threshold)), var.approximate_number_of_messages_visible_threshold)
approximate_number_of_messages_visible_period_raw = try(tonumber(lookup(data.aws_sqs_queue.queue.tags, "marbot:approximate-number-of-messages-visible:period", var.approximate_number_of_messages_visible_period)), var.approximate_number_of_messages_visible_period)
approximate_number_of_messages_visible_period = min(max(floor(local.approximate_number_of_messages_visible_period_raw / 60) * 60, 60), 86400)
approximate_number_of_messages_visible_evaluation_periods_raw = try(tonumber(lookup(data.aws_sqs_queue.queue.tags, "marbot:approximate-number-of-messages-visible:evaluation-periods", var.approximate_number_of_messages_visible_evaluation_periods)), var.approximate_number_of_messages_visible_evaluation_periods)
approximate_number_of_messages_visible_evaluation_periods = min(max(local.approximate_number_of_messages_visible_evaluation_periods_raw, 1), floor(86400 / local.approximate_number_of_messages_visible_period))
}

##########################################################################
Expand All @@ -23,14 +42,14 @@ locals {
##########################################################################

resource "aws_sns_topic" "marbot" {
count = (var.create_topic && var.enabled) ? 1 : 0
count = (var.create_topic && local.enabled) ? 1 : 0

name_prefix = "marbot"
tags = var.tags
}

resource "aws_sns_topic_policy" "marbot" {
count = (var.create_topic && var.enabled) ? 1 : 0
count = (var.create_topic && local.enabled) ? 1 : 0

arn = join("", aws_sns_topic.marbot.*.arn)
policy = data.aws_iam_policy_document.topic_policy.json
Expand Down Expand Up @@ -72,7 +91,7 @@ data "aws_iam_policy_document" "topic_policy" {

resource "aws_sns_topic_subscription" "marbot" {
depends_on = [aws_sns_topic_policy.marbot]
count = (var.create_topic && var.enabled) ? 1 : 0
count = (var.create_topic && local.enabled) ? 1 : 0

topic_arn = join("", aws_sns_topic.marbot.*.arn)
protocol = "https"
Expand All @@ -98,7 +117,7 @@ JSON

resource "aws_cloudwatch_event_rule" "monitoring_jump_start_connection" {
depends_on = [aws_sns_topic_subscription.marbot]
count = (var.module_version_monitoring_enabled && var.enabled) ? 1 : 0
count = (var.module_version_monitoring_enabled && local.enabled) ? 1 : 0

name = "marbot-sqs-queue-connection-${random_id.id8.hex}"
description = "Monitoring Jump Start connection (created by marbot)"
Expand All @@ -107,7 +126,7 @@ resource "aws_cloudwatch_event_rule" "monitoring_jump_start_connection" {
}

resource "aws_cloudwatch_event_target" "monitoring_jump_start_connection" {
count = (var.module_version_monitoring_enabled && var.enabled) ? 1 : 0
count = (var.module_version_monitoring_enabled && local.enabled) ? 1 : 0

rule = join("", aws_cloudwatch_event_rule.monitoring_jump_start_connection.*.name)
target_id = "marbot"
Expand All @@ -116,7 +135,7 @@ resource "aws_cloudwatch_event_target" "monitoring_jump_start_connection" {
{
"Type": "monitoring-jump-start-tf-connection",
"Module": "sqs-queue",
"Version": "0.7.0",
"Version": "1.0.0",
"Partition": "${data.aws_partition.current.partition}",
"AccountId": "${data.aws_caller_identity.current.account_id}",
"Region": "${data.aws_region.current.name}"
Expand All @@ -138,17 +157,17 @@ resource "random_id" "id8" {

resource "aws_cloudwatch_metric_alarm" "approximate_age_of_oldest_message" {
depends_on = [aws_sns_topic_subscription.marbot]
count = (var.approximate_age_of_oldest_message_threshold >= 0 && var.enabled) ? 1 : 0
count = (local.approximate_age_of_oldest_message == "static" && local.enabled) ? 1 : 0

alarm_name = "marbot-sqs-queue-message-age-${random_id.id8.hex}"
alarm_description = "Queue contains old messages. Is message processing failing or is the message procesing capacity too low? (created by marbot)"
namespace = "AWS/SQS"
metric_name = "ApproximateAgeOfOldestMessage"
statistic = "Maximum"
period = 60
evaluation_periods = 1
period = local.approximate_age_of_oldest_message_period
evaluation_periods = local.approximate_age_of_oldest_message_evaluation_periods
comparison_operator = "GreaterThanThreshold"
threshold = var.approximate_age_of_oldest_message_threshold
threshold = local.approximate_age_of_oldest_message_threshold
alarm_actions = [local.topic_arn]
ok_actions = [local.topic_arn]
dimensions = {
Expand All @@ -162,17 +181,17 @@ resource "aws_cloudwatch_metric_alarm" "approximate_age_of_oldest_message" {

resource "aws_cloudwatch_metric_alarm" "approximate_number_of_messages_visible" {
depends_on = [aws_sns_topic_subscription.marbot]
count = (var.approximate_number_of_messages_visible_threshold >= 0 && var.enabled) ? 1 : 0
count = (local.approximate_number_of_messages_visible == "static" && local.enabled) ? 1 : 0

alarm_name = "marbot-sqs-queue-length-${random_id.id8.hex}"
alarm_description = "Queue contains too many messages. Is message processing failing or is the message procesing capacity too low? (created by marbot)"
namespace = "AWS/SQS"
metric_name = "ApproximateNumberOfMessagesVisible"
statistic = "Maximum"
period = 60
evaluation_periods = 1
period = local.approximate_number_of_messages_visible_period
evaluation_periods = local.approximate_number_of_messages_visible_evaluation_periods
comparison_operator = "GreaterThanThreshold"
threshold = var.approximate_number_of_messages_visible_threshold
threshold = local.approximate_number_of_messages_visible_threshold
alarm_actions = [local.topic_arn]
ok_actions = [local.topic_arn]
dimensions = {
Expand Down
72 changes: 56 additions & 16 deletions variables.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
# We can not only check the var.topic_arn !="" because of the Terraform error: The "count" value depends on resource attributes that cannot be determined until apply, so Terraform cannot predict how many instances will be created.
variable "create_topic" {
type = bool
description = "Create SNS topic? If set to false you must set topic_arn as well!"
default = true
}

variable "topic_arn" {
type = string
description = "Optional SNS topic ARN if create_topic := false (usually the output of the modules marbot-monitoring-basic or marbot-standalone-topic)."
default = ""
}

variable "stage" {
type = string
description = "marbot stage (never change this!)."
default = "v1"
}

variable "endpoint_id" {
type = string
description = "Your marbot endpoint ID (to get this value: select a channel where marbot belongs to and send a message like this: \"@marbot show me my endpoint id\")."
Expand Down Expand Up @@ -26,33 +45,54 @@ variable "queue_name" {
description = "The SQS queue name that you want to monitor."
}



variable "approximate_age_of_oldest_message" {
type = string
description = "Old messages (static|off)."
default = "static"
}

variable "approximate_age_of_oldest_message_threshold" {
type = number
description = "The maximum age (in seconds) of a message in the queue (set to -1 to disable)."
description = "The maximum age (in seconds) of a message in the queue (>= 0)."
default = 600
}

variable "approximate_number_of_messages_visible_threshold" {
variable "approximate_age_of_oldest_message_period" {
type = number
description = "The maximum number of messages in the queue waiting for processing (set to -1 to disable)"
default = 10
description = "The period in seconds over which the specified statistic is applied (<= 86400 and multiple of 60)."
default = 60
}

# We can not only check the var.topic_arn !="" because of the Terraform error: The "count" value depends on resource attributes that cannot be determined until apply, so Terraform cannot predict how many instances will be created.
variable "create_topic" {
type = bool
description = "Create SNS topic? If set to false you must set topic_arn as well!"
default = true
variable "approximate_age_of_oldest_message_evaluation_periods" {
type = number
description = "The number of periods over which data is compared to the specified threshold (>= 1 and $period*$evaluation_periods <= 86400)."
default = 1
}

variable "topic_arn" {


variable "approximate_number_of_messages_visible" {
type = string
description = "Optional SNS topic ARN if create_topic := false (usually the output of the modules marbot-monitoring-basic or marbot-standalone-topic)."
default = ""
description = "Waiting messages (static|off)."
default = "static"
}

variable "stage" {
type = string
description = "marbot stage (never change this!)."
default = "v1"
variable "approximate_number_of_messages_visible_threshold" {
type = number
description = "The maximum number of messages in the queue waiting for processing (>= 0)."
default = 10
}

variable "approximate_number_of_messages_visible_period" {
type = number
description = "The period in seconds over which the specified statistic is applied (<= 86400 and multiple of 60)."
default = 60
}

variable "approximate_number_of_messages_visible_evaluation_periods" {
type = number
description = "The number of periods over which data is compared to the specified threshold (>= 1 and $period*$evaluation_periods <= 86400)."
default = 1
}

0 comments on commit bba4822

Please sign in to comment.