- June 22, 2018
- Posted by: iSummation Team
- Category: AWS
AWS Lambda lets you run code without provisioning or managing server. Since you are not managing server it is called “ServerLess” (yea, Serverless doesn’t mean no server). I think this is the way to handle microservices, mobile app API call where a developer does not want to manage own server.
Till now, I never get into Lambda as we are running huge enterprise application and cannot be convert everything into microservices. Also, I feel that managing those many microservices is more cumbersome compared to managing as single (lesser number of ) project.
Why Lambda now?
Well, many of our projects require to generate PDF and most of them we generate out of HTML. Earlier we were using PhantomJS and work fine. Recently started with Puppeteer (a Google Chrome headless browser) and we found work better and faster compare to PhantomJS. As in PDF generation, we have a large number of photos which taking huge CPU and affecting overall process of Application and that is the reason we decided to keep it outside of Application server and probably in a separate server or go with serverless Lambda.
Particularly for this case, Lambda fits better than spinning up a new server in terms of cost and availability as it will able to handle a large number of PDF generation request at the same time.
Installing SAM CLI
In an earlier stage of Lambda, it was difficult to build and test in a local environment and publish it to Lambda but “SAM Local” made this really easy to build and deploy from development computer.
Installation can be done with single command below, refer SAM CLI Requirement guide for more information.
After installation just test it is installed correctly. By
Now you can start project from scratch for supported platform by lambda. In my case, I have use nodejs8.10 runtime and you can find full project here on Github and feel free fork it.
Running On Local
Best things with SAM Local is it will provide you an exact same environment that Lambda has and really help to develop your microservices faster and troubleshoot easily without deploying on Lambda.
In command line, change directory to your project directory and run below command.
This will spin up docker container which match with runtime you have specified in template.yml and voila, it is done.
In my case, I am using Lambda function to generate pdf and API Gateway to call outside of AWS infrastructure or use an API service.
Before deploying on lambda you may need to build a package with the following command which will build (create zip version of the code) and upload it given s3 bucket and generate package.yaml for AWS Cloudformation.
You can use cloudformation deploy command by providing packaged.yaml in template and this will take care of everything that required to get my microservice running.
Yea, that is it. Now you are microservice running on lambda and can accessible via REST API endpoint.
There is more need to do for the security of API but I am leaving this for you ;).