How to create lambda function with nodejs from cloudformation inline?

0

I am new to lambda and node-js and want to create a CF template to create a lambda function inline without passing the lambda code from a S3 bucket zip file.

When I tried below code, it fails with error `Cannot find module 'app' as the CFT didn't deploy it as a node package and app.js and the nodejs directory structure is missing in it. Is there a way to create such lambda function without manually creating the zip file and adding it to the cloudFormation template? I can easily do it in python, but not sure if it's a limitation for lambda with nodejs cloudformation.

My CloudFormation Template:

Resources:
  OnConnectFunction:
    Type: "AWS::Lambda::Function"
    Properties:
      Description: OnConnectFunction
      Handler: app.handler
      MemorySize: 256
      Runtime: nodejs12.x
      Role: !GetAtt 'LambdaIAMRole.Arn'
      Timeout: 60
      Environment:
        Variables:
          TABLE_NAME:
            Ref: TableName
      Code:
        ZipFile: |
          const AWS = require('aws-sdk');
          const ddb = new AWS.DynamoDB.DocumentClient({ apiVersion: '2012-08-10', region: process.env.AWS_REGION });
          exports.handler = async event => {
            const putParams = {
              TableName: process.env.TABLE_NAME,
              Item: {
                connectionId: event.requestContext.connectionId
              }
            };

            try {
              await ddb.put(putParams).promise();
            } catch (err) {
              return { statusCode: 500, body: 'Failed to connect: ' + JSON.stringify(err) };
            }

            return { statusCode: 200, body: 'Connected.' };
          };

Error when the lambda is invoked:

{
    "errorType": "Runtime.ImportModuleError",
    "errorMessage": "Error: Cannot find module 'app'\nRequire stack:\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js",
    "stack": [
        "Runtime.ImportModuleError: Error: Cannot find module 'app'",
        "Require stack:",
        "- /var/runtime/UserFunction.js",
        "- /var/runtime/index.js",
    ]
}
AWS
asked 3 years ago5256 views
1 Answer
2
Accepted Answer

Your Handler attribute should be: index.handler. See here

profile pictureAWS
EXPERT
Uri
answered 3 years ago
profile picture
EXPERT
reviewed 4 months ago
profile picture
EXPERT
reviewed 10 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions