Ao usar o AWS re:Post, você concorda com os AWS re:Post Termos de uso

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
feita há 3 anos5,3 mil visualizações
1 Resposta
2
Resposta aceita

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

profile pictureAWS
ESPECIALISTA
respondido há 3 anos
profile picture
ESPECIALISTA
avaliado há 6 meses
profile picture
ESPECIALISTA
avaliado há um ano

Você não está conectado. Fazer login para postar uma resposta.

Uma boa resposta responde claramente à pergunta, dá feedback construtivo e incentiva o crescimento profissional de quem perguntou.

Diretrizes para responder a perguntas