Serverless Framework Integration - AWS Lambda

Hey All!

As more and more companies explore the blackhole of Serverless Technology, Providers are expanding their own version of serverless for their platforms (i.e. Lambda, Azure Functions, and Google Functions). Although Harness does have support for AWS Serverless, with Azure Functions and Google Functions on the short list for coming support, I’ve found that there is a growing framework that companies are leveraging for Serverless support: Serverless Framework.

The advantage of Serverless Framework is that it not only packages your serverless function for you, but it will also deploy it, and, in some cases, give you the Infrastructure-as-code to create/update the stack associated with the serverless function. However, there are a few issues with it that I’ve noticed:

  • Developer Scalability
  • Function Scalability
  • Automated Rollbacks
  • Automated Verification
  • Roll-Based Access Control

Fortunately, Harness has all of these abilities.

Unfortunately, for those who have already spent time building out Serverless Framework, adopting Harness and Serverless Framework is difficult when trying to understand where one stops and the other starts.

But no more!

This article will be the first in a series of how to leverage Serverless Framework and Harness together, starting with AWS Lambda!

Delegate Profile

Serverless Framework requires a few things for it to work correctly, namely Node and NPM. Additionally, to do this whole integration, we will need Git, AWS CLI, and Python. The way we will start this out is with a delegate profile (assuming you have a delegate already stood up in the AWS VPC and the AWS Cloud Provider is setup in the Harness account. If not, then check out this doc first https://docs.harness.io/article/h9tkwmkrm7-delegate-installation and this doc second https://docs.harness.io/article/whwnovprrb-cloud-providers).

Go to Setup > Harness Delegates > Manage Delegate Profiles > Add Delegate Profile

You’ll put the name at the top (Example: ServerlessFramework) and then use this info:

apt-get update

# Install Git with auto approval
echo "Installing Git"
yes | apt-get -y install git
# Check git install
git --version

# Install Node
echo "Installing NodeJS"
apt-get -y install nodejs
node -v

# Install NPM
echo "Installing NPM"
apt-get -y install npm
npm -v

# Install Python
echo "Installing Python"
apt-get -y install python3
which python

# Install Serverless Framework
echo "Installing Serverless Framework"
npm install -g serverless
serverless -v

# Install AWS CLI
echo "Installing AWS CLI"
curl -L -O "https://s3.amazonaws.com/aws-cli/awscli-bundle.zip"
unzip awscli-bundle.zip
./awscli-bundle/install -b ~/bin/aws
# Check AWS CLI install
/root/bin/aws --version

Then, you’ll assign the profile to the desired delegate (look for a section in a Delegate in the delegate list called Profile). Once there is a timestamp and the logs for that show that everything downloaded correctly, you’re ready to move to the next step.


You’ll then want to add a tag to the delegate (something like “serverless”)

Application Template Library

This is the part where the main integration comes together between Serverless Framework and Harness.

The initial step here is actually outside of Harness. You’ll need to go to your S3 bucket and grant a specific user to upload a .ZIP file. Next, you’ll need to go to the git repo that stores your Serverless Function with the serverless.yaml file inside, grab the git clone link, and also get the Personal Access Token for the account as well.

Once inside Harness, you’ll need to set up three secrets in the Harness Secrets Manager:

  1. Git Repo API Token
  2. AWS Access Key
  3. AWS Secret Key

These will be used a little bit later for the packaging process.

The next step is to go to the Template Library for the Application in Harness and you’ll create a new Shell Script there (follow this doc if you need some guidance on that: https://docs.harness.io/article/ygi6d8epse-use-templates#create_shell_script_template).

This script will essentially be the Serverless Build and Package script that will finish with uploading the Lambda ZIP file to the S3 bucket and then Harness will deploy it out natively from there with your choice of verification (We will be using CloudWatch in this case).

This is the script you’ll want to use, and make sure you customize it for your specific use case and environment:

# AWS Credentials
echo "Setting up AWS credentials"
export AWS_ACCESS_KEY_ID=${awsAccessKey}
export AWS_SECRET_ACCESS_KEY=${awsSecretKey}
serverless config credentials --provider aws --key ${awsAccessKey} --secret ${awsSecretKey}

mkdir /tmp/${env.name}
cd /tmp/${env.name}
rm -rf *

# Clone Serverless Repo
echo "cloning Git Repo"
git clone https://${gitApiKey}@<git-repo>.git
cd <folder where serverless.yaml exists>

# Package Serverless Function
echo "Packaging Serverless Function"
serverless package --package ./

# Upload Serverless Package to S3
echo "Uploading Serverless Function to S3"
/root/bin/aws s3 cp ./aws-nodejs.zip s3://<s3 bucket name>/<sub folder>

You’ll want to test the git clone out first on your local computer to know which folder you’ll need to cd into in the script.

Once the script is done and added to the Template Library, it is time to put it in the Workflow

Also, one of the important pieces here is the three variables at the bottom of the screenshot. Make sure that they exist in the script as well.

Service

The rest of the process is essentially going to use native Harness abilities and allow for a separation between Serverless Framework as CI and Harness for CD. The initial step here is to upload a zip file as the Lambda artifact in the S3 bucket, if you have not already. Once this is done, we will build out a Harness Service for Lambda (follow this doc: https://docs.harness.io/article/qp8hk4nzbo-2-service-for-lambda).

Environment

The next portion is simply setting up the Environment(s) for the Lambda deployment. Follow this doc for more: https://docs.harness.io/article/45dm9z3m2h-3-lambda-environments

Workflow

The next piece is one of the main parts here, which is building out the Workflow for Lambda to deploy out for you. This doc will walk you through the main steps of building the workflow (https://docs.harness.io/article/491a6etr7a-4-lambda-workflows-and-deployments) and then we will add the Serverless Framework section as a pre-deploy step.

Additional Workflow Steps

Now that you have the Lambda deployment in the Workflow, you’ll want to add a pre-deployment step where we reference the Template Library script we built out before by clicking the Link item in the top right

Then, once that is linked to the workflow, you’ll want to link the secrets that we added in the beginning of the doc:

And you’ll also want to add the tag that you assigned to the delegate to the Tags section here to make sure that the script is executed on the same delegate that the Delegate Profile is assigned to.

Verification

Adding your verification process is pretty simple, especially with CloudWatch (which is the recommended Verification tool for Lambda, btw). Just follow this doc and you’ll get that set up: https://docs.harness.io/article/m5ra1x7qa0-5-verifications-and-troubleshooting

Deploy

The final step, and probably the most fun, is deploying! Kick it off and see how it works out. There might be a couple nuances to troubleshoot from the script, but other than that, you are all set!

1 Like

Thanks for the stellar contribution as always!

1 Like

Impecable timing with this article as well! So many customers have asked about the Serverless Framework and how to leverage it in Harness. Our Delegate can handle it!

1 Like