How to Deploy Lambda with Node.js without Dependencies

2021/05/033 min read
bookmark this
Responsive image

Table of Contents

  1. Introduction
  2. Create Index.js
  3. Compress Files to Prepare for Deployment to AWS Lambda
  4. Prepare Node.js Code to Deploy to AWS Lambda
  5. Create Policy
  6. Create New Role
  7. Deploy Package to Lambda
  8. Update Function
  9. Test Lambda
  10. Conclusion

Introduction

AWS Lambda is a serverless compute service that lets you run code on AWS-managed servers in the cloud. You only need to worry about your code, and AWS will manage the underlying servers. This blog shows how to get started with Lambda using Node.js with no package dependencies.

The following code was created on macOS, and the IDE is Visual Studio Code.

Create Index.js

First, let's create a sample code file that will later run on AWS Lambda in the cloud. Run the following command to open a simple command line editor.

nano index.js

Once the editor opens, paste the following code into the command line editor and save it.

exports.handler = async (event, context, callback) => {

console.log(JSON.stringify(event));
console.log('hello!');

};

Compress Files to Prepare for Deployment to AWS Lambda

Run the following command in the same directory as the index.js file. This command will create a zip file called index.zip that we'll deploy to AWS later.

zip index.zip index.js

Prepare Node.js Code to Deploy to AWS Lambda

Now, we'll start deploying code to AWS. However, the following prerequisites are not covered in this blog:

  • Setting up an AWS account

  • Creating a user and role to deploy code to Lambda

  • Configuring the AWS CLI on macOS

Assuming you have already completed the above steps, we'll set up a role for AWS Lambda.

Create Policy

Go to IAM, click Policies, then click Create Policy. In the policy editor window, paste the following code.

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "logs:PutLogEvents",
                "logs:CreateLogGroup",
                "logs:CreateLogStream"
            ],
            "Resource": "arn:aws:logs:*:*:*"
        }
    ]
}

Create New Role

Click Roles and create a new role. Enter the name as aws-lambda-policy. Click Attach Policy to add the policy we created earlier.

Deploy Package to Lambda - create-function

Now, we should be ready to deploy the file package to Lambda. Run the following command in your terminal, and it should deploy the index.zip to AWS Lambda and create a new function called my-function.

You'll need to replace Your-AWS-Account-ID with your AWS account ID, and replace AWS-Role with the role we created earlier.

aws lambda create-function --function-name my-function \
--zip-file fileb://index.zip --handler index.handler --runtime nodejs12.x \
--timeout 10 --memory-size 1024 \
--role arn:aws:iam::{Your-AWS-Account-ID}:role/{AWS-Role}

For all the other options for create-function, you can reference the AWS CLI command reference.

Update Function

In case you need to update the index.js file again, you can follow the steps below to redeploy code to AWS Lambda.

Compress the file and recreate the index.zip file.

zip index.zip index.js

Redeploy the compressed file to AWS.

aws lambda update-function-code --function-name my-function --zip-file fileb://index.zip

Test Lambda

Now, you should be able to see the newly created function at AWS Lambda called my-function. You can test the Lambda by clicking the Test button and should be able to see the response.

Conclusion

This blog is a quick-start example for deploying Node.js code to AWS Lambda without any package dependencies.