Previously I wrote about how to Create a Serverless backend with AWS Lambda Function, Amazon API Gateway and Serverless Framework. 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, GitHub Actions, GitLab CI, CircleCI, Travis CI, Bitbucket Pipelines, AWS CodeBuild, AWS CodeDeploy, AWS CodePipeline and many more.
In this tutorial, I'll be using AWS, Serverless framework and GitHub Actions
GitHub Actions
GitHub 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)
- Have an AWS account
- Create an AWS IAM user 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
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
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
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.
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.
Extra resources
You can see the full project here
Read the docs to know more about GitHub actions
Read more about Serverless framework here
Learn more about AWS here
Top comments (3)
Very good. Thanks.
I've added my aws credentials to secrets in github but still got this error.
"AWS provider credentials not found. Learn how to set up AWS provider credentials in our docs here: slss.io/aws-creds-setup."
you need to specify environment name in steps, like below: