AWS SAM Template - Define SQS queue triggered by API Gateway

Viewed 2025

I'm facing a problem when trying to deploy my stack via AWS SAM CLI. I'm using the SAM simplified template which I package and deploy.

All I want is to create an SQS queue and implicitly create an API Gateway that will just put the payload into the queue.

This is what I tried so far (the piece of code where I define Queue + Api):

MyProjectQueue:
    Type: AWS::SQS::Queue
    Properties:
        Events:
            MyProjectApi:
                Type: Api
                Properties:
                    Path: /myproject/push
                    Method: post

All good when I run sam validate and sam package, but it fails when I run sam deploy. To fetch the error I used aws cloudformation describe-stack-events --stack-name myproject-stack

STACKEVENTS     
MyProjectQueue-CREATE_FAILED-2018-10-30T16:33:29.764Z       
MyProjectQueue                      
CREATE_FAILED   
Encountered unsupported property Events AWS::SQS::Queue arn:aws:cloudformation:eu-west-1:<MY_AWS>:stack/myproject-stack/<GIUD>     
myproject-stack  2018-10-30T16:33:29.764Z

It clearly says that Events it's not supported for AWS::SQS::Queue. But this works for Lambdas (resource type AWS::Serverless::Function) which is the reason why I tried this way.

But, if possible, I'd like to avoid having a lambda between the gateway and the queue.

Is it possible to define an API Gateway directly for the SQS Queue? And How?

Thanks!

3 Answers

The AWS::SQS::Queue resource type does not support an Events property like AWS::Serverless::Function. Amazon API Gateway does support resource methods directly calling another AWS service like SQS without the need for a Lambda function in between.

My recommendation is that you create a AWS::Serverless::Api resource in your SAM template that references an OpenAPI (Swagger) file defining the API resource methods. Then use the x-amazon-apigateway-integration OpenAPI extension to define the integration between the API resource method and an SQS queue.

I also recommend following the linked AWS documentation tip and use the console to define your integration with SQS first, then export it to an OpenAPI definition file. This will be easier than trying to write the OpenAPI file from scratch.

The error you are facing is expected. AWS::SQS::Queue doesn't support an Events property according its documentation, while AWS::Serverless::Function does.

Not sure if I fully understood your use case, but I suggest you take a look at the Events property of your function, as you should be able to define SQS as the Event Source.

Related