How to Deploy Lambda with Node.js with Package Dependencies

2021/5/64 min read
bookmark this

AWS Lambda is serverless compute service and let you run code at AWS's managing servers on the cloud, you will only worry about your code and AWS will manage the underneath servers. This blog is trying to show how to get start with Lambda by use Node.js with package dependencies, the code will include one dependency module util, it'll will use zip to compress the index.js and node_modules folder.

Following code is created at MacOS, IDE is Visual Studio Code.

Create Index.js

First, let's create a sample code and later will run at AWS lambda at the cloud. Run following code to open a simple command line editor. 

nano index.js

Once it open the editor, paste following code at the command line editor and save it. 

const util = require('util');
exports.handler = async (event, context, callback) => {

    console.log("Reading options from event:\n", util.inspect(event, {depth: 5}));
    console.log(JSON.stringify(event));
    console.log('hello!');
    
};

Npm install to install the dependencies

At the same directory, run following command, the first command npm install -y will create package.json file, after that than run the npm install util to install the dependency which will create node_modules folder.

npm init -y
npm install util

Compress File for prepare deploy to AWS Lambda

Run following command line at the index.js file's directory, this command line will create zip file index.zip later we'll deploy to AWS. 

zip -r index.zip index.js node_modules

Prepare Node.js Code to deploy to AWS Lambda

At here, we'll start to deploy code to AWS, but there are following few things are not include in this blog. 

  • Setup AWS Account
  • Create user and role so later can deploy code to Lambda
  • Configure AWS Cli at MacOS 

Now, assume you already setup above list, we'll try to setup role for AWS Lambda.

Create Role

Create Policy

Go to the IAM, click policies then click create Policy, at the policy editor window paste 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 new role, let's just enter the name as aws-lambda-policy. click Attach Policy to add the policy we create earlier. 

Deploy package to Lambda - create-function

Now, we should be ready and can start deploy file package to Lambda. Run the following command at your command line window then it should be deploy the index.zip to AWS Lambda and create new function called my-function.

You'll need to replace the Your-AWS-Account-ID, to your aws account's ID, and replace the AWS-Role to 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 if you need to update the index.js file again, you can follow the blow steps 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 create function at AWS Lambda my-function. You can test the lambda by click the Test button, and should be able to see the response.

Conslusion

This blog is quick start example to deploy Node.js code to AWS Lambda.