Deploying lambda serverless functions to AWS Cloud + Github Actions. πŸš€

Deploying lambda serverless functions to AWS Cloud + Github Actions. πŸš€

Setup AWS + Lambda + CI-CD Pipleine in just 5 steps.

Table of contents

No heading

No headings in the article.

In the journey of learning Devops , I have tried using Cloud Services and Serverless functions like Lambda πŸ”₯and AWS.

In this blog , I will walkthrough the steps and process of writing a simple Lambda function and deploying it on AWS with a complete Ci - Cd Pipeline using Github Actions.

7602.1513404277.png

  1. Create a empty folder and open it in your code Editor.

1.png

We need to install serverless CLI first.

npm i -g serverless

We need to create a boilerplate for our node js - aws application :

serverless create --template aws-nodejs

We will be served with a boilerplate code like this where we can customize it according to our needs.

2.png

Let's edit the handler.js function here , for this example I will be writing a simple Javascript function to generate any Random Integer.

3.png

Let's edit the serverless.yaml file here, which will deploy our code to AWS Lambda and Cloud !

serverless.yaml.png

The service name can be anything you desire here. The main thing here is the functions section. hello is the main function name which will get deployed.

handlergives the path to the main javascript handler function which we wrote previously.

timeout is set to 60 secs so that it doesn't runs in loop on Cloud.

memory is restricted to 128 mb because we don't want AWS to use a lot of memory each time our lambda gets deployed.

2 . Configure AWS console and IAM User.

iam.png

In AWS , if we want to deploy stuff then AWS needs to know who's the user and what services he is allowed to use. Hence when we create a IAM user we add some policies/permissions to the group so that we can use AWS services without any issue.

I won't be able to explain the whole walkthrough of creating a IAM User so you can refer this video. Just remember to add these following policies in your policies section.

IAM policies.png

You will get a AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY from the IAM console. kindly save it in your notepad.

3 . Let's configure Github and Github Actions now for CI -CD Pipeline.

github.png

  • Go to the settings page under Settings > Actions > Secret. and click on New Repository Secret to create your secrets.

secret.png

github-secrets.png

Add your AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY with their values that you will get on the IAM User console.

Now let's create a Yaml file for the Ci-Cd pipeline that we will use with Github Actions.

Back to your Code Editor (in my case VS Code).

In your root directory / folder create a .github folder and inside it create a workflows folder.

Now create a main.yaml file in workflows folder.

The folder structure will be as follows :

github-folder.png

Now let's write yaml code for our Ci-cd pipeline in main.yaml

name: Deploy Lambda

on:
  push:
    branches:
      - main

jobs:
  deploy:
    name: deploy
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [12.x]
    steps:
      - uses: actions/checkout@v2
      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v1
        with:
          node-version: ${{ matrix.node-version }}

      - name: serverless deploy
        uses: serverless/github-action@v3
        with:
          args: deploy
        env:
          # SERVERLESS_ACCESS_KEY: ${{ secrets.SERVERLESS_ACCESS_KEY }}
          AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
          AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}

Explanation of what this above code will do :

  • Name field is just the name of the deploy command which will run on Github Actions.

  • on push branches : main. This code will push your customized / edited code to github and the deployment cycle will start spinning from the branch you have specified.

  • In my case I have specified main branch because I have commited my code to my main branch only on my github repository.

  • jobs : This is the main deploy function that will be seen when your code will be deploying to any Cloud Service (for ex AWS).

  • deploy : this will start the deployment cycle by taking into consideration the attributes and values provided below this.

  • runs on: ubuntu-latest : this specifies the OS we want our code to run on Github Actions. For me I have kept it default as ubuntu (latest version).

  • steps : here we are mentioning the steps github needs to go with for running the code and the node - version.

  • env : Here we provide the environment variables which we have already specified in the settings section.

4 . Push your code to Github and watch the magic happen !

Let's commit and push our code to Github. For that , we need to first initialize an empty repository in our folder.

  • Open the terminal in that folder and write the following commands mentioned below:
git init
git remote add origin https://github.com/Github Username/repository-name.git
  • Add your all files to Github
git add .
  • Commit your code now
git commit -m 'initial commit'
  • Switch your branch to main because we want our code to deploy on main as we have specified in main.yaml file
git branch -M main
  • Push your code now, finally.
git push --set-upstream origin main

finally done.jpg

Yes, but you need to see the best thing yet πŸ˜‰. Go to your Github repository and you will see a small yellow dot 🟑 like this .

commit.png

Click on it.

Go to your Actions section and you will see your latest commited push name, for me it's blog-edit . Click on it

github-actions.png

github-deploy.png

You will see a UI like this where your code will be doing the job we specified :

Click on it and this will show you what are the processes Github Actions is going through and it will also show the error logs if there are any.

deploy-face.png

serverless-function.png

You can also check what all steps and backend code which is spinning in each job we specified. If your serverless.yaml file is incorrect or if their's an issue in your AWS configuration of IAM you will see errors in serverless deploy.

5 . Check your Lambda function on AWS.

Go to AWS Console with this link .

Search Lambda in this search Box.

lambda.png

lambda-dasboard.png

Go to the functions section and you will see your deployed function name here. For me it's next-serverless-demo-hello .

Click on it.

Now test your function by clicking here. You will see the result just below it !

test.png

Our random generated Integer is 66. It will be different every time you deploy your code or test it

73f198fb082c9d7df3572702c47278d1.jpg

You have finally done it ! πŸ”₯β€οΈπŸš€

  • Configure AWS and IAM User on Amazon Web Services.βœ…
  • Create IAM User.βœ…
  • Write your Lambda Code (handler.js).βœ…
  • Setup Ci - Cd Github Pipeline.βœ…
  • Write serverless.yaml file for serverless deployment on AWS.βœ…
  • Deploy code to Github > AWS.βœ…
  • Test your code. βœ…

That's all for today's post. Stay tuned for more ! Jyotindra Tavanoji ❀️

Β