AWS Partner Network (APN) Blog
Making Serverless CI/CD Easier with CircleCI and Serverless Framework
By Ram Dileepan, Sr. Solutions Architect at AWS
By Kyle Tryon, Partner Integration Engineer at CircleCI
CircleCI has released an aws-serverless-framework orb to make it easier to continuously integrate and deploy serverless applications built using the Serverless Framework.
An orb is a sharable package of continuous integration and continuous deployment (CI/CD) configurations that is easy to use and makes the software delivery faster and less error prone.
CircleCI is an AWS Partner Network (APN) Advanced Technology Partner with an AWS DevOps Competency. Its continuous integration and delivery platform allows teams to rapidly release code they trust by automating the build, test, and delivery process.
In this post, we show you how to use the Serverless Framework to develop and test a sample serverless application. We also show you how to use CircleCI to implement continuous integration and deployment of that serverless application.
The Serverless Framework
A serverless application is a combination of AWS Lambda functions, event sources, and other resources that work together to perform tasks. A serverless application is more than just a Lambda function. It can include additional resources such as APIs, databases, and event source mappings.
Serverless Framework is an open-source command line interface (CLI) that makes it easier to develop, test, and deploy serverless applications.
Serverless Framework lets you define how to handle a serverless application by writing a config file in YAML, a human-readable data serialization language. It also provides a hosted dashboard, which further simplifies these processes and enables you to monitor your serverless applications.
How to Develop and Test a Serverless Application
This procedure creates a sample serverless application using the Serverless Framework CLI, and deploys it into an Amazon Web Services (AWS) account using the CircleCI CI/CD pipeline.
In this example, we use GitHub as the repository for your source code. The CI/CD pipeline fetches the source code from the repository, builds it, and deploys it into your AWS account.
These are the major steps in this procedure:
- Prerequisites
- Step 1: Install the Serverless Framework CLI.
- Step 2: Create a serverless application using the Serverless Framework CLI.
- Step 3: Add an event.
- Step 4: Test the AWS Lambda function locally.
- Step 5: Create a CircleCI configuration file.
- Step 6: Configure AWS in CircleCI.
- Step 7: Kick off the CI/CD for the first time.
- Step 8: Test the deployed application.
Prerequisites
To start, register for a CircleCI account if you don’t already have one. Also register for an AWS account.
This post assumes you have:
- Required permissions to configure pipelines and create AWS resources in your account.
- Created a repo in one of the Git repositories, and linked that repo to CircleCI.
Step 1: Install the Serverless Framework CLI
The steps to install the Serverless Framework CLI depends on the operating systems you are using. These instructions apply to macOS, Linux, and Windows operating systems.
On macOS or Linux
On macOS or Linux, you can install the CLI as a standalone binary, or as an npm
package. To install as a standalone binary for the first time, use this command:
curl -o- -L https://slss.io/install | bash
To install the CLI as an npm
package, first make sure Node.js
is installed on your machine. If you don’t have Node installed on your machine, first follow the instructions on the Node.js site to install it.
After you install Node, use the following command to install the serverless package:
npm install -g serverless
You can upgrade the CLI using this command:
npm update -g serverless
On Windows
You can use the Chocolatey package manager to install the Serverless Framework CLI on a Windows machine as follows:
choco install serverless
To upgrade, use this command:
choco upgrade serverless
Step 2: Create a Serverless Application Using the Serverless Framework CLI
To create a sample serverless application, open a terminal and enter:
serverless create -t "aws-python3" -n "sls-example" -p "sls-example"
This creates a sample application that includes two files:
handler.py
serverless.yml
handler.py
This file contains a basic AWS Lambda function:
serverless.yml
This is the configuration file for your Serverless Framework. The code snippet below shows only the lines that are not commented out:
Step 3: Add an Event
Now, we can add an event to trigger the Lambda function, as shown below:
Step 4: Test the AWS Lambda Function Locally
The Serverless Framework allows you to test the Lambda function locally. You can test your function by typing the following command:
sls invoke local --function hello
The output is:
Please note that sls local invoke
uses the default profile in your AWS credential configuration file. If you don’t already have a credentials file configured, follow these instructions to set one up.
Step 5: Create a CircleCI Configuration File
Once you test the function locally, you can create a CI/CD pipeline in CircleCI. The pipeline builds and deploys this sample application into your AWS account. To create a CircleCI CI/CD pipeline, create a configuration file that defines the steps and their order.
First, create a folder named .circleci
in the root project folder. Inside this folder, create a file named config.yml
. Copy the text below and paste it into the config.yml
file.
Let’s examine the config file more closely.
- The
orbs
section of the file loads the AWS CLI orb and the Serverless Framework CLI orb from the orbs registry. These orbs include all the required packages to set up the CLIs for AWS and Serverless Framework. - We chose the
serverless/default
executor, which has all the packages required for both CLIs. - In the
jobs
section, we installed and configured the AWS CLI, and set up the Serverless Framework CLI.
Step 6: Configure AWS in CircleCI
Before you can deploy the application to your AWS account, you must provide the environment variables that CircleCI uses to access your AWS account.
Go to the environment variables section in your project settings and set up AWS_ACCESS_KEY_ID
, AWS_SECRET_ACCESS_KEY
, and AWS_DEFAULT_REGION
.
Step 7: Kick Off the CI/CD Pipeline for the First Time
You can now kick off the first CI/CD pipeline. Push the project to your Git repo, and include the CircleCI configuration you just created.
If your application deploys successfully, go to Step 8 to test your application. If your build fails, address the cause of failure before going to the next step. Some of the common reasons for failure are:
- Incompatible versions of software (for example, the Python runtime version may be different from the Python on the build machine). Address this by installing the proper versions of the software.
- You may not be able to access your AWS account from CircleCI. Check the environment variables you configured with AWS credentials under the CI/CD settings in CircleCI.
- You may not have permission to deploy a serverless application. Make sure you obtain all required permissions to deploy a serverless application.
Step 8: Test the Deployed Application
To test the application you just deployed, go to the sls deploy
section of the log and look for the endpoint. It should look similar to this, but replace the asterisks (**********) with the region in which you deployed the application.
endpoints:
GET - https://<api-id>.execute-api.*********.amazonaws.com/dev/hello
Then, enter the following command in a command line:
curl https://<api-id>.execute-api.us-east-1.amazonaws.com/dev/hello
The output is:
{"message": "Go Serverless v1.0! Your function executed successfully!",…
Conclusion
This post showed you how to create a serverless application. We demonstrated how to build and deploy it into your AWS account using the Serverless Framework and a CircleCI CI/CD pipeline.
With CircleCI, you can build and deploy serverless applications with minimal configuration to take advantage of continuous integration for serverless application development.
For more information, see CircleCI and Serverless.
CircleCI – APN Partner Spotlight
CircleCI is an AWS DevOps Competency Partner. Its continuous integration and delivery platform allows teams to rapidly release code they trust by automating the build, test, and delivery process.
Contact CircleCI | Solution Overview | AWS Marketplace
*Already worked with CircleCI? Rate this Partner
*To review an APN Partner, you must be an AWS customer that has worked with them directly on a project.