Salvatore Q Zeroastro
Salvatore Q Zeroastro

Reputation: 758

AWS SAM Template - Define SQS queue triggered by API Gateway

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!

Upvotes: 6

Views: 3059

Answers (3)

Nitheesh S Kumar
Nitheesh S Kumar

Reputation: 43

You might have already figured out the solution. For those who haven't, this functionality can be acheived by using x-amazon-apigateway-integration property of api gateway where API gateway acts as a proxy for pushing the payload to the SQS queue. For more explanation check this https://medium.com/@pranaysankpal/aws-api-gateway-proxy-for-sqs-simple-queue-service-5b08fe18ce50

Upvotes: 0

James Hood
James Hood

Reputation: 61

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.

Upvotes: 6

tyron
tyron

Reputation: 3865

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.

Upvotes: -2

Related Questions