Select different region on serverless based on branch

Viewed 276

I'm relative new on Serverless, I'm building now a lambda function and I need to deploy that same function on different stages and also different regions, for example for development stage I need to deploy to us-est-1 region and for production stage I need to deploy to a different region, how can I do that using my branches, for example when I do a merge to develop use us-est-1 region and then when code is merge to master branch use us-est-2 region?

Thanks in advance

1 Answers

You can use a bash script and override the default serverless yl file definition.

for instance:

provider:
  stage: dev
  region: us-west-1

then the script will check the branch and set environnement variables to override the default value (dev and us-est-1)

#!/usr/bin/env bash
set -e

BRANCH=$(git rev-parse --abbrev-ref HEAD)
MASTER='master'
DEVELOP='develop'

if [[ $BRANCH == $MASTER ]]; then
  STAGE="prod"
  AWS_REGION="us-west-2"
elif [[ $BRANCH == $DEVELOP ]]; then
  STAGE="dev"
  AWS_REGION="us-west-1"
fi

if [ -z ${STAGE+x} ]; then
  echo "Not deploying changes"
  exit 0
fi

echo "Prepare dependencies"
npm install

echo "Deploying from branch $BRANCH to stage $STAGE in region $AWS_REGION"

npx serverless deploy --stage $STAGE --region $AWS_REGION

Related