Setup CI/CD for your AWS Lambda with Serverless Framework and GitHub Actions
Published: 2022-02-05 11:36:49

---
title: Setup CI/CD for your AWS Lambda with Serverless Framework and GitHub Actions
published: true
description: Setup CI/CD for your AWS Lambda with serverless framework and GitHub Actions by creating workflow
tags: 
cover_image: https://res.cloudinary.com/rexben/image/upload/v1644058526/articles/Screenshot_2022-02-02_at_2.39.53_PM.png
---

Previously I wrote about how to [Create a Serverless backend with AWS Lambda Function, Amazon API Gateway and Serverless Framework](https://dev.to/rexben001/create-a-serverless-backend-with-aws-lambda-function-and-amazon-api-gateway-3d9l). In that tutorial, I showed you how to create, test and deploy your serverless app to AWS Lambda and Amazon API Gateway manually but in this tutorial, I'll be showing you how to deploy it using CI/CD.


#### What's CI/CD?
CI/CD stands for continuous integration, continuous deployment and continuous delivery. It's a process that alienates manual processes of doing things. It is the art of automating the process of building, testing, deployment and delivery of apps to your customers. There are different tools used for CI/CD, they include [Jenkins](https://www.jenkins.io/), [GitHub Actions](https://github.com/features/actions), [GitLab CI](https://docs.gitlab.com/ee/ci/), [CircleCI](https://circleci.com/), [Travis CI](https://www.travis-ci.com/), [Bitbucket Pipelines](https://bitbucket.org/product/features/pipelines), [AWS CodeBuild](https://aws.amazon.com/codebuild/), [AWS CodeDeploy](https://aws.amazon.com/codedeploy/), [AWS CodePipeline](https://aws.amazon.com/codepipeline/) and many more.

In this tutorial, I'll be using AWS, Serverless framework and GitHub Actions

#### GitHub Actions
[GitHub Actions](https://docs.github.com/en/actions) automate, customize, and execute your software development workflows right in your repository with GitHub Actions. You can discover, create, and share actions to perform any job you'd like, including CI/CD, and combine actions in a completely customized workflow.


#### Prerequisites to follow along:
* Have a GitHub account
* Fork and clone this repo (contains code from my [previous tutorial](https://dev.to/rexben001/create-a-serverless-backend-with-aws-lambda-function-and-amazon-api-gateway-3d9l))
* Have an AWS account
* Create an [AWS IAM user](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_users_create.html) that has the following permissions: **IAMFullAccess**, **AmazonS3FullAccess**, **CloudWatchFullAccess**, **AWSCloudFormationFullAccess**, **AWSLambda_FullAccess** and **AmazonAPIGatewayInvokeFullAccess**.
* Store the AWS user API key and secret key (keep it safe)


#### Create a main.yml file to define the workflow configuration
After ticking all the prerequisites, create a file called main.yml in folder `.github/workflows` and paste this code

{% gist https://gist.github.com/Rexben001/6be96f68679d803548c5d4b014a25a39 file=main.yml %}


A workflow is a configurable automated process made up of one or more jobs. 

##### Workflow syntax
**name**: the name of the workflow

**on**: the type of event that can run the workflow. Our workflow will only run when there's a git push to either the master or develop branch. Read more [here](https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows)

**jobs**: a workflow consists of one or more jobs. Jobs run in parallel unless a `needs` keyword is used. Each job runs in a runner environment specified by `runs-on`

**steps**: sequence of tasks to be carried out

**uses**: selects an action to run as part of a step in your job. An action is a reusable unit of code. Read more [here](https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsuses)

**with**: a map of input parameters

**run**: runs command-line programs

**env**: set the environment variables

#### Add API key and secret key to GitHub secret

Go to **settings** on the forked repo to add your API Key and Secret key. Click on **Secrets** on the left side nav and click on **New repository secret** to add your secrets. The API Key and Secret Key gives us programmatic access to your AWS environment.

![GitHub Secrets](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wtitf408r436n10xa5ym.png)

#### Push changes to GitHub to start the workflow
You can now commit your changes locally and push it to GitHub. Navigate the repo on GitHub, click on the actions, you should be able to see your workflows.

![GitHub workflows](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5jv8nheu07diumw667q0.png)

##### Extra resources
You can see the full project [here](https://github.com/Rexben001/aws-lambda-serverless-starter/tree/develop)
Read the [docs](https://docs.github.com/en/actions/learn-github-actions/understanding-github-actions) to know more about GitHub actions
Read more about Serverless framework [here](https://www.serverless.com/framework/docs/providers/aws/guide/intro)
Learn more about AWS [here](https://aws.amazon.com/training/)