AWS SAM Template - Define SQS queue triggered by API Gateway
Asked Answered
H

3

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!

Haircut answered 30/10, 2018 at 16:56 Comment(0)
H
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.

Hereunto answered 31/10, 2018 at 8:52 Comment(3)
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 definitionHaircut
You're welcome to open an issue asking for this feature on the SAM GitHub repo.Hereunto
I found this helpful (under section Building HTTP APIs direct integrations) aws.amazon.com/blogs/compute/…Crossjack
O
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

Odelet answered 13/12, 2021 at 11:39 Comment(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.Valerianaceous
P
-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.

Prospector answered 30/10, 2018 at 22:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.