serverless-apigateway-service-proxy icon indicating copy to clipboard operation
serverless-apigateway-service-proxy copied to clipboard

converting apigateway queryparams to sqs Message Body object

Open 2dev2 opened this issue 6 years ago • 4 comments

hi,

I am try to make get Rest api call with queryparams from api gateway to sqs. I want to insert some predefined query params from apigateway to sqs as part of message body, How we can achieved that..

?text=this_is_test_message&id=3

in the sqs we should be able to store this data as an object {text:this_is_test_message, id:3}

Please give some idea how we can achieved this.

2dev2 avatar Oct 23 '19 14:10 2dev2

Hey @2dev2 I think once #46 is merged you should be able to do something like this:

custom:
  apiGatewayServiceProxies:
    - sqs:
        path: /sqs
        method: get
        queueName: { 'Fn::GetAtt': ['SqsQueue', 'QueueName'] }
        cors: true
        acceptParameters:
          'method.request.querystring.body': true
        requestParameters:
          'integration.request.querystring.MessageBody': 'method.request.querystring.body'

resources:
  Resources:
    SqsQueue:
      Type: 'AWS::SQS::Queue'

Then send a GET request to https://*******.execute-api.us-east-1.amazonaws.com/dev/sqs?body={ "text": "this_is_test_message", "id": 3 }

Will that work for you?

erezrokah avatar Oct 26 '19 16:10 erezrokah

Thanks for looking into it and proposing the solution. But in Our case we don't have control over get API query structure, so it will not work for us.

In our case we are integrating with third party and they can't change their get query params structure is there any way to accept multiple query-params and convert them to integration MessageBody, like aws apigateway does in integration part.

can we pass requestTemplate and explicitly convert to sqs api request format.

2dev2 avatar Oct 26 '19 16:10 2dev2

Currently the plugin doesn't support using request templates for SQS: https://github.com/horike37/serverless-apigateway-service-proxy/blob/f36ef477116fb897aa4f21aa50a26eac1feac46c/lib/package/sqs/compileMethodsToSqs.js#L63

I'll have to look into it first to see if I can get SQS service proxy working with a request template instead request parameters.

Another (ugly) workaround might be to do the following:

custom:
  apiGatewayServiceProxies:
    - sqs:
        path: /sqs
        method: get
        queueName: { 'Fn::GetAtt': ['SqsQueue', 'QueueName'] }
        cors: true
        acceptParameters:
          'method.request.querystring.text': true
          'method.request.querystring.id': true
        requestParameters:
          'integration.request.querystring.MessageBody': "''"
          'integration.request.querystring.MessageAttribute.1.Name': "'text'"
          'integration.request.querystring.MessageAttribute.1.Value.StringValue': 'method.request.querystring.text'
          'integration.request.querystring.MessageAttribute.1.Value.DataType': "'String'"
          'integration.request.querystring.MessageAttribute.2.Name': "'id'"
          'integration.request.querystring.MessageAttribute.2.Value.StringValue': 'method.request.querystring.id'
          'integration.request.querystring.MessageAttribute.2.Value.DataType': "'Number'"

resources:
  Resources:
    SqsQueue:
      Type: 'AWS::SQS::Queue'

erezrokah avatar Oct 26 '19 16:10 erezrokah

@erezrokah Thanks for Providing solution, we have tried this but its working in get call

  1. For Post Method API call with query params , this is working fine. means we can pass query params in post call and in SQS event.MessageAttribute they will be present.
  2. For Get Method API call, serverless.yml got deployed but from AWS APIGATEWAY , when making get call its throwing following error with status code 403 in html format

Bad request. We can't connect to the server for this app or website at this time. There might be too much traffic or a configuration error. Try again later, or contact the app or website owner.
If you provide content to customers through CloudFront, you can find steps to troubleshoot and help prevent this error by reviewing the CloudFront documentation.Generated by cloudfront (CloudFront) Request ID: xxxxxxxxxxxxx==

2dev2 avatar Nov 03 '19 14:11 2dev2