Skip to content

Commit 054ab60

Browse files
[PRMP-1469] NRL queue - DLQ (#241)
* [PRMP-1469] add dlq for nrl queue * [PRMP-1469] add alarm to dlq nrl --------- Co-authored-by: Steph Torres <stephane.torres1@nhs.net>
1 parent 05c32ac commit 054ab60

File tree

8 files changed

+91
-7
lines changed

8 files changed

+91
-7
lines changed

infrastructure/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@
110110
| <a name="module_nems-message-lambda"></a> [nems-message-lambda](#module\_nems-message-lambda) | ./modules/lambda | n/a |
111111
| <a name="module_nems-message-lambda-alarm"></a> [nems-message-lambda-alarm](#module\_nems-message-lambda-alarm) | ./modules/lambda_alarms | n/a |
112112
| <a name="module_nems-message-lambda-alarm-topic"></a> [nems-message-lambda-alarm-topic](#module\_nems-message-lambda-alarm-topic) | ./modules/sns | n/a |
113+
| <a name="module_nrl-dlq-alarm-topic"></a> [nrl-dlq-alarm-topic](#module\_nrl-dlq-alarm-topic) | ./modules/sns | n/a |
113114
| <a name="module_route53_fargate_ui"></a> [route53\_fargate\_ui](#module\_route53\_fargate\_ui) | ./modules/route53 | n/a |
114115
| <a name="module_search-document-references-gateway"></a> [search-document-references-gateway](#module\_search-document-references-gateway) | ./modules/gateway | n/a |
115116
| <a name="module_search-document-references-lambda"></a> [search-document-references-lambda](#module\_search-document-references-lambda) | ./modules/lambda | n/a |
@@ -206,6 +207,7 @@
206207
| [aws_cloudwatch_metric_alarm.edge_presign_lambda_error](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_metric_alarm) | resource |
207208
| [aws_cloudwatch_metric_alarm.error_log_alarm](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_metric_alarm) | resource |
208209
| [aws_cloudwatch_metric_alarm.inbox-messages-not-consumed](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_metric_alarm) | resource |
210+
| [aws_cloudwatch_metric_alarm.nrl_dlq_new_messages](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_metric_alarm) | resource |
209211
| [aws_cloudwatch_metric_alarm.sns_topic_error_log_alarm](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_metric_alarm) | resource |
210212
| [aws_ecs_cluster.mesh-forwarder-ecs-cluster](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ecs_cluster) | resource |
211213
| [aws_ecs_service.mesh_forwarder](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ecs_service) | resource |

infrastructure/modules/sns/README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ No modules.
1717
| Name | Type |
1818
|------|------|
1919
| [aws_sns_topic.sns_topic](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/sns_topic) | resource |
20-
| [aws_sns_topic_subscription.sns_subscription](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/sns_topic_subscription) | resource |
20+
| [aws_sns_topic_subscription.sns_subscription_list](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/sns_topic_subscription) | resource |
21+
| [aws_sns_topic_subscription.sns_subscription_single](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/sns_topic_subscription) | resource |
2122

2223
## Inputs
2324

@@ -27,10 +28,12 @@ No modules.
2728
| <a name="input_delivery_policy"></a> [delivery\_policy](#input\_delivery\_policy) | Attach delivery or IAM policy | `string` | n/a | yes |
2829
| <a name="input_enable_deduplication"></a> [enable\_deduplication](#input\_enable\_deduplication) | Prevent content based duplication in notification queue | `bool` | `false` | no |
2930
| <a name="input_enable_fifo"></a> [enable\_fifo](#input\_enable\_fifo) | Attach first in first out policy to notification queue | `bool` | `false` | no |
31+
| <a name="input_is_topic_endpoint_list"></a> [is\_topic\_endpoint\_list](#input\_is\_topic\_endpoint\_list) | n/a | `bool` | `false` | no |
3032
| <a name="input_raw_message_delivery"></a> [raw\_message\_delivery](#input\_raw\_message\_delivery) | n/a | `bool` | `false` | no |
3133
| <a name="input_sns_encryption_key_id"></a> [sns\_encryption\_key\_id](#input\_sns\_encryption\_key\_id) | n/a | `string` | n/a | yes |
3234
| <a name="input_sqs_feedback"></a> [sqs\_feedback](#input\_sqs\_feedback) | Map of IAM role ARNs and sample rate for success and failure feedback | `map(string)` | `{}` | no |
33-
| <a name="input_topic_endpoint"></a> [topic\_endpoint](#input\_topic\_endpoint) | n/a | `any` | n/a | yes |
35+
| <a name="input_topic_endpoint"></a> [topic\_endpoint](#input\_topic\_endpoint) | n/a | `any` | `null` | no |
36+
| <a name="input_topic_endpoint_list"></a> [topic\_endpoint\_list](#input\_topic\_endpoint\_list) | n/a | `any` | `[]` | no |
3437
| <a name="input_topic_name"></a> [topic\_name](#input\_topic\_name) | Name of the SNS topic | `string` | n/a | yes |
3538
| <a name="input_topic_protocol"></a> [topic\_protocol](#input\_topic\_protocol) | n/a | `string` | n/a | yes |
3639

infrastructure/modules/sns/main.tf

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,22 @@ resource "aws_sns_topic" "sns_topic" {
99
sqs_success_feedback_sample_rate = try(var.sqs_feedback.success_sample_rate, null)
1010
}
1111

12-
resource "aws_sns_topic_subscription" "sns_subscription" {
12+
resource "aws_sns_topic_subscription" "sns_subscription_single" {
13+
count = var.is_topic_endpoint_list ? 0 : 1
1314
topic_arn = aws_sns_topic.sns_topic.arn
1415
protocol = var.topic_protocol
1516
endpoint = var.topic_endpoint
1617
raw_message_delivery = var.raw_message_delivery
1718
}
1819

20+
resource "aws_sns_topic_subscription" "sns_subscription_list" {
21+
for_each = toset(var.topic_endpoint_list)
22+
topic_arn = aws_sns_topic.sns_topic.arn
23+
protocol = var.topic_protocol
24+
endpoint = each.value
25+
raw_message_delivery = var.raw_message_delivery
26+
}
27+
1928
output "arn" {
2029
value = aws_sns_topic.sns_topic.arn
2130
}

infrastructure/modules/sns/variable.tf

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,14 @@ variable "topic_protocol" {
2424
}
2525

2626
variable "topic_endpoint" {
27-
type = any
27+
type = any
28+
default = null
29+
}
30+
31+
32+
variable "topic_endpoint_list" {
33+
type = any
34+
default = []
2835
}
2936

3037
variable "current_account_id" {
@@ -43,4 +50,9 @@ variable "sqs_feedback" {
4350

4451
variable "raw_message_delivery" {
4552
default = false
46-
}
53+
}
54+
55+
variable "is_topic_endpoint_list" {
56+
default = false
57+
type = bool
58+
}

infrastructure/modules/sqs/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ No modules.
4646

4747
| Name | Description |
4848
|------|-------------|
49+
| <a name="output_dlq_name"></a> [dlq\_name](#output\_dlq\_name) | n/a |
4950
| <a name="output_endpoint"></a> [endpoint](#output\_endpoint) | Same as sqs queue arn. For use when setting the queue as endpoint of sns topic |
5051
| <a name="output_sqs_arn"></a> [sqs\_arn](#output\_sqs\_arn) | n/a |
5152
| <a name="output_sqs_id"></a> [sqs\_id](#output\_sqs\_id) | n/a |

infrastructure/modules/sqs/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ resource "aws_sqs_queue" "sqs_queue" {
2020

2121
resource "aws_sqs_queue" "queue_deadletter" {
2222
count = var.enable_dlq ? 1 : 0
23-
name = "${terraform.workspace}-${var.name}-deadletter-queue"
23+
name = "${terraform.workspace}-deadletter-${var.name}"
2424
delay_seconds = var.delay
2525
visibility_timeout_seconds = var.max_visibility
2626
max_message_size = var.max_size_message

infrastructure/modules/sqs/variable.tf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,7 @@ output "sqs_read_policy_document" {
9898

9999
output "sqs_write_policy_document" {
100100
value = data.aws_iam_policy_document.sqs_write_policy.json
101+
}
102+
output "dlq_name" {
103+
value = var.enable_dlq ? aws_sqs_queue.queue_deadletter[0].name : null
101104
}

infrastructure/sqs-nrl.tf

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,63 @@ module "sqs-nrl-queue" {
33
name = "nrl-queue.fifo"
44
environment = var.environment
55
owner = var.owner
6-
message_retention = 1800
6+
message_retention = 1209600
77
enable_sse = true
88
enable_fifo = true
99
max_visibility = 601
1010
enable_deduplication = true
11+
enable_dlq = true
12+
max_receive_count = 1
1113
}
14+
15+
resource "aws_cloudwatch_metric_alarm" "nrl_dlq_new_messages" {
16+
alarm_name = "${terraform.workspace}_NRL_dlq_messages"
17+
comparison_operator = "GreaterThanThreshold"
18+
evaluation_periods = 1
19+
metric_name = "ApproximateNumberOfMessagesVisible"
20+
namespace = "AWS/SQS"
21+
period = 60
22+
statistic = "Sum"
23+
threshold = 0
24+
alarm_description = "Alarm when there are new messages in the nrl dlq"
25+
alarm_actions = [module.nrl-dlq-alarm-topic.arn]
26+
27+
dimensions = {
28+
QueueName = module.sqs-nrl-queue.dlq_name
29+
}
30+
}
31+
32+
module "nrl-dlq-alarm-topic" {
33+
source = "./modules/sns"
34+
sns_encryption_key_id = module.sns_encryption_key.id
35+
current_account_id = data.aws_caller_identity.current.account_id
36+
topic_name = "nrl-dlq-topic"
37+
topic_protocol = "email"
38+
is_topic_endpoint_list = true
39+
topic_endpoint_list = nonsensitive(split(",", data.aws_ssm_parameter.cloud_security_notification_email_list.value))
40+
delivery_policy = jsonencode({
41+
"Version" : "2012-10-17",
42+
"Statement" : [
43+
{
44+
"Effect" : "Allow",
45+
"Principal" : {
46+
"Service" : "cloudwatch.amazonaws.com"
47+
},
48+
"Action" : [
49+
"SNS:Publish",
50+
],
51+
"Condition" : {
52+
"ArnLike" : {
53+
"aws:SourceArn" : "arn:aws:cloudwatch:eu-west-2:${data.aws_caller_identity.current.account_id}:alarm:*"
54+
}
55+
}
56+
"Resource" : "*"
57+
}
58+
]
59+
})
60+
61+
depends_on = [module.sqs-nrl-queue]
62+
}
63+
64+
65+

0 commit comments

Comments
 (0)