Day 47 - Integrating AWS SQS and SNS for Reliable Messaging

I am encountering the following error: ‘as e:’. However, upon inspecting the cloud console, all resources appear to be successfully provisioned. The error output lacks descriptive details, making it difficult to debug.

Variables de entorno

PREFIX=xfusion
CF_TEMPLATE=/root/$PREFIX-priority-stack.yml
STACK_NAME=$PREFIX-priority-stack
SQS_HIGH_NAME=$PREFIX-High-Priority-Queue
SQS_LOW_NAME=$PREFIX-Low-Priority-Queue
SNS_TOPIC_NAME=$PREFIX-Priority-Queues-Topic
LAMBDA_NAME=$PREFIX-priorities-queue-function
LAMBDA_FILE=/root/index.py
ROLE_NAME=lambda_execution_role
REGION=us-east-1

Capturar codigo

LAMBDA_CODE=$(cat $LAMBDA_FILE)

Generar template

cat << EOF > $CF_TEMPLATE
AWSTemplateFormatVersion: ‘2010-09-09’
Description: Prioridad de colas con SQS, SNS, and Lambda.

Resources:

-------------------------------------------------------------------

1. SQS Colas

-------------------------------------------------------------------

HighPriorityQueue:
Type: AWS::SQS::Queue
Properties:
QueueName: ${SQS_HIGH_NAME}

LowPriorityQueue:
Type: AWS::SQS::Queue
Properties:
QueueName: ${SQS_LOW_NAME}

-------------------------------------------------------------------

2. SNS TOPIC y SUBSCRIPCION CON POLITICAS DE FILTRO

-------------------------------------------------------------------

PriorityTopic:
Type: AWS::SNS::Topic
Properties:
TopicName: ${SNS_TOPIC_NAME}

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

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

Politica para SNS para enviear mensajes a la cola de alta prioridad

HighPriorityQueuePolicy:
Type: AWS::SQS::QueuePolicy
Properties:
Queues:
- !Ref HighPriorityQueue
PolicyDocument:
Version: ‘2012-10-17’
Statement:
- Effect: Allow
Principal:
Service: sns.amazonaws.com
Action: sqs:SendMessage
Resource: !GetAtt HighPriorityQueue.Arn
Condition:
ArnEquals:
aws:SourceArn: !Ref PriorityTopic

Politica para SNS para enviear mensajes a la cola de baja prioridad

LowPriorityQueuePolicy:
Type: AWS::SQS::QueuePolicy
Properties:
Queues:
- !Ref LowPriorityQueue
PolicyDocument:
Version: ‘2012-10-17’
Statement:
- Effect: Allow
Principal:
Service: sns.amazonaws.com
Action: sqs:SendMessage
Resource: !GetAtt LowPriorityQueue.Arn
Condition:
ArnEquals:
aws:SourceArn: !Ref PriorityTopic

-------------------------------------------------------------------

3. IAM ROLE PARA LAMBDA

-------------------------------------------------------------------

LambdaExecutionRole:
Type: AWS::IAM::Role
Properties:
RoleName: ${ROLE_NAME}
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

-------------------------------------------------------------------

4. LAMBDA FUNCTION

-------------------------------------------------------------------

PriorityLambdaFunction:
Type: AWS::Lambda::Function
Properties:
FunctionName: ${LAMBDA_NAME}
Runtime: python3.12
Handler: index.lambda_handler
MemorySize: 128
Timeout: 10
Role: !GetAtt LambdaExecutionRole.Arn
Environment:
Variables:
high_priority_queue: !Ref HighPriorityQueue
low_priority_queue: !Ref LowPriorityQueue
Code:
ZipFile: |
$(echo “$LAMBDA_CODE” | sed ‘s/^/ /’)

-------------------------------------------------------------------

5. SQS EVENT SOURCE MAPPINGS (TRIGGERS)

-------------------------------------------------------------------

HighPriorityEventSourceMapping:
Type: AWS::Lambda::EventSourceMapping
Properties:
BatchSize: 10
EventSourceArn: !GetAtt HighPriorityQueue.Arn
FunctionName: !Ref PriorityLambdaFunction
Enabled: true

LowPriorityEventSourceMapping:
Type: AWS::Lambda::EventSourceMapping
Properties:
BatchSize: 10
EventSourceArn: !GetAtt LowPriorityQueue.Arn
FunctionName: !Ref PriorityLambdaFunction
Enabled: true

Outputs:
TopicArn:
Value: !Ref PriorityTopic
HighQueueArn:
Value: !GetAtt HighPriorityQueue.Arn
LowQueueArn:
Value: !GetAtt LowPriorityQueue.Arn
EOF

Crear Stack con CloudFormation

aws cloudformation create-stack
–stack-name $STACK_NAME
–template-body file://$CF_TEMPLATE
–capabilities CAPABILITY_NAMED_IAM
–region $REGION

Esperar hasta que se cree

aws cloudformation wait stack-create-complete
–stack-name $STACK_NAME
–region $REGION

echo -e “\033[0;32mStack\033[0m \033[1;31m$STACK_NAME\033[0m \033[0;32mdesplegado con éxito.\033[0m”

As it lacks a descriptive error message, it’s hard to point out what could have gone wrong.
Did you get a chance to refer to the solution recorded here?
Try to see if it helps.

Thanks @Santosh_KodeKloud but it didn’t work for me, but I found a repository that helped me. I solved it with mirakib.
Looks like the error is because I set up triggers.