Bird
Raised Fist0
AWScloud~10 mins

SNS and SQS integration pattern (fan-out) in AWS - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create an SNS topic named 'MyTopic'.

AWS
resource "aws_sns_topic" "example" {
  name = [1]
}
Drag options to blanks, or click blank then click option'
A"MyBucket"
B"MyQueue"
C"MyFunction"
D"MyTopic"
Attempts:
3 left
💡 Hint
Common Mistakes
Using a queue or bucket name instead of a topic name.
Forgetting to put the name in quotes.
2fill in blank
medium

Complete the code to create an SQS queue named 'MyQueue'.

AWS
resource "aws_sqs_queue" "example" {
  name = [1]
}
Drag options to blanks, or click blank then click option'
A"MyTopic"
B"MyQueue"
C"MyBucket"
D"MyFunction"
Attempts:
3 left
💡 Hint
Common Mistakes
Using the SNS topic name instead of the queue name.
Not using quotes around the name.
3fill in blank
hard

Fix the error in the subscription resource to connect the SNS topic to the SQS queue.

AWS
resource "aws_sns_topic_subscription" "example" {
  topic_arn = aws_sns_topic.example.arn
  protocol  = [1]
  endpoint  = aws_sqs_queue.example.arn
}
Drag options to blanks, or click blank then click option'
A"sqs"
B"http"
C"email"
D"lambda"
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'http' or 'email' instead of 'sqs' as the protocol.
Using the queue URL instead of ARN for the endpoint.
4fill in blank
hard

Fill both blanks to allow the SNS topic to send messages to the SQS queue by setting the correct policy.

AWS
resource "aws_sqs_queue_policy" "example" {
  queue_url = aws_sqs_queue.example.url
  policy    = jsonencode({
    Version = "2012-10-17"
    Statement = [{
      Effect    = "Allow"
      Principal = "*"
      Action    = [1]
      Resource  = aws_sqs_queue.example.arn
      Condition = {
        ArnEquals = {
          "aws:SourceArn" = [2]
        }
      }
    }]
  })
}
Drag options to blanks, or click blank then click option'
A"sqs:SendMessage"
B"sns:Publish"
Caws_sns_topic.example.arn
Daws_sqs_queue.example.arn
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'sns:Publish' as the action instead of 'sqs:SendMessage'.
Using the queue ARN instead of the topic ARN in the condition.
5fill in blank
hard

Fill all three blanks to create a complete fan-out pattern with two SQS queues subscribed to one SNS topic.

AWS
resource "aws_sns_topic" "example" {
  name = [1]
}

resource "aws_sqs_queue" "queue1" {
  name = [2]
}

resource "aws_sqs_queue" "queue2" {
  name = [3]
}

resource "aws_sns_topic_subscription" "sub1" {
  topic_arn = aws_sns_topic.example.arn
  protocol  = "sqs"
  endpoint  = aws_sqs_queue.queue1.arn
}

resource "aws_sns_topic_subscription" "sub2" {
  topic_arn = aws_sns_topic.example.arn
  protocol  = "sqs"
  endpoint  = aws_sqs_queue.queue2.arn
}
Drag options to blanks, or click blank then click option'
A"FanOutTopic"
B"QueueOne"
C"MyQueue"
D"FanOutQueue"
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same name for topic and queue.
Not matching queue names with subscription endpoints.

Practice

(1/5)
1. What is the main purpose of using SNS with multiple SQS queues in a fan-out pattern?
easy
A. To send the same message to multiple queues for parallel processing
B. To store messages permanently in one queue
C. To send messages only to one queue at a time
D. To delete messages immediately after sending

Solution

  1. Step 1: Understand SNS role in fan-out

    SNS publishes messages to multiple subscribers simultaneously.
  2. Step 2: Understand SQS role in fan-out

    Each SQS queue receives and holds messages independently for processing.
  3. Final Answer:

    To send the same message to multiple queues for parallel processing -> Option A
  4. Quick Check:

    SNS fan-out = multiple queues get same message [OK]
Hint: SNS fan-out means one message to many queues [OK]
Common Mistakes:
  • Thinking SNS sends to only one queue
  • Confusing message storage with deletion
  • Assuming messages are deleted immediately
