Node.js: Lambda で MariaDB のデータを読む (Read)

フォルダー構造

```text
$ tree -a
.
├── .env
├── function_update.sh
├── index.js
└── test_local.js
```

```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
}

// ---------------------------------------------------------------
```

```js: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 ***")
// ---------------------------------------------------------------
```

実行スクリプト

```bash
export NODE_PATH=/usr/lib/node_modules
node -r dotenv/config ./test_local.js
```

AWS のプログラムを更新するスクリプト

```bash: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 のコンソールで実行した結果
![maria_read_jun08.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/179446/0afdccf1-53b9-f67b-c30c-1e2707c19b80.png)