0
0

More than 3 years have passed since last update.

Node.js: Lambda で MariaDB のデータを削除 (Delete)

Last updated at Posted at 2020-06-07

フォルダー構造

$ tree -a
.
├── .env
├── function_update.sh
├── index.js
└── test_local.js
maria_delete/index.js
// ---------------------------------------------------------------
//  maria_delete/index.js
//
//                  Jun/07/2020
//
// ---------------------------------------------------------------
var mysql = require('mysql2/promise')

// ---------------------------------------------------------------
async function main(id_in)
{
    console.error("id_in = " + id_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
        })

    const command = "delete from cities where id = '" + id_in + "'"

    console.error (command)
    console.error ("*** before execute ***")
    await conn.execute(command)
    console.error ("*** after execute ***")
    } catch (ee) {
        console.log(ee)
        console.error ("*** error *** main ***")
        console.error(ee)
        return ''
    } finally {
        if (conn && conn.connection) {
        conn.end()
        console.error ("*** 終了 ***")
        }
    }
}

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

exports.handler = async (event) => {
const key_in = event['key']
console.error (key_in)

console.error("***** start maria_delete PM 16:14 ***")
var rvalue = {}
rvalue['message'] = 'Hello from maria_delete'

await main(key_in)

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

    console.error("***** end maria_delete ***")
    return response
}


// ---------------------------------------------------------------
test_local.js
#! /usr/bin/node
// ---------------------------------------------------------------
//  maria_delete/test_local.js
//
//                  Jun/07/2020
//
// ---------------------------------------------------------------
var maria_delete = require('./')
// ---------------------------------------------------------------
console.error ("*** 開始 *** test_local.js ***")

const key_in = process.argv[2]

const event = {
    "key": key_in
}

rvalue = maria_delete.handler(event)
console.log(rvalue)

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

実行スクリプト

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

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

function_update.sh
FUNCTION='maria_delete'
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_delete --region ap-northeast-1 \
    --payload '{"key": "t3322"}' \
 outputfile.txt
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0