2 Ways of Automating Lambda Deployment with Bitbucket Pipeline

Chandara Chea
3 min readJul 19, 2020

--

Before diving into the automating of Lambda deployment with Bitbucket pipeline, let’s take a brief introduction of what is Lambda and Bitbucket pipeline.

Bitbucket pipelines logo

What is AWS Lambda ?

AWS Lambda is the AWS-managed service running Functions-as-a-Service. AWS Lambda lets you run code without provisioning or managing servers and pay only for the compute time you consume. You can run code for virtually any type of application or backend service with zero administration.

What is Bitbucket Pipeline ?

Bitbucket Pipelines is the Continuous Integration/Continuous Delivery (CI/CD) pipeline integrated into Bitbucket. It makes easy for teams to get up and running building, testing, and deploying their code without setting up their own CI servers.

Assume a scenarios that we want to build a pipeline that :

  • run test on any new branch that has been created — to create PR against development branch
  • run test and deploy from development branch when code is merged — deploy to development environment

Here are two simple ways to make automated lambda deployment with bitbucket pipeline:

1. Serverless Framework

Serverless Framework is a free and open-source web framework written using Node. js. Serverless is the first framework developed for building applications on AWS Lambda.

Add your AWS credentials to Bitbucket Pipelines Environment Variables

  • AWS_ACCESS_KEY_ID: IAM user’s AWS access key.
  • AWS_SECRET_ACCESS_KEY: the IAM user’s AWS secret access key. Make sure that you save it as a secured variable.
  • AWS_DEFAULT_REGION: Your AWS region.

Add and commit bitbucket-pipeline.yml

# bitbucket-pipeline.ymlpipelines:
default: # any branch except defined branch below e.g feature br
- step:
script:
- npm ci
- npm test
- npm run lint
branches:
development: # development branch
- step:
script:
- npm ci
- npm test
- npm run lint
- step:
# Build and package the Lambda function.
name: Build and package
script:
- $BITBUCKET_CLONE_DIR/scripts/script.sh default
- sls deploy --stage dev

BITBUCKET_CLONE_DIR is the absolute path of the directory that the repository is cloned into within the Docker container.

Create script.sh file

# scripts/script.sh
#!/usr/bin/env bashPROFILE=$1apt-get update && apt-get install -y python python-dev python-pip
pip install awscli
npm install -g serverless@1.50.1
npm ci
aws configure set aws_access_key_id $AWS_ACCESS_KEY --profile $PROFILEaws configure set aws_secret_access_key $AWS_SECRET_ACCESS_KEY --profile $PROFILEaws configure set $PROFILE.region $REGIONexport AWS_PROFILE=$PROFILE

2. Bitbucket Pipes

Bitbucket Pipes is a new feature in Bitbucket which can automate Lambda deployments on AWS.

Add your AWS credentials to Bitbucket Pipelines Environment Variables

  • AWS_ACCESS_KEY_ID: IAM user’s AWS access key.
  • AWS_SECRET_ACCESS_KEY: the IAM user’s AWS secret access key. Make sure that you save it as a secured variable.
  • AWS_DEFAULT_REGION: Your AWS region.

Add and commit bitbucket-pipeline.yml

# bitbucket-pipeline.ymlpipelines:
default: # any branch except defined branch below e.g feature br
- step:
script:
- npm ci
- npm test
- npm run lint
branches:
development: # development branch
- step:
script:
- npm ci
- npm test
- npm run lint
- step:
# Build and package the Lambda function.
name: Build and package
script:
- apt-get update && apt-get install -y zip
- zip code.zip index.js
artifacts:
- code.zip
- step:
name: Deploy to development environment
script:
- pipe: atlassian/aws-lambda-deploy:0.2.1
variables:
AWS_ACCESS_KEY_ID: $AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY: $AWS_SECRET_ACCESS_KEY
AWS_DEFAULT_REGION: $AWS_DEFAULT_REGION
FUNCTION_NAME: 'my-function'
COMMAND: 'update'
ZIP_FILE: 'code.zip'

In this article, we have learned a simple ways to automate lambda deployment with bitbucket pipeline.

--

--