Search code examples
amazon-web-servicesterraformamazon-sqsamazon-snsterraform-provider-aws

SNS does not send messages to SQS


I have 2 SNS (order-finalized, customer-operations) and 2 SQS (notification-listen, customerPortal-listen) my team create these 4 resources via Terraform.

order-finalized(SNS) -> notification-listen(SQS) its crated 3 mounth ago and working fine.

customer-operations(SNS) -> customerPortal-listen(SQS) its crating now and messages do not publish to sqs.

SNS:

resource "aws_sns_topic" "order-finalized" {
  name              = "order-finalized"
  kms_master_key_id = "alias/aws/sns"
  tags = {
    Name        = "order-finalized",
    Environment = "dev"
  }
}

resource "aws_sns_topic" "customer-operations" {
  name              = "customer-operations"
  kms_master_key_id = "alias/aws/sns"
  tags = {
    Name        = "customer-operations",
    Environment = "dev"
  }
}

SQS:

resource "aws_sqs_queue" "notification-listen" {
  name                      = "notification-listen"
  delay_seconds             = 0
  max_message_size          = 2048
  message_retention_seconds = 86400
  receive_wait_time_seconds = 10
  redrive_policy = jsonencode({
    deadLetterTargetArn = aws_sqs_queue.deadletter.arn
    maxReceiveCount     = 1
  })

  tags = {
    Name        = "notification-listen"
    Environment = "dev"
  }
}

resource "aws_sqs_queue" "customerPortal-listen" {
  name                      = "customerPortal-listen"
  delay_seconds             = 0
  max_message_size          = 2048
  message_retention_seconds = 86400
  receive_wait_time_seconds = 10
  redrive_policy = jsonencode({
    deadLetterTargetArn = aws_sqs_queue.deadletter.arn
    maxReceiveCount     = 1
  })

  tags = {
    Name        = "customerPortal-listen"
    Environment = "dev"
  }
}

Subscriptions:

resource "aws_sns_topic_subscription" "order-finalized-target" {
  topic_arn = aws_sns_topic.order-finalized.arn
  protocol  = "sqs"
  endpoint  = aws_sqs_queue.notification-listen.arn
}


resource "aws_sns_topic_subscription" "customer-operations-target" {
  topic_arn = aws_sns_topic.customer-operations.arn
  protocol  = "sqs"
  endpoint  = aws_sqs_queue.customerPortal-listen.arn
}


Solution

  • Thanks to @ErvinSzilagyi

    I just add a policy and it solved my problem. I will post the terraform code for people who will search that think on google

    resource "aws_sqs_queue_policy" "customerPortal-listen-policy" {
      queue_url = aws_sqs_queue.customerPortal-listen.id
    
      policy = <<POLICY
    {
      "Version": "2012-10-17",
      "Id": "sqspolicy",
      "Statement": [
        {
          "Sid": "First",
          "Effect": "Allow",
          "Principal": "*",
          "Action": "sqs:SendMessage",
          "Resource": "${aws_sqs_queue.customerPortal-listen.arn}",
          "Condition": {
            "ArnEquals": {
              "aws:SourceArn": "${aws_sns_topic.customer-operations.arn}"
            }
          }
        }
      ]
    }
    POLICY
    }