Hello World AWS Lambda function

This simple guide shows a step-by-step process for deploying a simple Node.js microservice to AWS Lambda using Claudia.js.


If you’re completely new to using AWS from Node.js, check out Installing and configuring Claudia.js for information on how to set up access credentials.

Deploying your first AWS Lambda function

Create a new NPM project, and just give it a descriptive name (eg claudia-test):

mkdir claudia-test
cd claudia-test

npm init

Install Claudia.js as a global NPM utility:

npm install claudia -g

Now, create a simple JavaScript Lambda function – for example, in a file called lambda.js. For detailed information on the Lambda API, check out the Node.js Lambda Programming Model on AWS.

exports.handler = function (event, context) {
	context.succeed('hello world');

Send this function to AWS using Claudia. You will need to specify the main method for the Lambda to execute (in the Lambda terminology, that’s the handler). The syntax is module.method. Because the main micro-service module is lambda.js, and the method is handler, this argument should be lambda.handler. (Note that you need to use the module name, not the file name).

claudia create --region us-east-1 --handler lambda.handler

When the deployment completes, Claudia will save a new file claudia.json in your project directory, with the function details, so you can invoke and update it easily.

You can now invoke the Lambda function directly from the console:

claudia test-lambda

You should the following response:

  "StatusCode": 200,
  "Payload": "\"hello world\""

This means that the function was deployed to AWS, and is now ready to process events. For something more serious, you can connect this Lambda to various event sources, such as S3 file systems, SNS queues, CloudWatch log events, DynamoDB streams and so on.

For some nice examples of processing various event types, see the Claudia Example Projects

Updating an existing Lambda function

Let’s make something a bit more dynamic. We’ll send it a name, and expect a greeting in return. We’ll also log the request using CloudWatch. Modify the lambda.js file:

exports.handler = function (event, context) {
	context.succeed('hello ' + event.name);

Send the new version up to AWS:

claudia update

Now create a test event with the request data, for example in a file called event.json:

  "name": "Tom"

Now invoke the Lambda function with the test event:

claudia test-lambda --event event.json

The response should come out with the name from the event:

  "StatusCode": 200,
  "Payload": "\"hello Tom\""

You can now check out your logs using AWS Web Console or the AWS command-line tools:

aws logs filter-log-events --log-group-name /aws/lambda/claudia-test

Logging events is a good way to discover the right structure when you connect it to a new event source.

Where next

Claudia has lots of options to customise deployments. Check out Including/Excluding files from deployment for information on how to control which files get sent to AWS, and the Command Line Reference for information on the various options you can use when deploying your function.

For other tasks, see the list of Tutorials and Guides

Did you like this tutorial? Get notified when we publish the next one.

Once a month, high value mailing list, no ads or spam. (Check out the past issues)