6

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 Answers3

6

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.

James Hood
  • 61
  • 4
  • 1
    I was wondering if it was possible to define it implicitly (as per the Lambda) but couldn't find anything about and hence I tried using `Event`. Seems like I'll have to follow this path and define the API GW as a Resource then. Cheers for the suggestion about exporting the definition – Salvatore Q Zeroastro Oct 31 '18 at 09:15
  • 1
    You're welcome to open an issue asking for this feature on the [SAM GitHub repo](https://github.com/awslabs/serverless-application-model/issues). – James Hood Nov 01 '18 at 17:00
  • 1
    I found this helpful (under section Building HTTP APIs direct integrations) https://aws.amazon.com/blogs/compute/building-storage-first-applications-with-http-apis-service-integrations/ – Ryan Jan 10 '22 at 10:49
0

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

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – MD. RAKIB HASAN Dec 13 '21 at 11:51
-2

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.

tyron
  • 3,715
  • 1
  • 22
  • 36