Support response/request templates for APIG in v1
Problem
APIG Response/Request templates options are now missing in V1, we need to bring back this functionality. In v0.5.6 we let users define their velocity templates as strings in the `s-function.json` json file, which was very hard to maintain. We can still do that in v1 using our `serverless.yaml` file, but it'd still be really hard to maintain all the templates in all endpoints in all functions in a single file. Someone suggested before that we should let users define their velocity template in a `.vm` file and just reference it in the APIG config object in `serverless.yaml` (or `s-function.json`). I think this will make it much easier to deal with APIG and will provide super flexibility. Another option is for Serverless to provide an abstraction layer on top of velocity templates so that users don't have to provide velocity templates at all, just some yaml config options that will be compiled into valid velocity templates. This will probably be better UX, but will be much less flexible, and users with specific needs might get stuck. Or maybe we can do something in between. Thoughts...? cc/ @flomotlik @pmuens @ac360 Parent Issue: https://github.com/serverless/serverless/issues/1408
Unverified for your environment
Select your OS to check compatibility.
1 Fix
Reintroduce Velocity Template Support in APIG v1
In version 1 of the API Gateway (APIG), the functionality to define response/request templates using velocity templates has been removed, leading to difficulties in managing templates within the 'serverless.yaml' file. The previous method in v0.5.6 allowed for easier management by separating templates into individual .vm files, which is not supported in v1.
Awaiting Verification
Be the first to verify this fix
- 1
Create .vm Template Files
Define your velocity templates in separate .vm files to enhance maintainability. Each template should correspond to a specific endpoint and be stored in a designated templates directory.
velocity## Example of a velocity template file (response.vm) #set($response = $input.json('$')) { "status": "$response.status", "message": "$response.message" } - 2
Update serverless.yaml to Reference .vm Files
Modify the serverless.yaml configuration to reference the newly created .vm files for each endpoint. This allows the APIG to utilize the templates without embedding them directly in the configuration file.
yamlfunctions: myFunction: handler: handler.myFunction events: - http: path: mypath method: post request: template: application/json: ${file(./templates/request.vm)} response: template: application/json: ${file(./templates/response.vm)} - 3
Implement Template Compilation Layer (Optional)
Consider developing an abstraction layer that allows users to define templates using simplified YAML configurations. This layer would convert these configurations into valid velocity templates, balancing ease of use and flexibility.
yaml## Example of a simplified YAML config responses: success: status: 200 message: 'Operation successful' - 4
Test the Configuration
Deploy the updated serverless configuration and test the endpoints to ensure that the templates are being applied correctly. Use tools like Postman or curl to validate the responses.
bashcurl -X POST https://your-api-endpoint/mypath -H 'Content-Type: application/json' -d '{"key":"value"}'
Validation
Confirm that the API Gateway returns responses formatted according to the defined velocity templates. Check the logs for any errors related to template processing.
Sign in to verify this fix
Environment
Submitted by
Alex Chen
2450 rep