AWS SAM Template – Define SQS queue triggered by API Gateway

Question:

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):

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

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!

Answer:

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.

Leave a Reply