DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Emin Buğra Saral
Emin Buğra Saral

Posted on • Originally published at saral.dev

Multi-Region Serverless deployments on AWS with custom domains

In this post, I will be extending the article on Serverless Framework which we use at FINN actively, and tell you how to implement a service with custom domains and multiple regions.

What is Serverless?

Serverless is a way of providing backend services only when needed for use (for our case: AWS Lambda). We will be using the Serverless Framework described as Zero-friction development tooling for auto-scaling apps on AWS Lambda.

It simply deploys your services to AWS Lambda and creates a restful API (your choice) for your service. You can attach a custom domain to any stage of your deployment.

Steps

Now let's deploy our application to both staging and production environments with their relevant custom domains.

In the end, we will have four deployments on both Europe (eu-central-1) and US East (us-east-1) regions with the routing setting based on latency:

  • Domain: stg-multi-region.saral.dev Stage: staging Region: eu-central-1
  • Domain: stg-multi-region.saral.dev Stage: staging Region: us-east-1
  • Domain: multi-region.saral.dev Stage: production Region: eu-central-1
  • Domain: multi-region.saral.dev Stage: production Region: us-east-1

1) Install Serverless plugins

Assuming you already installed serverless.
In order to manage custom domains, install serverless-domain-manager:

yarn add -D serverless-domain-manager
Enter fullscreen mode Exit fullscreen mode

Then install stage manager:

yarn add -D serverless-stage-manager
Enter fullscreen mode Exit fullscreen mode

2) Setup the serverless.yml

This is just an example.

service: multi-region-service

app: myapp
org: myorg

frameworkVersion: '2'

provider:
  name: aws
  endpointType: REGIONAL
  runtime: nodejs14.x
  memorySize: 256
  stage: ${opt:stage, 'staging'}
  region: ${opt:region, 'eu-central-1'}
  tags:
    author: saral.dev

custom:
  domains:
    production: multi-region.saral.dev
    staging: stg-multi-region.saral.dev
  environments:
    staging: staging
    production: production
  customDomain:
    domainName: ${self:custom.domains.${self:provider.stage}}
    basePath: ''
    stage: ${self:provider.stage}
    createRoute53Record: true
    endpointType: 'regional'
    certificateRegion: ${opt:region, 'eu-central-1'}
    route53Params:
      routingPolicy: latency
  stages:
    - staging
    - production

plugins:
  - serverless-domain-manager
  - serverless-stage-manager
  - serverless-offline
Enter fullscreen mode Exit fullscreen mode

3) Create domains with regions

  • Create stg-multi-region.saral.dev on eu-central-1 region:
serverless create_domain --stage staging
Enter fullscreen mode Exit fullscreen mode

or

serverless create_domain --stage staging --region eu-central-1
Enter fullscreen mode Exit fullscreen mode
  • Create stg-multi-region.saral.dev on us-east-1 region:
serverless create_domain --stage staging --region us-east-1
Enter fullscreen mode Exit fullscreen mode
  • Create multi-region.saral.dev on eu-central-1 region:
serverless create_domain --stage production
Enter fullscreen mode Exit fullscreen mode

or

serverless create_domain --stage production --region eu-central-1
Enter fullscreen mode Exit fullscreen mode
  • Create multi-region.saral.dev on us-east-1 region:
serverless create_domain --stage production --region us-east-1
Enter fullscreen mode Exit fullscreen mode

4) Deploy services to AWS

  • Deploy staging on eu-central-1 region:
serverless deploy --stage staging
Enter fullscreen mode Exit fullscreen mode

or

serverless deploy --stage staging --region eu-central-1
Enter fullscreen mode Exit fullscreen mode
  • Deploy staging on us-east-1 region:
serverless deploy --stage staging --region us-east-1
Enter fullscreen mode Exit fullscreen mode
  • Deploy production on eu-central-1 region:
serverless deploy --stage production
Enter fullscreen mode Exit fullscreen mode

or

serverless deploy --stage production --region eu-central-1
Enter fullscreen mode Exit fullscreen mode
  • Deploy production on us-east-1 region:
serverless deploy --stage production --region us-east-1
Enter fullscreen mode Exit fullscreen mode

How to convert an existing Edge service to Regional

By default, Serverless does not deploy regional endpoints. Let's assume you have a setup like this in your serverless.yml:

customDomain:
    domainName: ${self:custom.domains.${self:custom.stage}}
    basePath: ''
    stage: ${self:custom.stage}
    createRoute53Record: true
Enter fullscreen mode Exit fullscreen mode

Before you do any multi-region changes, you should first run the following commands and remove your custom domains because, at the moment, it's not possible to alter the endpoint type.

serverless delete_domain --stage staging
serverless delete_domain --stage production
Enter fullscreen mode Exit fullscreen mode

After that, you can start steps 3 and 4 above.

Unfortunately, there will be a downtime until your deployment is done.


If you want to log the region in your lambda, the environment variable AWS_REGION is available via AWS Lambda. So you can print it with console.log(process.env.AWS_REGION). You can also check your deployments on Serverless Dashboard.

I hope you enjoyed the post. Don't forget to show some love. :)

Top comments (0)

Want to Create an Account?
Now it's your turn!
Β 
πŸ—’ Share a tutorial
πŸ€” Reflect on your coding journey
❓ Ask a question

Create an account to join hundreds of thousands of DEV members on their journey.