Skip to content

Commit ff51808

Browse files
[PRMP-1579] Add alarm and DLQ to MNS (#256)
* [PRMP-1579] enable MNS queue and lambda for sandbox * [PRMP-1579] add kms action permission to policy * [PRMP-1579] add alarm to notify when items hit DLQ * [PRMP-1579] pre-commit * [PRMP-1579] adjust dimensions on mns dlq alarm * [PRMP-1579] address PR comments * [PRMP-1579] add indexing on resources * [PRMP-1579] add var for dlq visibility timeout and a retry count * [PRMP-1579] fix typo * [PRMP-1579] fix variable * [PRMP-1579] fix variable * [PRMP-1579] change max visibility * [PRMP-1579] adjust visibility timeout
1 parent ce033e2 commit ff51808

File tree

7 files changed

+81
-77
lines changed

7 files changed

+81
-77
lines changed

bootstrap/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
| Name | Version |
1111
|------|---------|
12-
| <a name="provider_aws"></a> [aws](#provider\_aws) | 5.70.0 |
12+
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 4.0 |
1313

1414
## Modules
1515

infrastructure/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
| Name | Version |
1010
|------|---------|
11-
| <a name="provider_aws"></a> [aws](#provider\_aws) | 5.86.1 |
11+
| <a name="provider_aws"></a> [aws](#provider\_aws) | 5.72.1 |
1212

1313
## Modules
1414

@@ -95,6 +95,7 @@
9595
| <a name="module_manage-nrl-pointer-alarm"></a> [manage-nrl-pointer-alarm](#module\_manage-nrl-pointer-alarm) | ./modules/lambda_alarms | n/a |
9696
| <a name="module_manage-nrl-pointer-alarm-topic"></a> [manage-nrl-pointer-alarm-topic](#module\_manage-nrl-pointer-alarm-topic) | ./modules/sns | n/a |
9797
| <a name="module_manage-nrl-pointer-lambda"></a> [manage-nrl-pointer-lambda](#module\_manage-nrl-pointer-lambda) | ./modules/lambda | n/a |
98+
| <a name="module_mns-dlq-alarm-topic"></a> [mns-dlq-alarm-topic](#module\_mns-dlq-alarm-topic) | ./modules/sns | n/a |
9899
| <a name="module_mns-notification-alarm"></a> [mns-notification-alarm](#module\_mns-notification-alarm) | ./modules/lambda_alarms | n/a |
99100
| <a name="module_mns-notification-alarm-topic"></a> [mns-notification-alarm-topic](#module\_mns-notification-alarm-topic) | ./modules/sns | n/a |
100101
| <a name="module_mns-notification-lambda"></a> [mns-notification-lambda](#module\_mns-notification-lambda) | ./modules/lambda | n/a |
@@ -216,6 +217,7 @@
216217
| [aws_cloudwatch_metric_alarm.edge_presign_lambda_error](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_metric_alarm) | resource |
217218
| [aws_cloudwatch_metric_alarm.error_log_alarm](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_metric_alarm) | resource |
218219
| [aws_cloudwatch_metric_alarm.inbox-messages-not-consumed](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_metric_alarm) | resource |
220+
| [aws_cloudwatch_metric_alarm.msn_dlq_new_message](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_metric_alarm) | resource |
219221
| [aws_cloudwatch_metric_alarm.nrl_dlq_new_messages](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_metric_alarm) | resource |
220222
| [aws_cloudwatch_metric_alarm.sns_topic_error_log_alarm](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_metric_alarm) | resource |
221223
| [aws_ecs_cluster.mesh-forwarder-ecs-cluster](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ecs_cluster) | resource |

infrastructure/lambda-mns-notification.tf

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module "mns-notification-lambda" {
2-
count = local.is_sandbox ? 0 : 1
2+
count = 1
33
source = "./modules/lambda"
44
name = "MNSNotificationLambda"
55
handler = "handlers.mns_notification_handler.lambda_handler"
@@ -29,13 +29,13 @@ module "mns-notification-lambda" {
2929
}
3030

3131
resource "aws_lambda_event_source_mapping" "mns_notification_lambda" {
32-
count = local.is_sandbox ? 0 : 1
32+
count = 1
3333
event_source_arn = module.sqs-mns-notification-queue[0].endpoint
3434
function_name = module.mns-notification-lambda[0].lambda_arn
3535
}
3636

3737
module "mns-notification-alarm" {
38-
count = local.is_sandbox ? 0 : 1
38+
count = 1
3939
source = "./modules/lambda_alarms"
4040
lambda_function_name = module.mns-notification-lambda[0].function_name
4141
lambda_timeout = module.mns-notification-lambda[0].timeout
@@ -46,7 +46,7 @@ module "mns-notification-alarm" {
4646
}
4747

4848
module "mns-notification-alarm-topic" {
49-
count = local.is_sandbox ? 0 : 1
49+
count = 1
5050
source = "./modules/sns"
5151
sns_encryption_key_id = module.sns_encryption_key.id
5252
current_account_id = data.aws_caller_identity.current.account_id
@@ -76,17 +76,18 @@ module "mns-notification-alarm-topic" {
7676
}
7777

7878
resource "aws_iam_policy" "kms_mns_lambda_access" {
79-
count = local.is_sandbox ? 0 : 1
79+
count = 1
8080

8181
name = "${terraform.workspace}_mns_notification_lambda_access_policy"
82-
description = "KMS policy to allow lambda to read MNS SQS messages"
82+
description = "KMS policy to allow lambda to read and write MNS SQS messages"
8383

8484
policy = jsonencode({
8585
Version = "2012-10-17"
8686
Statement = [
8787
{
8888
Action = [
8989
"kms:Decrypt",
90+
"kms:GenerateDataKey"
9091
]
9192
Effect = "Allow"
9293
Resource = module.mns_encryption_key[0].kms_arn

infrastructure/mns.tf

Lines changed: 64 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ data "aws_ssm_parameter" "mns_lambda_role" {
44

55

66
module "mns_encryption_key" {
7-
count = local.is_sandbox ? 0 : 1
7+
count = 1
88
source = "./modules/kms"
99
kms_key_name = "alias/mns-notification-encryption-key-kms-${terraform.workspace}"
1010
kms_key_description = "Custom KMS Key to enable server side encryption for mns subscriptions"
@@ -17,21 +17,24 @@ module "mns_encryption_key" {
1717
}
1818

1919
module "sqs-mns-notification-queue" {
20-
count = local.is_sandbox ? 0 : 1
21-
source = "./modules/sqs"
22-
name = "mns-notification-queue"
23-
max_size_message = 256 * 1024 # allow message size up to 256 KB
24-
message_retention = 60 * 60 * 24 * 14 # 14 days
25-
environment = var.environment
26-
owner = var.owner
27-
max_visibility = 1020
28-
delay = 60
29-
enable_sse = null
30-
kms_master_key_id = module.mns_encryption_key[0].id
20+
count = 1
21+
source = "./modules/sqs"
22+
name = "mns-notification-queue"
23+
max_size_message = 256 * 1024 # allow message size up to 256 KB
24+
message_retention = 60 * 60 * 24 * 14 # 14 days
25+
environment = var.environment
26+
owner = var.owner
27+
max_visibility = 901
28+
delay = 60
29+
enable_sse = null
30+
kms_master_key_id = module.mns_encryption_key[0].id
31+
enable_dlq = true
32+
dlq_visibility_timeout = 0
33+
max_receive_count = 3
3134
}
3235

3336
resource "aws_sqs_queue_policy" "mns_sqs_access" {
34-
count = local.is_sandbox ? 0 : 1
37+
count = 1
3538

3639
queue_url = module.sqs-mns-notification-queue[0].sqs_url
3740

@@ -49,3 +52,51 @@ resource "aws_sqs_queue_policy" "mns_sqs_access" {
4952
]
5053
})
5154
}
55+
56+
resource "aws_cloudwatch_metric_alarm" "msn_dlq_new_message" {
57+
alarm_name = "${terraform.workspace}_MNS_dlq_messages"
58+
comparison_operator = "GreaterThanThreshold"
59+
evaluation_periods = 1
60+
metric_name = "ApproximateNumberOfMessagesVisible"
61+
namespace = "AWS/SQS"
62+
period = 60
63+
statistic = "Sum"
64+
threshold = 0
65+
alarm_description = "Alarm for when there are new messages in the MNS DLQ"
66+
alarm_actions = [module.mns-dlq-alarm-topic.arn]
67+
68+
dimensions = {
69+
QueueName = module.sqs-mns-notification-queue[0].dlq_name
70+
}
71+
}
72+
73+
module "mns-dlq-alarm-topic" {
74+
source = "./modules/sns"
75+
sns_encryption_key_id = module.sns_encryption_key.id
76+
current_account_id = data.aws_caller_identity.current.account_id
77+
topic_name = "mns-dlq-topic"
78+
topic_protocol = "email"
79+
is_topic_endpoint_list = true
80+
topic_endpoint_list = nonsensitive(split(",", data.aws_ssm_parameter.cloud_security_notification_email_list.value))
81+
delivery_policy = jsonencode({
82+
"Version" : "2012-10-17",
83+
"Statement" : [
84+
{
85+
"Effect" : "Allow",
86+
"Principal" : {
87+
"Service" : "cloudwatch.amazonaws.com"
88+
},
89+
"Action" : [
90+
"SNS:Publish",
91+
],
92+
"Condition" : {
93+
"ArnLike" : {
94+
"aws:SourceArn" : "arn:aws:cloudwatch:eu-west-2:${data.aws_caller_identity.current.account_id}:alarm:*"
95+
}
96+
}
97+
"Resource" : "*"
98+
}
99+
]
100+
})
101+
depends_on = [module.sqs-mns-notification-queue[0]]
102+
}
Lines changed: 0 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +0,0 @@
1-
## Requirements
2-
3-
No requirements.
4-
5-
## Providers
6-
7-
| Name | Version |
8-
|------|---------|
9-
| <a name="provider_aws"></a> [aws](#provider\_aws) | n/a |
10-
11-
## Modules
12-
13-
No modules.
14-
15-
## Resources
16-
17-
| Name | Type |
18-
|------|------|
19-
| [aws_sqs_queue.queue_deadletter](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/sqs_queue) | resource |
20-
| [aws_sqs_queue.sqs_queue](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/sqs_queue) | resource |
21-
| [aws_sqs_queue_redrive_allow_policy.terraform_queue_redrive_allow_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/sqs_queue_redrive_allow_policy) | resource |
22-
| [aws_sqs_queue_redrive_policy.dlq_redrive](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/sqs_queue_redrive_policy) | resource |
23-
| [aws_iam_policy_document.sqs_read_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
24-
| [aws_iam_policy_document.sqs_write_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source |
25-
26-
## Inputs
27-
28-
| Name | Description | Type | Default | Required |
29-
|------|-------------|------|---------|:--------:|
30-
| <a name="input_delay"></a> [delay](#input\_delay) | The time in seconds that the delivery of all messages in the queue will be delayed | `number` | `0` | no |
31-
| <a name="input_enable_deduplication"></a> [enable\_deduplication](#input\_enable\_deduplication) | Prevent content based duplication in queue | `bool` | `false` | no |
32-
| <a name="input_enable_dlq"></a> [enable\_dlq](#input\_enable\_dlq) | n/a | `bool` | `false` | no |
33-
| <a name="input_enable_fifo"></a> [enable\_fifo](#input\_enable\_fifo) | Attach first in first out policy to sqs | `bool` | `false` | no |
34-
| <a name="input_enable_sse"></a> [enable\_sse](#input\_enable\_sse) | Enable server-side encryption (SSE) of message content with SQS-owned encryption keys, requires kms resource for queue | `bool` | `true` | no |
35-
| <a name="input_environment"></a> [environment](#input\_environment) | Tags | `string` | n/a | yes |
36-
| <a name="input_kms_master_key_id"></a> [kms\_master\_key\_id](#input\_kms\_master\_key\_id) | The ID of an AWS-managed customer master key (CMK) for Amazon SQS or a custom CMK | `string` | `null` | no |
37-
| <a name="input_max_receive_count"></a> [max\_receive\_count](#input\_max\_receive\_count) | n/a | `number` | `1` | no |
38-
| <a name="input_max_size_message"></a> [max\_size\_message](#input\_max\_size\_message) | Max message size in bytes before sqs rejects the message | `number` | `2048` | no |
39-
| <a name="input_max_visibility"></a> [max\_visibility](#input\_max\_visibility) | Time in seconds during which Amazon SQS prevents all consumers from receiving and processing the message | `number` | `30` | no |
40-
| <a name="input_message_retention"></a> [message\_retention](#input\_message\_retention) | Number of seconds sqs keeps a message | `number` | `86400` | no |
41-
| <a name="input_name"></a> [name](#input\_name) | n/a | `string` | n/a | yes |
42-
| <a name="input_owner"></a> [owner](#input\_owner) | n/a | `string` | n/a | yes |
43-
| <a name="input_receive_wait"></a> [receive\_wait](#input\_receive\_wait) | Number of seconds sqs will wait for a message when ReceiveMessage is received | `number` | `2` | no |
44-
45-
## Outputs
46-
47-
| Name | Description |
48-
|------|-------------|
49-
| <a name="output_dlq_name"></a> [dlq\_name](#output\_dlq\_name) | n/a |
50-
| <a name="output_endpoint"></a> [endpoint](#output\_endpoint) | Same as sqs queue arn. For use when setting the queue as endpoint of sns topic |
51-
| <a name="output_sqs_arn"></a> [sqs\_arn](#output\_sqs\_arn) | n/a |
52-
| <a name="output_sqs_id"></a> [sqs\_id](#output\_sqs\_id) | n/a |
53-
| <a name="output_sqs_read_policy_document"></a> [sqs\_read\_policy\_document](#output\_sqs\_read\_policy\_document) | n/a |
54-
| <a name="output_sqs_url"></a> [sqs\_url](#output\_sqs\_url) | n/a |
55-
| <a name="output_sqs_write_policy_document"></a> [sqs\_write\_policy\_document](#output\_sqs\_write\_policy\_document) | n/a |

infrastructure/modules/sqs/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ resource "aws_sqs_queue" "queue_deadletter" {
2222
count = var.enable_dlq ? 1 : 0
2323
name = "${terraform.workspace}-deadletter-${var.name}"
2424
delay_seconds = var.delay
25-
visibility_timeout_seconds = var.max_visibility
25+
visibility_timeout_seconds = var.dlq_visibility_timeout
2626
max_message_size = var.max_size_message
2727
message_retention_seconds = var.message_retention
2828
receive_wait_time_seconds = var.receive_wait

infrastructure/modules/sqs/variable.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ variable "enable_dlq" {
6666
default = false
6767
}
6868

69+
variable "dlq_visibility_timeout" {
70+
type = number
71+
default = 0
72+
}
73+
6974
# Tags
7075
variable "environment" {
7176
type = string

0 commit comments

Comments
 (0)