2. Which of the following is the correct way to subscribe an SQS queue to an SNS topic using AWS CLI?
easy
A. aws sns subscribe --topic-arn --protocol sqs --notification-endpoint
B. aws sqs subscribe --topic-arn --queue-url
C. aws sns create-subscription --topic --queue
D. aws sns add-queue --topic-arn --queue-arn

Solution

  1. Step 1: Identify correct AWS CLI command

    The AWS CLI command to subscribe is 'aws sns subscribe'.
  2. Step 2: Check parameters for SQS subscription

    Use '--protocol sqs' and '--notification-endpoint' with the queue ARN.
  3. Final Answer:

    aws sns subscribe --topic-arn <topic-arn> --protocol sqs --notification-endpoint <queue-arn> -> Option A
  4. Quick Check:

    Subscribe SQS to SNS uses 'aws sns subscribe' with protocol sqs [OK]
Hint: Subscribe SQS with 'aws sns subscribe' and protocol sqs [OK]
Common Mistakes:
  • Using 'aws sqs subscribe' which does not exist
  • Confusing queue URL with queue ARN
  • Using wrong command like 'aws sns add-queue'
3. Given an SNS topic with two subscribed SQS queues, if a message is published to SNS, what will be the state of the message in the queues?
medium
A. The message is deleted from SNS and not delivered to any queue.
B. The message appears in only one queue randomly.
C. The message appears in both queues independently, waiting to be processed.
D. The message is duplicated in one queue only.

Solution

  1. Step 1: Understand SNS fan-out delivery

    SNS sends a copy of the message to each subscribed queue.
  2. Step 2: Understand SQS message handling

    Each queue holds its own copy until processed or deleted.
  3. Final Answer:

    The message appears in both queues independently, waiting to be processed. -> Option C
  4. Quick Check:

    Fan-out means each queue gets its own message copy [OK]
Hint: SNS fan-out sends message copy to each queue [OK]
Common Mistakes:
  • Thinking message goes to only one queue
  • Assuming message is deleted immediately
  • Believing message duplicates in one queue only
4. You configured an SNS topic with two SQS queues subscribed, but only one queue receives messages. What is the most likely cause?
medium
A. SNS only sends messages to one queue by design
B. The second queue's subscription is not confirmed
C. The message size exceeds SNS limits
D. The first queue deleted the message too fast

Solution

  1. Step 1: Check subscription confirmation

    SQS subscriptions to SNS require confirmation to activate delivery.
  2. Step 2: Understand SNS delivery behavior

    Without confirmation, SNS does not send messages to that queue.
  3. Final Answer:

    The second queue's subscription is not confirmed -> Option B
  4. Quick Check:

    Unconfirmed subscription blocks message delivery [OK]
Hint: Confirm all subscriptions to receive messages [OK]
Common Mistakes:
  • Assuming SNS sends to only one queue
  • Blaming message size without checking subscription
  • Thinking message deletion affects delivery
5. You want to build a system where an SNS topic sends messages to three SQS queues. One queue processes orders, another sends notifications, and the third logs events. How should you configure the system to ensure reliable processing and no message loss?
hard
A. Subscribe only the order queue to SNS and forward messages to others manually
B. Send messages directly to each SQS queue without SNS
C. Use one SQS queue for all processing and filter messages inside the consumer
D. Subscribe all three SQS queues to the SNS topic, enable dead-letter queues for each, and set appropriate message retention

Solution

  1. Step 1: Use SNS fan-out to send messages to all queues

    Subscribing all three SQS queues to the SNS topic ensures each gets the message.
  2. Step 2: Enable dead-letter queues and set retention

    Dead-letter queues catch failed messages; retention ensures messages stay until processed.
  3. Final Answer:

    Subscribe all three SQS queues to the SNS topic, enable dead-letter queues for each, and set appropriate message retention -> Option D
  4. Quick Check:

    Fan-out + DLQ + retention = reliable, no message loss [OK]
Hint: Use fan-out with DLQ and retention for reliability [OK]
Common Mistakes:
  • Skipping dead-letter queues causing message loss
  • Using one queue for all processing causing bottlenecks
  • Not subscribing all queues to SNS topic
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard