Home > front end >  Is it possible to save a scan data from DynamoDb to a variable in Lambda function?
Is it possible to save a scan data from DynamoDb to a variable in Lambda function?

Time:10-27

I am using AWS Lambda function and I am performing read operation from my DynamoDB. The question is, is it possible to save the data that i retrieved from DynamoDB. Since i want to send one of the values of the field in a write or update operation later.

'use strict';

const aws = require('aws-sdk');
const ddb = new aws.DynamoDB.DocumentClient({region: 'us-east-2'});

const docClient = new aws.DynamoDB.DocumentClient();

exports.handler = (event, context, callback) => {
 
    const params = {
            TableName: 'parking_lots',
            Limit: 10
    }
    return ddb.scan(params, function(err, data){
        if(err){
            console.log(err);
        }else{
            console.log(data);
        }
    });
    
 
};

Something like for example:

const data = ddb.scan(params, function(err, data){
        if(err){
            console.log(err);
        }else{
            console.log(data);
        }
    });

So i can access a data something like this

data.lot_number;

CodePudding user response:

I believe you meant "store" the data in a variable.

It will be easier for you to understand if you use async/await.

'use strict';

const aws = require('aws-sdk');
const ddb = new aws.DynamoDB.DocumentClient({region: 'us-east-2'});

const docClient = new aws.DynamoDB.DocumentClient();

exports.handler = async (event, context) => {
 
    const params = {
            TableName: 'parking_lots',
            Limit: 10
    }

    const data = await ddb.scan(params).promise()
    
    // You now have access to data.
    console.log(data)
    
    return Promise.resolve()
};

CodePudding user response:

Your SDK command for dynamo returns a json object. You can do anything with that object, including storing it in memory for later use in the same invocation

That data will not still be available the next time the lambda is called. They do not keep state between invocations (technically their is a possibility but you should never rely on it because it exists some times and other times it doesn't and it can really mess up other invocstions)

  • Related