AWS Lambda can be used to securely connect to any Amazon Aurora PostgreSQL-Compatible or Amazon RDS for PostgreSQL database to run stored procedures. This approach provides the flexibility, security and scalability to manage stored procedures in the cloud seamlessly.
In the solution overview, the process is initiated through the AWS Command Line Interface (CLI), invoking the Lambda function, which performs the necessary operations and sends notifications to DBAs through Amazon SNS.
Creating a Secrets Manager secret, storing a PostgreSQL database user credential, and configuring Amazon SNS to send notifications to designated DBAs users upon successful completion of stored procedures are some prerequisites.
Lambda layer for psycopg3 is used to run the Lambda function which connects to the PostgreSQL database, runs a stored procedure, and sends a success message to an SNS topic. The function retrieves the database credentials from Secrets Manager and other connection parameters from environment variables.
The Lambda function is customized by setting environment variables and running the desired stored procedures using the configuration of Lambda function's execution role with the necessary permissions to execute.
Lambda functions have a default timeout of 3 seconds, and you can configure the timeout to up to 15 minutes to accommodate long-running stored procedures. You can break down the stored procedure into smaller, more manageable tasks that can be run sequentially, or implement an asynchronous processing pattern.
Make sure to implement proper logging and error handling in your Lambda function to monitor and troubleshoot any issues that might arise during database connectivity. Handling long-running stored procedures can be performed using various options.
Delete the resources created to avoid incurring future charges.
This approach offers several advantages, including improved security, scalability, reduced infrastructure overhead, seamless integration, and cost optimization.
Senior Database Consultant with AWS, Ramesh Pathuri, and Senior Partner Solutions Architect at AWS, Gautam Bhaghavatula worked on this solution.