Introduction
Welcome to this practical blog, where we’ll take a closer look at deploying an example NodeJS Lambda application across several AWS Regions.
Growth of Serverless It is undeniable that Serverless has revolutionized the field of application development. Although the advantages, disadvantages, and pros of Serverless have been widely covered elsewhere, let’s concentrate on the fascinating process of installing and operating apps utilizing this ground-breaking methodology. I suggest reading the following blog for individuals looking for a theoretical knowledge of Serverless.
For a better understanding, we recommend to check out before: Serverless Framework On AWS: A Beginners Guide
Benefits of deploying your application in multiple aWS regions
The Advantages of Deploying Your Application in Multiple AWS Regions
Discover the untapped potential of deploying your application in multiple AWS regions, as we explore the array of benefits it offers. Experience improved performance, heightened high availability and disaster recovery, compliance adherence, global scalability, localized data processing, and efficient load balancing, all while taking your application to new heights of success.
Key Benefits:
- Performance Optimization and Latency Reduction: Seamlessly position your application closer to end-users in various regions, ensuring lightning-fast response times and unparalleled user experiences.
- Uninterrupted Availability and Business Continuity: Embrace enhanced high availability and disaster recovery capabilities, allowing your application to seamlessly failover during outages, guaranteeing uninterrupted service and uninterrupted business continuity.
- Compliance and Data Sovereignty: Comply with strict data storage and processing regulations by strategically deploying your application in specific regions, mitigating legal risks and ensuring compliance.
- Global Scalability and Expansion: Efficiently scale your application by leveraging multiple regions, effectively reaching a global user base, and penetrating new markets.
- Enhanced Data Processing and Privacy: Process data closer to end-users, ensuring data privacy, reducing latency, and fortifying security measures.
- Optimal Load Balancing and Traffic Distribution: Leverage the power of multiple regions to achieve efficient load balancing, maximize resource utilization, and effortlessly scale your application.
By taking advantage of the many benefits that come with deploying your application across several AWS regions, you may achieve great performance, unshakable availability, compliance adherence, and global scalability. Advance your application by providing unmatched user experiences, fulfilling legal obligations, and extending its worldwide reach. Utilize this chance to strategically deploy your application across several AWS regions, opening up new possibilities for its success.
Benefits of SAM
AWS Serverless Application Model. “Build serverless applications in simple and clean syntax”.
An open-source framework for creating serverless apps is the AWS Serverless Application Model (SAM). It offers a concise syntax for expressing functions, APIs, databases, and mappings of event sources. You may create and model the application you want using YAML with just a few lines per resource. You can create serverless apps more quickly since SAM extends and translates the SAM syntax into AWS CloudFormation syntax during deployment.
SAM enables us to develop templates for the configuration and definitions we desire for our functions, the API Gateway, and even for authentication, logging, and monitoring using AWS Resources. In the end, it just generates several CloudFormation stacks, the AWS Infrastructure as Code solution.
Deploying a multi-region application
There’s a guide available on deploying a SAM template in multiple regions, using a Node.js simple API as an example. The source code for the application can be found here: Github Repository
The application itself was created using the powerful sam init command, which allows developers to generate pre-defined production-grade templates for building serverless architectures effortlessly.
Our REST API application serves to receive requests and return the region where it is currently running. This information is obtained from the Lambda AWS environment variables that are automatically injected during the build and deployment process.
To facilitate the deployment in multiple regions, we make use of the .github/workflows/deployment_job.yml file. This file acts as the configuration hub for our deployment process, where all necessary settings for deployment are defined. The only configuration required in this file is the setup of GitHub secrets for the AWS Access Keys, specifically AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY.
To execute the deployment, the workflow begins by setting up the AWS credentials environment. It then proceeds to build the application using SAM commands. Finally, it initiates the deployment process to all six regions defined in the deployment command, utilizing TOML files stored in the regions folder.
In the regions folder, you will find individual TOML files, named samconfig-REGION.toml, which store the region-specific configurations. These files are referenced during the deployment command execution in the CI/CD pipeline.
By following this guide and leveraging the capabilities of AWS SAM CLI and GitHub Actions, you can seamlessly deploy your SAM template in multiple AWS regions, empowering your application to reach a wider audience while ensuring optimal performance and availability.
Explain how the deployment works
on:
push:
branches:
- master
jobs:
# This workflow contains a single job called "build"
build:
# The type of runner that the job will run on
runs-on: ubuntu-latest
# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v2
# Configures AWS credentials from github secrets
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: us-east-1
- name: sam build
uses: TractorZoom/sam-cli-action@master
with:
sam_command: "build"
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
# Deploy on North California
- name: sam deploy north california
uses: youyo/aws-sam-action/python3.7@master
with:
sam_command: 'deploy --stack-name hello-world-multi-region --no-fail-on-empty-changeset --no-confirm-changeset --region us-west-1 --config-file ./regions/samconfig-north-california.toml'
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
# Deploy on North Virginia Region
- name: sam deploy north virginia
uses: youyo/aws-sam-action/python3.7@master
with:
sam_command: 'deploy --stack-name hello-world-multi-region --no-fail-on-empty-changeset --no-confirm-changeset --region us-east-1 --config-file ./regions/samconfig-north-virginia.toml'
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
# Deploy on Ohio Region
- name: sam deploy ohio
uses: youyo/aws-sam-action/python3.7@master
with:
sam_command: 'deploy --stack-name hello-world-multi-region --no-fail-on-empty-changeset --no-confirm-changeset --region us-east-2 --config-file ./regions/samconfig-ohio.toml'
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
# Deploy on Oregon Region
- name: sam deploy oregon
uses: youyo/aws-sam-action/python3.7@master
with:
sam_command: 'deploy --stack-name hello-world-multi-region --no-fail-on-empty-changeset --no-confirm-changeset --region us-west-2 --config-file ./regions/samconfig-oregon.toml'
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
With the power of GitHub Actions and CloudFormation, deploying your application to multiple AWS regions has never been easier. By simply pushing your code to the master branch, the deployment pipeline kicks off automatically and takes care of deploying your application to all six regions seamlessly.
Behind the scenes, CloudFormation handles the heavy lifting by updating the code of your Lambda Functions and configuring the AWS API Gateway to expose your application to the internet. This entire process is orchestrated by the steps defined in the .github/workflows/deployment_job.yml file, which is executed by GitHub Actions.
Through this setup, you can achieve widespread availability and improved performance for your application. By deploying to multiple regions, you ensure that your application can effectively serve users across different geographical locations. Additionally, this approach helps mitigate risks associated with single points of failure and provides redundancy for high availability.
The automation provided by GitHub Actions and CloudFormation simplifies your deployment workflow, allowing you to focus on developing your application rather than managing complex deployment processes. With just a single push to the master branch, your code is deployed to all regions, ensuring a consistent and reliable deployment across the board.
This streamlined deployment process eliminates the need for manual intervention, reducing the chance of errors and saving valuable time. You can confidently deliver updates to your application with speed and efficiency, knowing that it will be deployed consistently across all AWS regions.
After the initial deployment, we could go to the API Gateway console on each region and see our Application’s endpoint. As defined in the template:
[`https://$](https://%24/){ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/hello/`
Conclusion
In conclusion, using CloudFormation with GitHub Actions to deploy your application to many AWS regions has a number of advantages, including increased availability, performance, and resilience. Your ability to concentrate on developing your application while assuring a quick and easy deployment procedure is made possible by the tools’ seamless integration.