Hey,
I have not had so much time to do as many posts as I wanted since I started my recent project of my own app on AWS using serverless framework. As every good engineer while making my solution I looked at many open source options and serverless seemed to be really good … until I wanted to do something which no one seems to have been doing before ( every one of us know it right ? ) …
So what was that special thing ? Well nothing fancy – just wanted to “attach AWS API Gateway Basic Request Validation” … So I thought … how hard can it be 🙂
As everyone I used “google” to tell me who has done something like that before …. and thats how I visited issue related directly to my problem => https://github.com/serverless/serverless/issues/3464
Had a conversation there with all interested and we all seemed to agree that there is nothing that would work ( at that specific moment 🙂 )
Well by default ( at the moment of writing of this article ) serverless does not support this “out of the box” therefore this has kicked my off to create my own plugin. Since being developer is not my primary focus ( as I do it only as hobbyist 🙂 ) it was a bit of challenge. But it was completed with success. So what was needed to happen to create it ( if you want to skip the story just scroll down 🙂 ) ?
I started by looking into creating a form of “hello world” plugin that would help me to understand how to approach this in best way. Resources for this can be found on the official site of serverless
- https://serverless.com/blog/writing-serverless-plugins/
- https://serverless.com/blog/writing-serverless-plugins-2/
But I had a feeling this was quite incomplete to create fully functional plugin. So I spend quite a while browsing internet and reading how people created their own plugins from really simple ones – into more advanced which rocks&roll 😉 Here I think the resource that will show you more detailed steps can be found here
With that bits of knowledge I went to the official repo page of plugins and browsed through repositories which were there. This gave me better idea about what I needed to use.
Having all that knowledge I compiled my action plan which basically was:
- Create plugin core structure
- Add resource to all functions of CloudFormation template based on serverless.yml
- Merge template on hook being called before:deploy
- Publish for people & get a beer 🙂
This is how I managed to create my plugin available in github https://github.com/RafPe/serverless-reqvalidator-plugin and via npm:
npm install serverless-reqvalidator-plugin
All it does require is extremely simple we start off by creating custom resource in serverless.yml
xMyRequestValidator: Type: "AWS::ApiGateway::RequestValidator" Properties: Name: 'my-req-validator' RestApiId: Ref: ApiGatewayRestApi ValidateRequestBody: true ValidateRequestParameters: false
With that one done we add plugin to be enabled
plugins: - serverless-reqvalidator-plugin
And then in our functions we specify validator
debug: handler: apis/admin/debug/debug.debug timeout: 10 events: - http: path: admin/debug method: get cors: true private: true reqValidatorName: 'xMyRequestValidator'
Voilla 😉 And the code in the plugin that does the magic ? You would not believe how much of code that is ….
resources[methodName].Properties.RequestValidatorId = {"Ref": `${event.http.reqValidatorName}`};
And thats it folks 😉 Enjoy and happy coding 🙂