Deploying Lambda Functions

AWS Lambda is a ‘serverless’ compute service that runs your code in response to an event. The principal benefit of using such a service is that it automatically manages the compute resources required to complete the function, allowing greater scalability and performance. However, using Lambda functions also reduces costs, as you are only charged when your functions are run, and will take care of the security around your code so you don’t have to.

The following blog assumes the following:

  • You have basic knowledge of Node (other languages can be used)
  • You have an AWS account
  • You have AWS CLI set up on your machine

Setting up Serverless

We will start by installing the Serverless framework on our machine using NPM, by running `npm i –g serverless` in terminal. This will install the Serverless framework globally and allow us to use Serverless to manage serverless services.

Next it is time to create a service boilerplate that can be used to develop and then deploy to AWS. By using the `create` command we can create a node service. This create command accepts three options: template, path and name.

The template we will use is `aws-nodejs`, and our path and name will both be `lambda-hello-world`.

Creating the serverless function

Once the boilerplate is created, we can change directory into it and list the contents of the directory. You will see two files. `serverless.yml` is the main configuration file for our service and `handler.js` is where the boilerplate function is defined.

If you open `serverless.yml`, you will see the basic configuration for our service which we defined when we used `serverless create`. For the sake of this blog, we don’t need to make any changes here, so let’s move on to the `handler.js` file. This is where we will modify the existing function to return `Hello World`.

You will see the boilerplate function `hello` which takes three arguments: `event`, `context`, and `callback`. The response is constructed within the function that is returned when the `callback` is invoked. Now we will go ahead and change the message to say `Hello World` for the sake of minimal customization.

Your code will look like this:

"use strict";

module.exports.hello = (event, context, callback) => {
  const response = {
    statusCode: 200,
    body: JSON.stringify({
      message: "Hello World",
      input: event
    })
  };

  callback(null, response);
};

Testing the function locally

We will now run the serverless function locally in order to test it. We can do this by running the `invoke local` command using Serverless. The `invoke local` command can take three options: `function`, `path` and `data`.

  • `function` is the name of the function that you would like to run
  • `path` is the path to the file holding input data if applicable
  • and `data` is the input data for the function.

In our case we will run the `hello` function locally by running the following command in terminal: `serverless invoke local --function hello`. You will see the the response of the function with a status code of `200`, a body which contains the message `Hello World` and an input which is empty.

Deploying the function to Lambda

Now that we know that our function is returning what we expected, we can deploy the function to Lambda. To do this, all you have to do is run the `serverless deploy` command in terminal.

Testing the function on Lambda

Once uploaded you will now be able to test the function on Lambda to make sure it is running as you expect it to. You can do this by running the `serverless invoke` command but this time without the `local` attached to it.

`invoke` can accept seven different options but we are only interested in 2 for now: the `function` and `log` options. You can run `serverless invoke --function hello --log` which will in turn run the `hello` function and display the log.

And there you have it; your first serverless function is now on Lambda.

Leave a Reply

Your email address will not be published. Required fields are marked *