Skip to content

Commit 0cba857

Browse files
committed
feat(sqs): add documentation for SQS request templates
1 parent be672a9 commit 0cba857

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ This Serverless Framework plugin supports the AWS service proxy integration feat
2828
- [Customizing API Gateway parameters](#customizing-api-gateway-parameters)
2929
- [Customizing request body mapping templates](#customizing-request-body-mapping-templates)
3030
- [Kinesis](#kinesis-1)
31+
- [SQS](#sqs-1)
3132
- [SNS](#sns-1)
3233

3334
## Install
@@ -155,6 +156,10 @@ custom:
155156
'integration.request.querystring.MessageAttribute.2.Value.DataType': "'String'"
156157
```
157158

159+
#### Customizing request body mapping templates
160+
161+
See the [SQS section](#sqs-1) under [Customizing request body mapping templates](#customizing-request-body-mapping-templates)
162+
158163
### S3
159164

160165
Sample syntax for S3 proxy in `serverless.yml`.
@@ -653,6 +658,36 @@ custom:
653658

654659
Source: [How to connect SNS to Kinesis for cross-account delivery via API Gateway](https://theburningmonk.com/2019/07/how-to-connect-sns-to-kinesis-for-cross-account-delivery-via-api-gateway/)
655660

661+
#### SQS
662+
663+
Customizing SQS request templates requires us to force all requests to use an `application/x-www-form-urlencoded` style body. The plugin sets the `Content-Type` to `application/x-www-form-urlencoded` for you, but API Gateway will still look for the template under the `application/json` request template type, so that is where you need to configure you request body in `serverless.yml`:
664+
665+
```yml
666+
custom:
667+
apiGatewayServiceProxies:
668+
- sqs:
669+
path: /{version}/event/receiver
670+
method: post
671+
queueName: { 'Fn::GetAtt': ['SqsQueue', 'QueueName'] }
672+
request:
673+
template:
674+
application/json: |-
675+
#set ($body = $util.parseJson($input.body))
676+
Action=SendMessage##
677+
&MessageGroupId=$util.urlEncode($body.event_type)##
678+
&MessageDeduplicationId=$util.urlEncode($body.event_id)##
679+
&MessageAttribute.1.Name=$util.urlEncode("X-Custom-Signature")##
680+
&MessageAttribute.1.Value.DataType=String##
681+
&MessageAttribute.1.Value.StringValue=$util.urlEncode($input.params("X-Custom-Signature"))##
682+
&MessageBody=$util.urlEncode($input.body)
683+
```
684+
685+
Note that the `##` at the end of each line is an empty comment. In VTL this has the effect of stripping the newline from the end of the line (as it is commented out), which makes API Gateway read all the lines in the template as one line.
686+
687+
Be careful when mixing additional `requestParameters` into your SQS endpoint as you may overwrite the `integration.request.header.Content-Type` and stop the request template from being parsed correctly. You may also unintentionally create conflicts between parameters passed using `requestParameters` and those in your request template. Typically you should only use the request template if you need to manipulate the incoming request body in some way.
688+
689+
Your custom template must also set the `Action` and `MessageBody` parameters, as these will not be added for you by the plugin.
690+
656691
#### SNS
657692

658693
Similar to the [Kinesis](#kinesis-1) support, you can customize the default request mapping templates in `serverless.yml` like so:

0 commit comments

Comments
 (0)