Building a Gitlab pipeline for serverless aka containerizing the Serverless Framework for the win.
My team at SCCA has been migrating our two main applications to the serverless architecture for the past year and a half and we have learned a ton along the way. This post focuses on building a deployment pipeline for the Serverless Framework using Gitlab and Aws.
Readers should have some familiarity with the serverless architecture, the Serverless Framework, Docker and build pipelines.
The general idea is to run the Serverless Framework inside of Docker and push it to an image repository (in our case ecr). Doing this has a few advantages:
1. You can reference the same build across your pipeline
2. Your deployments run in a consistent way in your pipeline
3. The same build is used for all deployment to all environments
4. You save images with all your past deployed code
Running Serverless Framework in Docker
Let’s start by taking a look at the docker file.
Nothing too crazy here.
Start with a base image. Install git, node, and yarn. Do the yarn install. Install serverless. Copy your files. And finally set serverless as the entry point.
Use this Docker image in Gitlab pipeline
For simplicity I am just going to show one env and some unit tests.
At the build stage it builds the docker image and then pushes it to ecr. Each stage after that pulls down the image and runs what it needs to run. The serverless deployment will be done from inside of Docker.
Enhancements
This can be enhanced with additional image tagging. Deployment for integration tests. I also recommend creating some ecr cleanup rules so that you don’t have to manage it.
Conclusion
This is a pattern that has work well for us. I plan to publish more on serverless. If you like this post give it clap and pass it along. This will encourage me to make more post like this one.
If you are doing something similar or have improvement suggestions please add a comment. :)