Amazon Relational Database Service (Amazon RDS) for SQL Server is a completely managed database service that makes it easy to arrange, function, and scale SQL Server deployments within the cloud. It handles routine database duties reminiscent of provisioning, patching, backup, restoration, and failure detection. AWS Lambda is a serverless compute service that runs your code in response to occasions and mechanically manages the underlying compute assets, permitting you to construct responsive purposes with out provisioning or managing servers.
Organizations utilizing Amazon RDS for SQL Server more and more have to combine their database operations with serverless workflows to construct fashionable, event-driven architectures. The power to invoke Lambda capabilities in response to database occasions allows highly effective use instances reminiscent of triggering automated workflows, sending real-time notifications, calling exterior APIs, and orchestrating complicated enterprise processes, all whereas sustaining the separation of issues between information persistence and utility logic. On this submit, we show learn how to allow this integration by utilizing Amazon CloudWatch subscription filters, Amazon Easy Queue Service (Amazon SQS), and Amazon Easy Notification Service (Amazon SNS) to invoke Lambda capabilities from RDS for SQL Server saved procedures, serving to you construct responsive, data-driven purposes.
Answer overview
The next diagram illustrates the answer structure.
The workflow consists of the next steps:
- Amazon RDS for SQL Server publishes occasion error logs to CloudWatch Logs.
- A customized message within the error logs triggers a CloudWatch metric filter.
- The modified worth within the metric filter triggers a CloudWatch alarm, which invokes the Callback-Lambda perform.
- The Lambda perform publish messages to the SNS matter.
- The SNS matter, utilizing the subscription filter coverage, publishes the message to the respective SQS queue.
- The SQS queue triggers the Vacation spot-Lambda perform.
When SQL Server sends error logs to CloudWatch Logs, an automatic processing pipeline handles these logs. You create a subscription filter to match particular log entries that start with ##LambdaCallBack##, which serves as a marker for messages requiring Lambda perform invocation. For instance, with the log ##LambdaCallBack##arn:aws:lambda:eu-north-1:111122223333:perform:Vacation spot-Lambda##param##{id: 1, identify: “string”}, the Lambda perform CallBack-Lambda is triggered to course of the message.
You possibly can create the filter by the AWS Command Line Interface (AWS CLI), an API name, or the AWS Cloud Improvement Package (AWS CDK). On this submit, we use the AWS CDK.
Instantly invoking the Vacation spot-Lambda perform from database occasions may generate surprising prices with lots of of Lambda executions per second. This method additionally creates safety issues by requiring the Callback-Lambda perform to have broad permissions for invoking a number of Lambda capabilities. We suggest implementing a fan-out structure utilizing Amazon SNS with a subscription filter coverage to name the respective Amazon SQS queue, which acts as a message buffer for the Vacation spot-Lambda perform. The SQS queue serves as a decoupling mechanism, permitting the proxy Lambda (Callback-Lambda) to shortly ahead messages with minimal execution time, whereas the enterprise logic Lambda (Vacation spot-Lambda) processes messages asynchronously with managed concurrency. This method optimizes each efficiency and value administration.
Within the following sections, we stroll by the steps to allow error logging to CloudWatch, create the saved process, and deploy the answer utilizing the AWS CDK.
Stipulations
Earlier than deploying this answer, guarantee you might have the next stipulations:
- The AWS CLI put in and configured
- Python 3.11 or later
- The AWS CDK put in
- Collect the next details about the DB Occasion from the place you need to invoke Lambda
- DB Occasion identify (
) - VPC ID (
) - Subnet ID (
) - Safety Group ID (
)
- DB Occasion identify (
Publish RDS for SQL Server DB occasion logs to CloudWatch
You possibly can publish logs out of your RDS for SQL Server database to CloudWatch Logs. Supported logs embrace each agent logs and error logs, however this submit focuses on error logs. Publishing these logs to CloudWatch supplies steady visibility into database errors and exercise whereas storing the information in extremely sturdy storage managed by the CloudWatch Logs agent.
Full the next steps to publish the error logs to CloudWatch:
- Open the Amazon RDS console.
- Within the navigation pane, select Databases, after which select the DB occasion that you simply need to modify.
- Select Modify.
- Within the Log exports part, select the logs that you simply need to begin publishing to CloudWatch Logs. For this submit, we choose Error log.

- Evaluation Abstract of modifications displaying that Error log will likely be revealed to CloudWatch logs.

- Select to use for the modifications instantly.

- Select Modify DB Occasion.
You can too use the AWS CLI modify-db-instance or an API name to ModifyDBInstance
Command for Home windows:
Command for Linux/macOS:
Create a saved process to jot down a customized message to the ERRORLOG
The ERRORLOG captures crucial info reminiscent of server startups and shutdowns, authentication failures, backup operations, useful resource warnings, and important database occasions.SQL Server mechanically creates a brand new ERRORLOG file when the service begins and maintains six earlier log recordsdata by default (named ERRORLOG.1, ERRORLOG.2, and so forth). The present log is constantly named ERRORLOG with no quantity.
Database customers can log customized messages within the ERRORLOG utilizing the RAISERROR assertion with out elevated permissions. First, create a devoted database on the SQL Server occasion to comprise your code,
The next saved process makes use of RAISERROR to jot down customized messages to the ERRORLOG:
The customized message consists of three elements:
- ##LambdaCallBack## – An identifier string for the CloudWatch filter to detect customized messages
- @LambdaARN – The Amazon Useful resource Title (ARN) parameter figuring out the goal Lambda perform
- @lambdaArgs – The parameter containing arguments for Lambda perform execution throughout the RDS occasion
IMPORTANT: any info handed as arguments to sp_invoke_lambda will likely be written to SQL Server ERRORLOG and subsequently to CloudWatch Logs in plain textual content. Anybody with CloudWatch Logs entry permissions can view these arguments in clear textual content.
If you should cross delicate or confidential info (reminiscent of personally identifiable info, credentials, or business-sensitive information), you MUST
- Encrypt the delicate information earlier than passing it as arguments to sp_invoke_lambda
- Implement the decryption logic throughout the Callback-Lambda perform
- Confirm correct key administration and entry controls are in place
Deploy the answer utilizing the AWS CDK
Full the next steps to deploy the remaining answer assets:
- Clone the AWS CDK code hosted on GitHub:
- Navigate to the listing sample-invoke-lambda-from-rds-mssql:
- Bootstrap the AWS CDK. That is required the primary time you utilize the AWS CDK in a selected AWS setting (a mix of an AWS account and AWS Area).
- Deploy the stack:
The AWS CDK mechanically provisions and configures the next assets:
The IAM permissions are mechanically dealt with by the AWS CDK utilizing least privilege rules. The AWS CDK implements these permissions following AWS safety greatest practices, the place SNS is granted sns:Publish permissions for the Callback-Lambda and SQS is configured with sqs:SendMessage for SNS integration and sqs:ReceiveMessage/sqs:DeleteMessage for the Vacation spot-Lambda capabilities.
Validate the answer
Take a look at the answer by working the saved process out of your DB occasion:
The sp_invoke_lambda saved process writes a customized message to the SQL Server ERRORLOG with the Lambda perform identify and its arguments.
You possibly can confirm the profitable execution of the process by executing the sp_readerrorlog process:
The place:
- 0 – The log file quantity to view (0 is the present log)
- 1 – The product log sort (1 for SQL Server Log, 2 for SQL Agent Log)
- ##LambdaCallBack## – The filter string that identifies customized messages within the error log”

You will note within the CloudWatch log group a log like the next screenshot.

You can too view the subscription filter configured in that log group.

The filter triggers the Callback-Lambda perform. The Python used within the AWS CDK parses the log (it’s a base64 encoded string) and publishes the message within the SNS matter. Within the message is specified the message attributes:
Utilizing this filter, the subject pushes the message to the proper SQS queue that will likely be consumed by the goal Lambda perform. The Vacation spot-Lambda-A perform will print the message with the parsed message. The next screenshot exhibits the entire log occasions.

As a substitute, if you wish to invoke the Vacation spot-Lambda-B for a special scope:
This question will set off the opposite perform:

This instance demonstrates how the Vacation spot-Lambda-A and Vacation spot-Lambda-B capabilities processes parameters obtained from RDS. Whereas our instance merely prints these parameters, you may implement your particular enterprise logic inside this Lambda perform. Frequent use instances embrace calling REST APIs or sending SNS notifications for electronic mail or push alerts. This versatile method lets you outline and implement the exact enterprise logic your utility requires. By following the steps outlined on this submit, you may successfully leverage AWS companies to create scalable and environment friendly event-driven serverless architectures.
Clear up
Clear up the assets you created by finishing the next steps:
- Delete the AWS CDK assets:
- Manually delete the [Lambda] database.
- Disable ERRRORLOG publishing to CloudWatch.
- Delete the CloudWatch log group.
Conclusion
This submit demonstrated learn how to invoke AWS Lambda capabilities from Amazon RDS for SQL Server saved procedures utilizing a serverless integration sample. You discovered learn how to publish database error logs to Amazon CloudWatch Logs, create saved procedures that write customized messages to ERRORLOG, and deploy an automatic processing pipeline utilizing CloudWatch subscription filters, Amazon SNS, and Amazon SQS to set off Lambda capabilities based mostly on database occasions. The structure supplies a strong method to set off serverless computations from database occasions, producing automated responses to database operations whereas sustaining system reliability and cost-efficiency.
To get began utilizing this answer, obtain the entire AWS CDK template from the GitHub repo, and comply with the steps on this submit to deploy the answer in your setting.
We’d love to listen to from you. Share your expertise implementing this answer, ask questions, or counsel enhancements by leaving a remark beneath. You can too be part of the AWS Group Builders program to attach with different builders and share your serverless structure patterns.
Concerning the authors
