Level Up Your Lambda Deployments with Serverless Framework
Fatima Medlij
Posted on October 3, 2023
Serverless Framework enables the creation of project stages for deployment. These stages serve as valuable tools for establishing separate environments dedicated to testing and development purposes. Typically, a staging environment is generated as a distinct replica of the production setup, facilitating comprehensive testing and verification of the code before final deployment.
In this piece, we’ll focus on setting up multiple stages, “live” and “dev” environments, for deployment. The “dev” stage is typically used for testing and development purposes. The “live” stage is used for deploying the application to the live/production AWS account.
The development process for deploying Lambda functions can be outlined as follows:
Code Development and Local Testing
During this stage, developers write the Lambda function code and perform thorough testing locally to ensure its correctness and efficiency.
Deployment to “dev” Testing Environment
Once the code is deemed suitable for production, it is deployed to the “dev” stage within the testing environment. Here, Quality Assurance (QA) teams can thoroughly test the Lambda function to identify any potential issues or bugs.
Confirmation and Deployment to “live” Stage
After successful testing, the Lambda function is deployed to the “live” stage. At this point, it becomes accessible to end-users.
Serverless architecture simplifies the process of deploying Lambda functions to different stages. There are two viable options to achieve this:
Single AWS Account with Naming Convention
In this approach, a single AWS account is used for both development/testing and production environments. The differentiation between the two stages is primarily achieved through the naming convention of lambda functions. Specifically, functions intended for the “dev” stage are named accordingly, while those intended for the “live” stage have different naming conventions.
In your Lambda serverless.yml add the following (default stage is “dev”)
service: testing-lambda
provider:ya
name: aws
runtime: nodejs16.x
region: ur-region
stage: ${opt:stage, 'dev'}
environment:
host: ${self:custom.database.${self:provider.stage}.host}
user: ${self:custom.database.${self:provider.stage}.username}
password: ${self:custom.database.${self:provider.stage}.password}
functions:
func_name:
handler: handler.func_name
role: ${self:custom.lambdaRole.${self:provider.stage}}
custom:
lambdaRole:
dev: arn:aws:iam::123456789:role/dev-lambdaRole
live: arn:aws:iam::123456789:role/live-lambdaRole
database:
dev:
host:"testing-cluster.ur-region.rds.amazonaws.com"
username: "username-dev"
password: "pwd-dev"
live:
host:"live-cluster.ur-region.rds.amazonaws.com"
username: "username-live"
password: "pwd-live"
to use your lambda to the “live” stage add the --stage live option.
serverless invoke local -f func_name -- stage live
serverless deploy -- stage live
Separate AWS Accounts for Testing and Production
Alternatively, you can opt for having two separate AWS accounts — one dedicated to testing and the other for production. In this setup, “dev” or testing Lambda functions are deployed to the designated AWS testing account. Once they pass all necessary tests and validations, they are then published on the live/production AWS account, making them accessible to end-users.
To use stages across different accounts you have to:
Setup the testing account by running aws configure with --profile option and a name for the new profile then add the necessary details.
aws configure --profile testing
In your Lambda serverless.yml use the following template
service: testing-lambda
provider:
name: aws
runtime: nodejs16.x
region: ur-region
stage: ${opt:stage, 'dev'}
# new line here
profile: ${self:custom.awsProfile.${self:provider.stage}}
environment:
host: ${self:custom.database.${self:provider.stage}.host}
user: ${self:custom.database.${self:provider.stage}.username}
password: ${self:custom.database.${self:provider.stage}.password}
functions:
func_name:
handler: handler.func_name
role: ${self:custom.lambdaRole.${self:provider.stage}}
custom:
lambdaRole:
dev: arn:aws:iam::123456789:role/dev-lambdaRole
live: arn:aws:iam::123456789:role/live-lambdaRole
database:
dev:
host:"testing-cluster.ur-region.rds.amazonaws.com"
username: "username-dev"
password: "pwd-dev"
live:
host:"live-cluster.ur-region.rds.amazonaws.com"
username: "username-live"
password: "pwd-live"
# new lines here
awsProfile:
dev: testing
live: default
Here the serverless framework will detect the AWS account and use it to deploy, your “live” and “dev” lambdas will be on different accounts.
It is essential to carefully consider the specific needs and requirements of the project to choose the most appropriate approach for deploying Lambda functions to different stages effectively.
Conclusion
The Serverless Framework offers efficient solutions for deploying Lambda functions to distinct stages, facilitating testing and development. Whether using a single AWS account with naming conventions or separate accounts for testing and production, developers can streamline the deployment process while maintaining high-quality applications.
Posted on October 3, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.