Friday, February 6, 2026

Set off AWS Lambda capabilities from Amazon RDS for SQL Server database occasions


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:

  1. Amazon RDS for SQL Server publishes occasion error logs to CloudWatch Logs.
  2. A customized message within the error logs triggers a CloudWatch metric filter.
  3. The modified worth within the metric filter triggers a CloudWatch alarm, which invokes the Callback-Lambda perform.
  4. The Lambda perform publish messages to the SNS matter.
  5. The SNS matter, utilizing the subscription filter coverage, publishes the message to the respective SQS queue.
  6. 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 ()

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:

  1. Open the Amazon RDS console.
  2. Within the navigation pane, select Databases, after which select the DB occasion that you simply need to modify.
  3. Select Modify.
  4. 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.

    Log exports configuration interface showing checkboxes for Agent log and Error log options, with Error log selected for publishing to Amazon CloudWatch Logs.
  5. Evaluation Abstract of modifications displaying that Error log will likely be revealed to CloudWatch logs.

    AWS RDS database modification summary screen showing CloudWatch Logs configuration for error log publishing
  6. Select to use for the modifications instantly.

    Amazon RDS Schedule modifications dialog showing two options: apply during next maintenance window or apply immediately, with the immediate option selected.
  7. Select Modify DB Occasion.

You can too use the AWS CLI modify-db-instance or an API name to ModifyDBInstance

Command for Home windows:

aws rds modify-db-instance ^
--db-instance-identifier <mssql-instance-name> ^
--cloudwatch-logs-export-configuration "{"EnableLogTypes":["error"]}" ^ 
--region 

Command for Linux/macOS:

aws rds modify-db-instance 
--db-instance-identifier <mssql-instance-name> 
--cloudwatch-logs-export-configuration '{"EnableLogTypes":["error"]}'  --region 

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,

CREATE DATABASE [Lambda]
GO
USE [Lambda]
GO

The next saved process makes use of RAISERROR to jot down customized messages to the ERRORLOG:

CREATE PROCEDURE [dbo].[sp_invoke_lambda] 
    @lambdaARN nvarchar(128),
    @lambdaArgs nvarchar(2000)
AS
BEGIN    
    SET NOCOUNT ON;
    DECLARE @msgtext VARCHAR(2047);
    SET @msgtext = CONCAT('##LambdaCallBack##', @lambdaARN, '##params##', @lambdaArgs);
    RAISERROR (@msgtext,16,1) WITH LOG;
END
GO

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

  1. Encrypt the delicate information earlier than passing it as arguments to sp_invoke_lambda
  2. Implement the decryption logic throughout the Callback-Lambda perform
  3. 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:

  1. Clone the AWS CDK code hosted on GitHub:
    $ git clone https://github.com/aws-samples/sample-invoke-lambda-from-rds-mssql.git

  2. Navigate to the listing sample-invoke-lambda-from-rds-mssql:
    $ cd sample-invoke-lambda-from-rds-mssql

  3. 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).
  4. Deploy the stack:
    $ cdk deploy -c instance_name= -c vpc_id= -c subnet_id= -c sg_id=

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:

exec lambda.dbo.sp_invoke_lambda 
	‘’,
	'{"id": 2, "identify": "Michele", "surname": "Scarimbolo"}'

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:

exec sp_readerrorlog 
	0,
	1,
	'##LambdaCallBack##'

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”

Database results table showing a Lambda callback log entry with timestamp 2025-08-27 08:19:31.890, process ID spid60, and AWS Lambda ARN from EU North 1 region

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

AWS CloudWatch Log Events interface showing error logs for RDS instance mssql01.node1 with filter bar, time range controls, and log entry table displaying timestamp and message columns.

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

AWS CloudWatch console showing the subscription filters configuration for an RDS SQL Server instance error log group with one active Lambda callback filter.

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:

MessageAttributes={
    "targetLambdaArn": {
        "DataType": "String",
        "StringValue": target_lambda_arn
    }
}

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.

AWS CloudWatch Logs interface displaying Lambda function execution logs with timestamps, performance metrics, and navigation controls for the RdsLambdaBlogStack-DestinationLambda function

As a substitute, if you wish to invoke the Vacation spot-Lambda-B for a special scope:

exec lambda.dbo.sp_invoke_lambda 
	‘’,
	'{"id": 1, "identify": "Andrea", "surname": "Caldarone"}'

This question will set off the opposite perform:

AWS CloudWatch Logs interface showing log events for RdsLambdaBlogStack Lambda function with execution metrics including 1.40ms duration and 34MB memory usage

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:

  1. Delete the AWS CDK assets:
  2. Manually delete the [Lambda] database.
  3. Disable ERRRORLOG publishing to CloudWatch.
  4. 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

Andrea Caldarone

Andrea Caldarone

Andrea is a Principal Technical Account Supervisor at AWS. He’s a database fanatic with 30 years of expertise in relational database methods. Outdoors of labor, Andrea performs soccer for an newbie workforce and likes to make pizza for his household and associates.

Michele Scarimbolo

Michele Scarimbolo

Michele is a Technical Account Supervisor at AWS. He began his profession with net and cellular improvement and now helps prospects construct serverless options. Outdoors of labor, he enjoys swimming along with his workforce and touring to attempt new meals.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles