1

I am using AWS Lambda service to read the content from the CSV file uploaded on the S3 bucket and convert it to the JSON format. I got stuckup on the CSV to JSON phase.

    const AWS = require('aws-sdk');
    const csvtojson = require('csvtojson');
    const S3 = new AWS.S3();

    exports.handler = async (event,content,callback) => {
        console.log(' inside trigger ')
        const src_bkt = event.Records[0].s3.bucket.name;
        const src_key = event.Records[0].s3.object.key;
        
        try {
            const params = {Bucket: src_bkt, Key:src_key}
       
       // get csv file and create stream
            const stream = S3.getObject(params).createReadStream();
            // convert csv file (stream) to JSON format data
            const json = await csvtojson().fromStream(stream);
            console.log(json);
      }
      catch (err) {
          console.log(JSON.stringify(err))
        return {
          statusCode: err.statusCode || 400,
          body: err.message || JSON.stringify(err.message)
        }
      }
    };

Error Message: "errorType": "Runtime.ImportModuleError", "errorMessage": "Error: Cannot find module 'csvtojson'",

I tried to convert the node module on the lambda to check any luck.

1
  • did you attach the node modules Commented Jan 4, 2020 at 12:32

1 Answer 1

1

You need to package and deploy the lambda as a zip file. The file should contain the javascript file with the handler function as well as the node_modules directory with all the dependencies.

Zip File structure.

handler.js

node_modules/

......................../csvtojson

how to setup the project

  • initialise a project folder for e.g my-project using npm init, it will create a package.json file for you
  • create the handler function in handler.js
  • install csv2json using npm install csv2json --save, now you will see a node_modules directory created

  • zip the my-project directory, so you have my-project.zip, the zip should directly contain the handler.js and node_modules

Reference:

https://aws.amazon.com/premiumsupport/knowledge-center/lambda-deployment-package-nodejs/

hope this helps.

Sign up to request clarification or add additional context in comments.

3 Comments

I wasn't sure if you know these things already. I felt like you created the lambda from AWS console directly without uploading zip file. Was I right
Yes, Arun. I am not aware of this. As you said, I directly created the Lambda on the AWS console.
Glad to be of help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.