4 Elements of A Great Serverless Application Deployment Strategy
Serverless is still relatively new and shiny, but deploying a serverless infrastructure introduces a host of novel considerations.
Join the DZone community and get the full member experience.Join For Free
serverless is the new buzzword in development, selling developers the ability to focus on writing applications instead of managing servers.
this is true for the most part, but serverless apps also have a certain property that can make their deployment and maintenance time-consuming. that is, they depend on services — lots of them. a serverless architecture is typically designed over services for storing data, load balancing, data caching, and code execution, just to name a few.
with that said, how do you both provision infrastructure and deploy code to test/staging/production environments in an automated and risk-minimized way?
if you can’t answer this question easily, or some part of your deploys still requires you to look at your cloud provider’s console, then this post is for you.
separate environments properly
to reduce the risk of integrating new code, most teams set up various environments to test the robustness of submitted code before it is pushed to production.
when configuring serverless architectures in these environments, special care needs to be taken in deciding whether a single stack or multi-stack approach architecture is best for your project. otherwise, you might be wasting a lot of time on deployments, or even worse, introducing unnecessary risk to your application.
the single stack approach shares its services and infrastructure across all environments (like testing, stag, ng and production). to differentiate between environments at runtime, environment variables or similar mechanisms are used.
in the context of a serverless app deployed on aws, a single stack setup might configure a single lambda function with multiple aliases, an api gateway with multiple stages, and a dynamodb service with multiple tables — all with the purpose of supporting multiple test, staging, and production environments.
in contrast, a multi-stack approach uses a separate instance of each service for every environment and does not use api stages or lambda aliases to differentiate between them.
using the aws example again, a multi-stack setup would configure a separate lambda function, api gateway, and dynamodb instance for each environment.
the main difference between the two approaches is that the multi-stack approach minimizes risk, while the single stack approach minimizes setup and maintenance effort.
if something goes wrong in a single stack approach, there is a greater chance that your production systems are negatively impacted as well. after all, the environments in this approach are provisioned on top of each other, and rely only on environment variables and lambda aliases for indirection.
in the multi-stack approach, this probability is decreased as a result of using separate resources for each environment. this advantage comes at a cost however — multi-stack approaches require more work to configure and maintain in comparison to single stack architectures. and in most scenarios, they are more expensive to run as well.
if you are in the early stages of your project, it might be a better idea for you to run with a single stack approach. otherwise, a multi-stack approach is the way to go.
the main idea behind continuous delivery is to produce production-ready artifacts from your code base frequently in an automated fashion. it ensures that code can be rapidly and safely deployed to production by delivering every change to a production-like environment and that any business applications and services function as expected through rigorous automated testing.
the most important point however, is that all of this must be automated.
using their front end web application, cloud providers such as amazon web services make it easy to spin off a new lambda function for testing purposes or to update a function’s application code. however, all aspects of your continuous delivery strategy should be automated — the only manual step should be the push of the
deploy to production
button. this is vital for many reasons:
- minimizing risk — even with proper resource access controls in place (which is seldom the case), the risk of accidentally causing bad things to happen are higher than you think. you definitely don’t want to be the reason for a disruption to your service.
- scalability — as the number of developers in the project grows, and by extension, the volume of code deliveries, any process that is not completely automated will likely slow down your team’s agility.
- traceability — when your continuous delivery strategy is an automated, often using an automated pipeline of some sort, the use of deployments, builds and other components are better documented, making it easier to resolve and recover from failures.
using bitbucket pipelines, i usually use the following skeleton pipeline in multi-environment, continuous delivery environments. first, it runs your static analysis tools and automated tests for every pull request. once the changes are deployed to master, it automatically updates the
environment. finally, the
environment is updated once the changes in
are known to be safe using a manual trigger. if you are using aws, checkout
for more information on the actual automated deployment of lambda functions.
pipelines: default: - step: name: run static analysis tools script: ... - step: name: run automated tests script: ... branches: master: - step: name: run static analysis tools script: ... - step: name: run automated tests script: ... - step: name: update staging deployment: staging script: (deploy to staging)... - step: name: update prod deployment: production trigger: manual script: (deploy to production)...
app configuration is stored in the environment
an app’s configuration consists of everything that is likely to vary between deploys (staging, production, developer environments, etc). this typically includes:
- resource handles to the database, cache or any other attached resources
- credentials to external services such as amazon s3 or twitter
- per-deploy values such as the canonical hostname for the deploy apps sometimes store config variables as constants in source code which is problematic for several reasons. first, any security sensitive related config data is visible to everyone who has access to the source code, which poses a serious security concern.
additionally, config varies substantially across deploys, whereas code does not. developing code that dynamically detects the environment and sets the appropriate config data as required results in code that is unnecessarily complicated and difficult to maintain, especially as the number of environments/deploys increases. on top of this, any required changes to an environment config requires pushing a change through the code base and waiting for it to propagate through the software delivery pipeline, which is tedious and slow.
rather than keeping your config data in the source code, consider using consider using a service dedicated for this purpose like aws secrets manager . or as a slightly less safe but commonly used alternative, store this data in environment variables. now your application can simply refer to abstract variables for config info, which are dynamically set by a build pipeline during deployment. in this way, environment variables can be leveraged to generate flexible and infinite variations (test, staging, prod, etc..) of your environment template. lastly, environment variables are os and language agnostic, which means they can easily be integrated across all types of projects.
infrastructure is provisioned and deployed as code
infrastructure as code (iac) is all about provisioning and updating entire workloads (applications, infrastructure) using code. the idea here is that the same engineering discipline that is typically used for application code can be applied to deploying and managing workloads as well. you can implement your operations procedures as code and automate their execution by triggering them in response to events.
by using code to automate the process of setting up and configuring a virtual machine or container for example, you have a fast and repeatable method for replicating the process. so if you build a virtual environment for the development of an application, you can repeat the process of creating that vm simply by running the same code once you are ready to deploy.
additionally, iac processes increase agility. developers don’t have to wait for however long the it department needs to provision a new vm for them to do work.
when it comes to practicing iac in the cloud, the serverless framework is a great tool for configuring serverless architectures. it’s a command line interface for building and deploying entire serverless applications through the use of configuration template files. it, along with many other services including aws’s serverless application model (sam), for example, provide a scalable and systematic solution to many of the operational complexities of multi-environment serverless architectures. the serverless framework is probably the most popular cloud provider-agnostic framework for configuring serverless applications and supports aws, microsoft azure, google cloud platform, ibm openwhisk, and more.
the typical serverless application uses a combination of cloud services, which often need to be configured and setup separately for each environment.
serverless makes this process much more straightforward and scalable by utilizing a component-based configuration where all the infrastructure and code for your entire application can be provisioned easily. not only do these components conveniently refer to commonly used serverless services (aws: s3 buckets, lambda functions, api gateways, etc..), they can be re-used and composed into higher level components to build entire serverless applications. components are open source, and there is lots of pre-built functionality developers can tap into right away.
as mentioned in the previous section, an automated continuous delivery solution is key for your serverless application. serverless frameworks can be easily integrated into ci/cd pipeline to deploy sophisticated serverless applications in one step, making it much easier to manage multi-environment configurations.
serverless applications often consist of plucking and combining various services and packages together which can make keeping track of everything a complex process, especially when running multi-environment setups.
in such scenarios, i feel it’s important that processes surrounding serverless architectures should be scalable, automated, and require little manual intervention. this list represents my thoughts on how to deal with the complexity.
if you think this list could benefit from other ideas or concepts i’ve missed, let me know in the comments below!
Published at DZone with permission of Muntazir Fadhel. See the original article here.
Opinions expressed by DZone contributors are their own.