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
gefragt vor 3 Jahren4956 Aufrufe
1 Antwort
1
Akzeptierte Antwort

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

profile pictureAWS
EXPERTE
Uri
beantwortet vor 3 Jahren
profile picture
EXPERTE
überprüft vor 5 Monaten

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen