I have a requirement to trigger an AWS Lambda function when there some process pushes the message to Amazon SQS. I have following CloudFormation configuration. The other process is able to push the message to SQS but it is not triggering the Lambda function.
TestFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: abc/xxx/
Handler: com.test.TestHandler::handleRequest
Runtime: java11
MemorySize: 512
Timeout: 120
FunctionName: !Join [ "-", [ !Ref TestPrefix, "TestFunction" ] ]
Role: !Ref LambdaIAMRole
VpcConfig:
SecurityGroupIds:
- !Ref TestSecurityGroupId
SubnetIds: !Ref TestSubnetIds
Events:
MySQSEvent:
Type: SQS
Properties:
Queue: !GetAtt TestQueue.Arn
BatchSize: 1
TestQueue:
Type: AWS::SQS::Queue
Properties:
QueueName: !Ref TestQueueName
Enabled: true
MessageRetentionPeriod: 300
VisibilityTimeout: 900
2
Answers
You cannot trigger a lambda function directly from SQS. You need to create an Event Source Mapping.
You can do this Using EventBridge Pipes. It has worked for me!
EventBridge Pipes was announced in Reinvent 2022
The source will be SQS and Target will be Lambda.
Earlier this was not possible without Eventbridge pipes but now it is. You can take a look at various sources and target for eventbridge pipes
Just extend this to your CFn template: Cloudformation EventBridge Docs.