DEV Community

Deploying CI/CD For NodeJS Serverless Applications Workshop: Part II

In the first part, we setup the environment, created a simple Node.JS app, explored the SAM template, and lambda code.

In this part, we will run the project locally.

You will learn how to run it and test it locally using the AWS SAM CLI. This is important because its part of the day to day development workflow. It helps you verify if the application is behaving as expected, debug what’s wrong, and fix any issues before pushing your changes to a central repository.

image

INSTALL DEPENDENCIES

Before we run the application locally, it’s a common practice to install third-party libraries or dependencies that your application might be using. These dependencies are defined in a file that varies depending on the runtime, for example package.json for NodeJS projects or requirements.txt for Python ones.

In the terminal, go into the sam-app/hello-world folder.

cd sam-app/hello-world
Enter fullscreen mode Exit fullscreen mode

And install the dependencies:

npm install
Enter fullscreen mode Exit fullscreen mode

Example:

image

RUN USING SAM CLI

There are 2 ways of running a Serverless app locally: 1) By invoking an individual Lambda function or 2) By running a local HTTP server that simulates API Gateway.

We will focus on number 2, but you can learn about invoking individual functions in the SAM Local Invoke reference .

In the terminal, run the following command from the root directory of the sam-app folder:

cd ~/environment/sam-app
sam local start-api --port 8080
Enter fullscreen mode Exit fullscreen mode

Test your endpoint

Once your local server is running, we can send HTTP requests to test it. Chose one of the following options:

Option A) Using CURL

Without killing the running process, open a new terminal.

image

Test your endpoint by running a CURL command that triggers an HTTP GET request.

curl http://localhost:8080/hello
Enter fullscreen mode Exit fullscreen mode

Option B) Using a browser window

In Cloud9, go to the top menu and chose Tools > Preview > Preview Running Application. A browser tab will open, append /hello to the end of the URL.

This will invoke your Lambda function locally.

image

Note how SAM is pulling the Docker container image lambci/lambda:nodejs12.x automatically. This is how SAM is able to simulate the Lambda runtime locally and run your function within it. The first invocation might take a few seconds due to the docker pull command, but subsequent invocations should be much faster.

MAKE A CODE CHANGE

While the app is still running, open the file sam-app/hello-world/app.js and make a simple code change. For example, change the response message to return hello my friend instead of hello world. Your Lambda handler should look like this after the change:

image

Note: Make sure you save the file after changing it.

You don’t have to restart the sam local process, just refresh the browser tab or re-trigger the CURL command to see the changes reflected in your endpoint.

image

RUN THE UNIT TESTS

As you typically would, with any software project, running the unit tests locally is no different for Serverless applications. Developers run them before pushing changes to a code repository. So, go ahead and run the unit tests for your project.

In the terminal, run this command from the sam-app/hello-world folder to run the unit tests:

cd ~/environment/sam-app/hello-world
npm run test
Enter fullscreen mode Exit fullscreen mode

The tests should fail. This is expected!

image

Fix the unit test

Makes sense right? We changed the response message to hello my friend and the unit test was expecting hello world. This is an easy fix, let’s update the unit test.

Open the file sam-app/hello-world/tests/unit/test-handler.js and update the expected value for the response to match the new message. The unit test should look like this after the update:

'use strict';

const app = require('../../app.js');
const chai = require('chai');
const expect = chai.expect;
var event, context;

describe('Tests index', function () {
    it('verifies successful response', async () => {
        const result = await app.lambdaHandler(event, context)

        expect(result).to.be.an('object');
        expect(result.statusCode).to.equal(200);
        expect(result.body).to.be.an('string');

        let response = JSON.parse(result.body);

        expect(response).to.be.an('object');
        expect(response.message).to.be.equal("hello my friend"); // <- FIX
    });
});

Enter fullscreen mode Exit fullscreen mode

Run the tests again

Run the same command again.

npm run test
Enter fullscreen mode Exit fullscreen mode

Now the tests should pass:

image

Note:
  • This project uses the Chai Framework for running the unit tests, but you can chose any other framework. SAM doesn’t enforce any particular one.

Thanks for reaching the second part end. In the upcoming part, we will deploy the app manually and build the pipeline.

Discussion (0)