Day 47: Integrating AWS SQS and SNS for Reliable Messaging

I did it via the cloud formation, even though messages are correctly getting processed, but the solution, when submitted, is failing with an exception. What am I doing wrong?

Here is my CF template
AWSTemplateFormatVersion: β€˜2010-09-09’
Description: Priority Queue Processing Stack
Resources:

HighPriorityQueue:
Type: AWS::SQS::Queue
Properties:
QueueName: datacenter-High-Priority-Queue
VisibilityTimeout: 60

LowPriorityQueue:
Type: AWS::SQS::Queue
Properties:
QueueName: datacenter-Low-Priority-Queue
VisibilityTimeout: 60

PriorityQueuesTopic:
Type: AWS::SNS::Topic
Properties:
TopicName: datacenter-Priority-Queues-Topic

HighPrioritySubscription:
Type: AWS::SNS::Subscription
Properties:
TopicArn: !Ref PriorityQueuesTopic
Protocol: sqs
Endpoint: !GetAtt HighPriorityQueue.Arn
FilterPolicy:
priority:
- high

LowPrioritySubscription:
Type: AWS::SNS::Subscription
Properties:
TopicArn: !Ref PriorityQueuesTopic
Protocol: sqs
Endpoint: !GetAtt LowPriorityQueue.Arn
FilterPolicy:
priority:
- low

HighPriorityPolicy:
Type: AWS::SQS::QueuePolicy
Properties:
Queues:
- !Ref HighPriorityQueue
PolicyDocument:
Statement:
- Effect: Allow
Principal: β€œ*”
Action: SQS:SendMessage
Resource: !GetAtt HighPriorityQueue.Arn
Condition:
ArnEquals:
aws:SourceArn: !Ref PriorityQueuesTopic

LowPriorityPolicy:
Type: AWS::SQS::QueuePolicy
Properties:
Queues:
- !Ref LowPriorityQueue
PolicyDocument:
Statement:
- Effect: Allow
Principal: β€œ*”
Action: SQS:SendMessage
Resource: !GetAtt LowPriorityQueue.Arn
Condition:
ArnEquals:
aws:SourceArn: !Ref PriorityQueuesTopic

LambdaExecutionRole:
Type: AWS::IAM::Role
Properties:
RoleName: lambda_execution_role
AssumeRolePolicyDocument:
Version: β€˜2012-10-17’
Statement:
- Effect: Allow
Principal:
Service: lambda.amazonaws.com
Action: sts:AssumeRole
ManagedPolicyArns:
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
- arn:aws:iam::aws:policy/AmazonSQSFullAccess
- arn:aws:iam::aws:policy/AmazonSNSFullAccess

PriorityLambdaFunction:
Type: AWS::Lambda::Function
Properties:
FunctionName: datacenter-priorities-queue-function
Runtime: python3.9
Handler: index.lambda_handler
Role: !GetAtt LambdaExecutionRole.Arn
Timeout: 5
Environment:
Variables:
high_priority_queue: !Ref HighPriorityQueue
low_priority_queue: !Ref LowPriorityQueue
Code:
S3Bucket: kklabsuser-911514
S3Key: function-code.zip

Usually if there’s an exception, it’s because the name of one of the resources embedded in the cloudformation yaml is not matching what we listed in the task. This would be my guess as to what the problem is.

try to refer to this solution

ok, Thanks was able to fix my template issue

1 Like

if you find my git kodekloud solutions repo helpful please give them a star , i will help me a lot