I have replaced the way to connect Database from lambda function.
Previously, the function established a connection to RDS using MySQL module and query via 3306 port.
Currently, I tried to use Data API and I have finished to replace all codes without test.
Now I'm thinking about how to guarantee my codes.
I have a few test codes to connect with local MySQL database running on docker image.
However, Data API cannot connect with local MySQL Database because of no endpoint.
So I want to know is there any service or library to mockup Data API.
This is my docker-compose.yml to run MySQL DB.
services:
db:
build: ./docker/mysql
image: mysql:5.7
restart: always
environment:
MYSQL_DATABASE: $DB
MYSQL_USER: $USER
MYSQL_PASSWORD: $PWD
MYSQL_ROOT_PASSWORD: $ROOT_PWD
ports:
- "3306:3306"
volumes:
- ./docker/mysql/initdb.d:/docker-entrypoint-initdb.d
- ./docker/mysql/conf.d:/etc/mysql/conf.d
- ./docker/log:/var/log/mysql
Previously, the function tried to connect to DB by following code
import * as mysql from 'mysql';
import * as util from 'util';
const connection: mysql.Pool = mysql.createPool({
connectionLimit: 100,
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
port: Number(process.env.DB_PORT),
database: process.env.DB_NAME,
connectTimeout: 60 * 60 * 1000,
acquireTimeout: 60 * 60 * 1000,
timeout: 60 * 60 * 1000
});
export async function query(sql: string, param?: Array<any>): Promise<any> {
const connQueryPromisified = util
.promisify(connection.query)
.bind(connection);
return await connQueryPromisified(sql, param);
}
Currently:
import * as aws from 'aws-sdk';
import {
ExecuteStatementRequest,
ExecuteStatementResponse,
Field,
SqlParametersList,
BatchExecuteStatementRequest,
BatchExecuteStatementResponse,
SqlParameterSets
} from 'aws-sdk/clients/rdsdataservice';
const RDS = new aws.RDSDataService({ region: 'ap-northeast-1' });
const secretArn = process.env.SECRET_ARN;
const arn = process.env.RESOURCE_ARN;
const dbName = process.env.DATABASE_NAME;
export async function query(
sql: string,
param?: SqlParametersList
): Promise<any> {
const params: ExecuteStatementRequest = {
secretArn: secretArn,
resourceArn: arn,
sql: sql,
parameters: param,
database: dbName,
continueAfterTimeout: true,
includeResultMetadata: true
};
return RDS.executeStatement(params).promise();
}
Does anybody know about the Data API mockup service? or is there any ides to write codes with Data API?