Deploying Serverless AppsDeploying Serverless Apps

A step by step tutorial for deploying a Serverless service with local bash and GitHub Action pipeline.

Serverless apps can be deployed in several ways, in this article, we gonna try deploying in the 2 easiest ways possible.

  1. Serverless App Setup

    Serverless Portal Signup

    • Create a new app in the Serverless dashboard:
    Create new app Serverless Dashboard Create App

    Deploy Existing Project

    • Install Serverless cli using the below command ( If you did not have node or npm installed check out this tutorial ).
    npm install -g serverless
    
    # check with 
    serverless -v
    
    # output:
    # Framework Core: 3.28.1
    # Plugin: 6.2.3
    # SDK: 4.3.2
    • Select existing project and run the below command in the root of your project (checkout sample project here, I am using aws-node-typescript sample.):
    npm install 
    
    serverless --org=<username>
    • Login with the Serverless Dashboard account and authorize the serverless cli as below:

    Serverless CLI Authorize Dashboard

    • Continue to the next step on cli and Select AWS Access Role and Authorize your AWS account in your Serverless Dashboard.

    Select Authorize AWS

    Select Cloud Provider AWS AWS Authorize
    AWS Authorizing In Progress AWS Authorizing Success
    • Finally, you will see success on cli as below:

    AWS Authorize CLI Success

  2. Local Deployment

    After all setup, you can finally run the below command to deploy

    # to deploy default set env
    serverless deploy
    
    # to deploy <stage> env
    serverless deploy --stage <stage>
    
    # to deploy production env
    serverless deploy --stage production

    CLI Deploy Complete

    You can also remove and free all cloud resources from local by using the below command:

    serverless remove

    CLI Remove Complete

  3. GitHub Action Deployment

    serverless/github-action Action wraps the Serverless Framework to enable common Serverless commands.

    • To deploy via serverless/github-action you need either the SERVERLESS_ACCESS_KEY from step 1, or you can directly deploy to AWS using AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY.

    Getting SERVERLESS_ACCESS_KEY

    Getting AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY

    • Or you can directly deploy to AWS from GitHub Action using AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY, to get these secrets go to AWS Console and create a user. The path is Security credentials (in upper-right corner menu) -> Users.

      AWS Add New User

    • Then open created user, in my case serverless user and create new Access keys from Security Credentials tab.

      aws-create-secret.png aws-create-secret-level.png
    • Finally, add the applicable secret as environment variables from the GitHub secrets from GitHub Project -> Settings -> Secrets and variables -> Actions and add a New repository secret.
    • Create a .github/workflows/deploy-live.yml file same as the below details:

                          
                              name: Serverless AWS Deploy
      
      on:
        push:
          branches:
            - main
      
      jobs:
        deploy:
          name: deploy
          runs-on: ubuntu-latest
          strategy:
            matrix:
              node-version: [16.x]
          steps:
          - uses: actions/checkout@v3
          - name: Use Node.js ${{ matrix.node-version }}
            uses: actions/setup-node@v3
            with:
              node-version: ${{ matrix.node-version }}
          - run: npm ci
          - name: serverless deploy --stage live
            uses: serverless/github-action@v3.1
            with:
              args: deploy
            env:
              SERVERLESS_ACCESS_KEY: ${{ secrets.SERVERLESS_ACCESS_KEY }}
              PUPPETEER_SKIP_CHROMIUM_DOWNLOAD: true
              # or if using AWS credentials directly
              # AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
              # AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
                          
                      

    • The above workflow trigger when the code is merged into the main branch. You can change the trigger to run the action on pull request creation as below:

      on:
        pull_request:
          branches:
            - main
    • In the end, raise a PR on the main branch or push changes to the main branch and see your action deploying your app to AWS.

      github-action-run.png

  4. Debugging

    • Error: unable to get local issuer certificate error on serverless --org=<username>. Disable Node.js SSL check temporarily by using the below command.
    export NODE_TLS_REJECT_UNAUTHORIZED=0

References: