フォルダー構造
$ tree -a
.
├── .env
├── function_update.sh
├── index.js
└── test_local.js
maria_update/index.js
#! /usr/bin/node
// ---------------------------------------------------------------
// maria_update/index.js
//
// Jun/07/2020
//
// ---------------------------------------------------------------
var mysql = require('mysql2/promise')
// ---------------------------------------------------------------
function get_current_date_proc ()
{
const today = new Date ()
var ddx = (1900 + today.getYear ()) + "-" + (today.getMonth () +1)
ddx += "-" + today.getDate ()
return ddx
}
// ---------------------------------------------------------------
function update_command_gen (id_in,population_in)
{
const today = get_current_date_proc()
var command = "update cities set population = " + population_in
command += " , date_mod = '" + today + "'"
command += " where id = '" + id_in + "'"
console.log (command)
return command
}
// ---------------------------------------------------------------
async function main(key_in,population_in)
{
console.error("key_in = " + key_in)
console.error("population_in = " + population_in)
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)
try{
var conn = await mysql.createConnection ({
host: host,
user: user,
password: password,
database: data_base
})
console.error("***** after mysql.createConnection ***")
const command = update_command_gen (key_in,population_in)
console.error(command)
console.error("***** before execute ***")
await conn.execute(command)
console.error("***** after execute ***")
} catch (ee) {
console.log(ee)
console.error ("*** error *** read01 ***")
console.error(ee)
return ''
} finally {
if (conn && conn.connection) {
conn.end()
console.error ("*** 終了 ***")
}
}
}
// ---------------------------------------------------------------
console.error ("*** 開始 ***")
exports.handler = async (event) => {
const key_in = event['key']
const population_in = event['population']
console.error (key_in + "\t" + population_in)
console.error("***** start maria_update PM 16:30 ***")
var rvalue = {}
rvalue['message'] = 'Hello from maria_update'
await main(key_in,population_in)
const response = {
statusCode: 200,
body: JSON.stringify(rvalue),
}
console.error("***** end maria_update ***")
return response
}
// ---------------------------------------------------------------
test_local.js
#! /usr/bin/node
// ---------------------------------------------------------------
// maria_update/test_local.js
//
// Jun/07/2020
//
// ---------------------------------------------------------------
var maria_update = require('./')
// ---------------------------------------------------------------
console.error ("*** 開始 *** test_local.js ***")
const key_in = process.argv[2]
const population_in = parseInt(process.argv[3],10)
const event = {
"key": key_in,
"population": population_in
}
rvalue = maria_update.handler(event)
console.log(rvalue)
console.error ("*** 終了 *** test_local.js ***")
// ---------------------------------------------------------------
実行スクリプト
export NODE_PATH=/usr/lib/node_modules
node -r dotenv/config ./test_local.js t3328 21498700
AWS のプログラムを更新するスクリプト
function_update.sh
FUNCTION='maria_update'
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 のプログラムを cli で実行する方法
cli_exec.sh
#
aws lambda invoke --invocation-type Event \
--function-name maria_update --region ap-northeast-1 \
--payload '{"key": "t3326", "population": 4312500}' \
outputfile.txt