Skip to content

Commit

Permalink
SQS Component Refactor (#1042)
Browse files Browse the repository at this point in the history
  • Loading branch information
Benbentwo authored May 22, 2024
1 parent 0fb45d8 commit cd7eee3
Show file tree
Hide file tree
Showing 11 changed files with 217 additions and 564 deletions.
23 changes: 23 additions & 0 deletions modules/sqs-queue/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
## Pull Request [#1042](https://github.com/cloudposse/terraform-aws-components/pull/1042) - Refactor `sqs-queue` Component

Components PR [#1042](https://github.com/cloudposse/terraform-aws-components/pull/1042)

### Affected Components

- `sqs-queue`

### Summary

This change to the sqs-queue component, [#1042](https://github.com/cloudposse/terraform-aws-components/pull/1042),
refactored the `sqs-queue` component to use the AWS Module for queues, this provides better support for Dead-Letter
Queues and easy policy attachment.

As part of that change, we've changed some variables:

- `policy` - **Removed**
- `redrive_policy` - **Removed**
- `dead_letter_sqs_arn` - **Removed**
- `dead_letter_component_name` - **Removed**
- `dead_letter_max_receive_count` - Renamed to `dlq_max_receive_count`
- `fifo_throughput_limit` **type changed** from `list(string)` to type `string`
- `kms_master_key_id` **type changed** from `list(string)` to type `string`
90 changes: 64 additions & 26 deletions modules/sqs-queue/README.md

Large diffs are not rendered by default.

65 changes: 37 additions & 28 deletions modules/sqs-queue/main.tf
Original file line number Diff line number Diff line change
@@ -1,32 +1,51 @@
locals {
enabled = module.this.enabled
aws_account_number = one(data.aws_caller_identity.current[*].account_id)
policy_enabled = local.enabled && length(var.iam_policy) > 0
redrive_policy_enabled = local.enabled && (var.dead_letter_sqs_component_name != null || var.dead_letter_sqs_arn != null)
enabled = module.this.enabled
aws_account_number = one(data.aws_caller_identity.current[*].account_id)
policy_enabled = local.enabled && length(var.iam_policy) > 0
}

data "aws_caller_identity" "current" {
count = local.enabled ? 1 : 0
}
module "sqs" {
source = "terraform-aws-modules/sqs/aws"
version = "4.2.0"

name = module.this.id

module "sqs_queue" {
source = "./modules/terraform-aws-sqs-queue"
create_dlq = var.dlq_enabled
dlq_name = "${module.this.id}-${var.dlq_name_suffix}"
dlq_content_based_deduplication = var.dlq_content_based_deduplication
dlq_deduplication_scope = var.dlq_deduplication_scope
dlq_kms_master_key_id = var.dlq_kms_master_key_id
dlq_delay_seconds = var.dlq_delay_seconds
dlq_kms_data_key_reuse_period_seconds = var.dlq_kms_data_key_reuse_period_seconds
dlq_message_retention_seconds = var.dlq_message_retention_seconds
dlq_receive_wait_time_seconds = var.dlq_receive_wait_time_seconds
create_dlq_redrive_allow_policy = var.create_dlq_redrive_allow_policy
dlq_redrive_allow_policy = var.dlq_redrive_allow_policy
dlq_sqs_managed_sse_enabled = var.dlq_sqs_managed_sse_enabled
dlq_visibility_timeout_seconds = var.dlq_visibility_timeout_seconds
dlq_tags = merge(module.this.tags, var.dlq_tags)
redrive_policy = var.dlq_enabled ? {
maxReceiveCount = var.dlq_max_receive_count
} : {}

visibility_timeout_seconds = var.visibility_timeout_seconds
message_retention_seconds = var.message_retention_seconds
max_message_size = var.max_message_size
delay_seconds = var.delay_seconds
receive_wait_time_seconds = var.receive_wait_time_seconds
policy = try([var.policy[0]], [])
redrive_policy = try([var.redrive_policy[0]], [])
max_message_size = var.max_message_size
fifo_queue = var.fifo_queue
fifo_throughput_limit = try([var.fifo_throughput_limit[0]], [])
content_based_deduplication = var.content_based_deduplication
kms_master_key_id = try([var.kms_master_key_id[0]], [])
kms_master_key_id = var.kms_master_key_id
kms_data_key_reuse_period_seconds = var.kms_data_key_reuse_period_seconds
deduplication_scope = try([var.deduplication_scope[0]], [])
sqs_managed_sse_enabled = var.sqs_managed_sse_enabled
fifo_throughput_limit = var.fifo_throughput_limit
deduplication_scope = var.deduplication_scope

context = module.this.context
tags = module.this.tags
}

data "aws_caller_identity" "current" {
count = local.enabled ? 1 : 0
}

module "queue_policy" {
Expand All @@ -45,7 +64,7 @@ module "queue_policy" {
merge(
statement,
{
resources = [module.sqs_queue.arn]
resources = [module.sqs.queue_arn]
},
var.iam_policy_limit_to_current_account ? {
conditions = concat(statement.conditions, [
Expand All @@ -67,16 +86,6 @@ module "queue_policy" {
resource "aws_sqs_queue_policy" "sqs_queue_policy" {
count = local.policy_enabled ? 1 : 0

queue_url = module.sqs_queue.url
queue_url = module.sqs.queue_url
policy = one(module.queue_policy[*].json)
}

resource "aws_sqs_queue_redrive_policy" "dead_letter_queue" {
count = local.redrive_policy_enabled != null ? 1 : 0

queue_url = module.sqs_queue.url
redrive_policy = jsonencode({
deadLetterTargetArn = var.dead_letter_sqs_arn != null ? var.dead_letter_sqs_arn : one(module.dead_letter_sqs_remote_state[*].outputs.arn)
maxReceiveCount = var.dead_letter_max_receive_count
})
}
279 changes: 0 additions & 279 deletions modules/sqs-queue/modules/terraform-aws-sqs-queue/context.tf

This file was deleted.

Loading

0 comments on commit cd7eee3

Please sign in to comment.