Demo hướng dẫn insert vào DynamoDB AWS bằng Node JS runtime 2021

Demo hướng dẫn insert vào DynamoDB AWS bằng Node JS runtime 2021
Written by Tommy Tran
Posted on Fri, 01/29/2021 - 11:17
423 views

Ngày 29.1.2021, mình có demo nhỏ cho các bạn về cách insert vào DynamoDB AWS bằng Node JS runtime thông qua AWS Lambda 

Ví dụ 1: Khóa chính của DynamoDB tự xin của hệ thống

// Loads in the AWS SDK

const AWS = require('aws-sdk');

// Creates the document client specifing the region 

// The tutorial's table is 'in us-east-1'

const ddb = new AWS.DynamoDB.DocumentClient({region: 'us-east-1'});

exports.handler = async (event, context, callback) => {

    // Captures the requestId from the context message

    const requestId = context.awsRequestId;

    // Handle promise fulfilled/rejected states

    await createMessage(requestId).then(() => {

        callback(null, {

            statusCode: 201,

            body: '',

            headers: {

                'Access-Control-Allow-Origin' : '*'

            }

        });

    }).catch((err) => {

        console.error(err)

    })

};

// Function createMessage

// Writes message to DynamoDb table Message 

function createMessage(requestId) {

    const params = {

        TableName: 'Student',

        Item: {

            'studentId' : requestId,

            'message' : 'Hello ABC',

            'national' : 'Belarus',

            'Aeg' : '25'

        }

    }

    return ddb.put(params).promise();

}

Ví dụ 2: Khóa chính mình tự insert

// Loads in the AWS SDK

const AWS = require('aws-sdk');


// Table is 'in us-east-1'

const ddb = new AWS.DynamoDB.DocumentClient({region: 'us-east-1'});


exports.handler = async (event, context, callback) => {

    // Handle promise fulfilled/rejected states

    await createMessage().then(() => {

        callback(null, {

            statusCode: 201,

            body: '',

            headers: {

                'Access-Control-Allow-Origin' : '*'

            }

        });

    }).catch((err) => {

        console.error(err)

    })

};


// Function createMessage

// Writes message to DynamoDb table Message 

function createMessage() {

    const params = {

        TableName: 'Class',

        Item: {

            'classID' : 100,

            'Description' : 'Finance Class',

            'Size' : 100            

        }

    }

    console.log("Adding a new item...");

    return ddb.put(params).promise();

}

Kết quả

Lambda Function

Add new comment

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.
3 + 0 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.