MySQL
AWS
python3
lambda

MySQL にアクセスする Lambda 関数 (Python3)

python3 で MySQL にアクセスする AWS の Lambda 関数を書いてみました。
ライブラリーは、pymysql を使います。

mysql_scan.py
# -*- coding: utf-8 -*-
#
#   mysql_scan.py
#
#                   Jan/27/2018
# --------------------------------------------------------------------
import  sys
import  json
#
from mysql_scan_exec import mysql_scan_exec_proc
# --------------------------------------------------------------------
def mysql_scan_handler(event, context):
    sys.stderr.write("*** mysql_scan_handler *** start ***\n")
#
    table_name = "No table_name"
#
    sys.stderr.write("Received event: " + json.dumps(event, indent=2) + "\n")
    if ('table_name' in event):
        sys.stderr.write("table_name = " + event['table_name'] + "\n")
        table_name = event['table_name']
#
    version = "Jan/27/2018 PM 18:46"
    sys.stderr.write("version: " + version + "\n")
#
    sys.stderr.write("*** mysql_scan_handler *** ccc ***\n")
#
    rvalue = mysql_scan_exec_proc(table_name)
#
    sys.stderr.write("*** mysql_scan_handler *** end ***\n")
#
    return rvalue
# --------------------------------------------------------------------
mysql_scan_exec.py
# -*- coding: utf-8 -*-
#
#   mysql_scan_exec.py
#
#                   Jan/28/2018
# --------------------------------------------------------------------
import  os
import  sys
import  json
sys.path.append(os.path.join(os.path.abspath(os.path.dirname(__file__)),'api'))
import pymysql
#
from mysql_config import mysql_config_proc
# --------------------------------------------------------------------
def mysql_scan_exec_proc(table_name):
    rvalue = []
    connection = mysql_config_proc()
    cursor = connection.cursor()
#
    sql_str="select * from " + table_name
    sys.stderr.write("sql_str = " + sql_str + "\n")
    cursor.execute (sql_str)
    rows = cursor.fetchall ()
#
    cursor.close()
    connection.close()
#
    return rows
# --------------------------------------------------------------------
mysql_config.py
# -*- coding: utf-8 -*-
#
#   mysql_config.py
#
#                   Jan/28/2018
# ---------------------------------------------------------------
import os
import sys
sys.path.append(os.path.join(os.path.abspath(os.path.dirname(__file__)),'api'))

import pymysql
# ---------------------------------------------------------------
def mysql_config_proc():
#
    host_aa='test_aa.abcdefghijkl.ap-northeast-1.rds.amazonaws.com'
    data_base = 'city'
    user_aa ='scott'
    password_aa = 'tiger123'
#
    connection = pymysql.connect(host_aa, user=user_aa, \
        passwd=password_aa, db=data_base, \
        cursorclass=pymysql.cursors.DictCursor, connect_timeout=5)
    return connection
#
# ---------------------------------------------------------------

ラムダ関数の作成手順
api というフォルダーに pymysql を保存

mkdir api
pip install PyMySQL -t ./api

ラムダ関数を作成するスクリプト
morning_exec_role というロールが作られているとします。

function_create.sh
#
FUNCTION='mysql_scan'
ZIP_FILE=$FUNCTION".zip"
#
zip -r $ZIP_FILE $FUNCTION".py" mysql_config.py mysql_scan_exec.py api
#
#
aws lambda create-function \
    --function-name $FUNCTION \
    --runtime python3.6 \
    --role arn:aws:iam::123456789012:role/morning_exec_role \
    --handler $FUNCTION.mysql_scan_handler \
    --zip-file fileb://$ZIP_FILE \
    --region ap-northeast-1
#

Lambda 関数を更新するスクリプト
(何かを変更した時に使います。)

function_update.sh
#
FUNCTION='mysql_scan'
ZIP_FILE=$FUNCTION".zip"
#
zip -r $ZIP_FILE $FUNCTION".py" mysql_config.py mysql_scan_exec.py api
#
#
aws lambda update-function-code \
    --function-name $FUNCTION \
    --zip-file fileb://$ZIP_FILE
#