要在 AWS Lambda 中连接多个数据库实例,可以通过使用多个数据库客户端对象来实现。以下是一个使用 Python 语言的示例代码:
import psycopg2
import mysql.connector
def lambda_handler(event, context):
# Connect to PostgreSQL database
postgres_connection = psycopg2.connect(
host='postgres.host.com',
dbname='my_postgres_database',
user='postgres_user',
password='my_postgres_password'
)
# Connect to MySQL database
mysql_connection = mysql.connector.connect(
host='mysql.host.com',
database='my_mysql_database',
user='mysql_user',
password='my_mysql_password'
)
# Run database queries
# ...
# Close database connections
postgres_connection.close()
mysql_connection.close()
return {
'statusCode': 200,
'body': 'Database connections closed.'
}
上述代码中,首先分别连接了一个 PostgreSQL 数据库和一个 MySQL 数据库。然后可以执行每个数据库实例的查询。最后,需要关闭每个数据库连接。
请注意,这只是一个示例代码,实际实现可能因数据库或编程语言不同而异。