Create an AWS based serverless backend in a couple of minutes!
I’m not joking nor exaggerated when saying a couple of minutes, yes I’ve measure the time 120 secs. Here is the summary of the entry so you can try it and not waste your time.
Initialize your new backend project choosing a memorable name
npm create @durazno-technologies/dzn brujaMiruca
Enter into your brand new project folder and learn how to create a new entity
cd bruja-miruca && npx new-dzn-entity
Generate a new entity with your fresh knowledge from above
npx new-dzn-entity dino dinosaurs “id?:string;name:string;horns?:number;isExtinct:boolean”
The new entity dinosaurs has been generated. You can CRUD your entity in your local machine now using
yarn start
But we’ve made a promise, 120 secs, so let’s skip testing it in our local machines and better deploying directly to the amazon cloud
yarn deploy
That’s it! Now the full story just for very curious people…
The journey to choose the “right” tech stack
There are tens of good technology frameworks out there to choose so you can pick them and start writing your backend, just to name a few we have the Enterprise ones:
- C# with .NET from Microsoft
- Java with Spring and Hibernate from Oracle
- Go, the new contender by Google
After the Enterprises ones let’s present the cool kids; they are not as fast but you’ll have more fun using the newest trends:
- Nest.js the pinnacle of all the node.js based frameworks like express
- Django with the batteries included and all the power of Python3
- Laravel PHP is better than ever so we should give it a second try they say
Among all of these good technologies what would you choose and why? Our decision at Durazno Technologies was biased because all of our infrastructure was living on AWS, we were tired of the DevOps time wasted updating packages, applying patches, checking security breaches, installing web servers, configure the reverse proxies, dealing with SSL certificates and all the stuff related to have a working CI/CD pipeline for every product we build. It’s humongous the time invested, if we measure it, doing all of the above steps if we want to deploy a simple REST API. The conclusion we find out was not trivial, we needed a backend generator tool. As it didn’t exist in the market with all the following requisites that we needed, the decision was then very clear, we had to create it and share it of course!
- Deployment for new projects should be measured in minutes, not hours nor days.
- The tool should abstract, not disable neither hide, all the complex parts behind running an API on the cloud. Just like netlify, vercel and heroku do.
- Export the generated endpoints to config files that can be consumed by Insomia or Postman
- Working with Serverless technologies is a MUST
Therefore the “right” tech stack for this severless & free & fast & robust & friendly & adaptable backend generator tool was…
Typescript: JavaScript but with types
Middy.js: the king of middlewares
Serverless: development solution for auto-scaling apps
Lambda: serverless computing platform
Node.js: JavaScript runtime environment
DynamoDB: MongoDB’s biggest headache
Faker: realistic fake data
At the end of the day the API that what we’re trying to deploy on the cloud will look like this… with one small lambda function per action, all of them orchestrated by API Gateway which demands an amazon-api-key header to work and optionally a JWT. There are 5 actions associated with the basic CRUD operations and each one should write/read to/from DyanamoDB storing the logs and stats inside CloudWatch.
Let’s analyze in deep the two commands that the package create-dzn exposes so we can understand what happens under the hood of the library and how it is capable of producing the desired infrastructure as output.
npm create @durazno-technologies/dzn brujaMiruca
- New folder named “brujaMiruca” is created
- The seed repository is cloned into the new folder
- Commands npm install and sls dynamodb install are executed
- package.json file is edited, name and author are replaced
- Same with serverless.yml file
- insomniaProject.yml file is also generated with a default User entity
If we import the insomniaProject.yml file into Insomnia this is the result
We can make it work locally by running yarn start and we can play around with our new toy like kids
Stop playing around. It’s time to dive into the second command
npx new-dzn-entity dino dinosaurs “id?:string;name:string;horns?:number;isExtinct:boolean”
By executing this command we will get an output like this explaining which files were modified and which ones were created
Note: A new insomnia.yaml file is generated inside serverless folder every time a new entity is created. You can import this file into insomnia and your new endpoints will be attached to your insomnia project which is awesome!
We didn’t reinvent the wheel
We just put together a mix of amazing technologies out there, paste them with glue, implemented architectural patterns, make it configurable and extendible so it can be used to solve a bunch of same problems-like and finally bundle it all into a beautiful distributable npm package.
Enjoy! We hope this CLI tool gives you happiness & joy and also could reduce your pain & frustration produced by the first time migrating from monolithic applications to serverlessland. We did have a lot of fun creating it and any feedback will be appreciated.