BACKGROUND IMAGE: iSTOCK/GETTY IMAGES
As serverless technologies increasingly power critical production systems, it's important for dev teams to incorporate emerging best practices into their software delivery cycle.
The necessary steps to build and release serverless apps are often manual and tedious. Services such as AWS CodePipeline can simplify many of these steps and also add reliability and consistency to serverless deployments.
At a high level, developers who build serverless applications should:
- Have a local integrated development environment and a code versioning tool (most likely Git).
- Locally test code. To do this with Lambda functions, use Serverless Application Model (SAM) Local, or other tools, such as Python Lambda Local or NodeJS Lambda-local.
- Package and deploy code into one or more test environments for integration and load tests.
- Conduct code approvals at every stage of development, including production.
- Deploy into a production environment and have a way to roll back code in case of failure.
AWS CodePipeline capabilities
AWS CodePipeline automates all these required steps to detect code updates and to build, test and deploy application code. It identifies and orchestrates tasks that take place in other AWS offerings, such as S3, CodeCommit, CodeBuild, CodeDeploy, CloudFormation, Lambda and Simple Notification Service.
CodePipeline can deliver code into EC2 instances but also works with serverless components. It can automatically start a step based on events, such as a Git commit or a code package moving into an S3 bucket. CodePipeline can also integrate with a Jenkins server to automate build or integration test steps for Lambda functions.
In addition, it can invoke a Lambda function to trigger custom logic in any step of a pipeline to add flexibility to task orchestration.
AWS CodePipeline advantages
CodePipeline easily integrates with many native AWS offerings. This way, teams can either use CodePipeline's built-in steps or extend functionality through CloudWatch Events and Lambda functions.
CodePipeline also has built-in integration with GitHub but can integrate with other third-party code repositories. Users can create jobs in Bitbucket or GitLab to automatically publish code into an S3 bucket whenever a commit takes place. Once the code reaches S3, this turns into an automated pipeline trigger.
With CodeBuild, serverless code is packaged using an OS image similar to one that will run in AWS Lambda. This eliminates situations where code executes in a local environment, but not in Lambda -- an issue that commonly occurs when dev teams use third-party libraries and create packages in local workstations that run macOS or Windows.
CodePipeline offers several ways to roll back a deployment in case of failure. When developers use the service with a code repository, for example, they can revert to a specific commit. It's also possible to revert to a specific code package in S3, which has a versioned bucket requirement. If their pipeline uses CodeBuild artifacts, developers can also point to a specific build package they want to redeploy. Even better, CloudWatch metrics and alarms can automatically trigger a deployment rollback.
Furthermore, CodePipeline works seamlessly with AWS SAM. Teams can use CodeBuild to package code using SAM's CLI commands and eventually run the CloudFormation commands that SAM expects. This enables the code to deploy into Lambda.
For some, SAM is the preferred method to package and deploy serverless components. But users need to become familiar with the framework and the creation of SAM templates. It takes some time to use SAM properly, but it can be worth the investment.
As with any automation tool, AWS CodePipeline has its barriers to entry. There is a bit of a learning curve, and developers have to build pipelines for each application. Still, those willing to invest the time will appreciate the long-term benefits.