Node.js : Lambda에서 MariaDB 데이터 읽기 (읽기)

폴더 구조
$ tree -a
.
├── .env
├── function_update.sh
├── index.js
└── test_local.js

maria_read/index.js
// ---------------------------------------------------------------
//  maria_read/index.js
//
//                  Jun/08/2020
//
// ---------------------------------------------------------------
'use strict'

// ---------------------------------------------------------------
async function read01 (mysql,host,user,password,data_base)
{
    try {
        console.error ("*** read01 *** start ***")
    var conn = await mysql.createConnection({
        host: host,
        user: user,
        password: password,
        database: data_base
        })
    const sql_str = 'select * from cities'
        console.error (sql_str)
    const [rows, fields] = await conn.execute(sql_str)

    var dict_aa = new Object ()

    rows.forEach(function(row)
        {
        dict_aa[row.id] = {"name": row.name,
            "population": row.population,
            "date_mod": row.date_mod}
        })

    return dict_aa
    } catch (ee) {
        console.log(ee)
        console.error ("*** error *** read01 ***")
        console.error(ee)
        return ''
    } finally {
        if (conn && conn.connection) {
        conn.end()
    }
}

}

// ---------------------------------------------------------------
async function main()
{
    var mysql = require('mysql2/promise')

    const host = `${process.env.host}`
    const user = `${process.env.user}`
    const password = `${process.env.password}`
    const data_base = `${process.env.data_base}`

    console.error("host = " + host)
    console.error("user = " + user)
    console.error("password = " + password)
    console.error("data_base = " + data_base)

    const dict_aa = await read01 (mysql,host,user,password,data_base)

    console.error ("*** main *** end ***")

    return dict_aa
}

// ---------------------------------------------------------------
console.error ("*** 開始 ***")
exports.handler = async (event,context,callback) => {
    console.error("***** start maria_read PM 20:20 ***")
    var rvalue = {}
    rvalue['message'] = 'Hello from maria_read'

    const dict_aa = await main()

    rvalue['data'] = dict_aa

    const response = {
        statusCode: 200,
        body: JSON.stringify(rvalue),
    }

    console.error("***** end maria_read ***")

    callback(null, response) 

//  return response
}

// ---------------------------------------------------------------

test_local.js
// ---------------------------------------------------------------
//  test_local.js
//
//                      Jun/08/2020
//
// ---------------------------------------------------------------
'use strict'
// ---------------------------------------------------------------
var maria_read = require('./')
// ---------------------------------------------------------------
function func01(aaa,rvalue)
{
    console.error ("*** func01 *** start ***")

//    console.log(aaa)
    console.log(rvalue)

    console.error ("*** func01 *** end ***")
}

// ---------------------------------------------------------------
console.error ("*** 開始 *** test_local.js ***")

const event = {}
const context = ""

maria_read.handler(event,context,func01)

console.error ("*** 終了 *** test_local.js ***")
// ---------------------------------------------------------------

실행 스크립트
export NODE_PATH=/usr/lib/node_modules
node -r dotenv/config ./test_local.js

AWS 프로그램을 업데이트하는 스크립트

function_update.sh
FUNCTION='maria_read'
ZIP_FILE=$FUNCTION".zip"
#
rm -f *.zip
#
zip -r $ZIP_FILE index.js
#
#
aws lambda update-function-code \
    --function-name $FUNCTION \
    --zip-file fileb://$ZIP_FILE
#

AWS 콘솔에서 수행한 결과

좋은 웹페이지 즐겨찾기