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
demandé il y a 3 ans5,3 k vues
1 réponse
2
Réponse acceptée

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

profile pictureAWS
EXPERT
répondu il y a 3 ans
profile picture
EXPERT
vérifié il y a 6 mois
profile picture
EXPERT
vérifié il y a un an

Vous n'êtes pas connecté. Se connecter pour publier une réponse.

Une bonne réponse répond clairement à la question, contient des commentaires constructifs et encourage le développement professionnel de la personne qui pose la question.

Instructions pour répondre aux questions