You can return to the original look by selecting English in the language selector
above.

Invoking a Lambda Function from an Amazon Aurora MySQL DB
Cluster

You can invoke an AWS Lambda function from an Amazon Aurora with MySQL compatibility
DB cluster with a native function
or a stored procedure. Before invoking a Lambda function from an Aurora MySQL, the
Aurora DB cluster
must have access to Lambda.

Starting with Aurora MySQL version 1.16, using a stored procedure is deprecated.
For Aurora MySQL 5.6-compatible clusters using Aurora MySQL version 1.16 and later,
we strongly recommend using an Aurora MySQL native function.

For Aurora MySQL 5.7-compatible clusters, currently only the stored procedure technique
is available.

You can invoke an AWS Lambda function from an Aurora MySQL DB cluster by calling the
native functions lambda_sync and lambda_async. This approach
can be useful when you want to integrate your database running on Aurora MySQL with
other AWS services.
For example, you might want to send a notification using Amazon Simple Notification
Service (Amazon SNS) whenever a row is inserted into a
specific table in your database.

Working with Native Functions to Invoke
a Lambda Function

The lambda_sync and lambda_async functions are built-in,
native functions that invoke a Lambda function synchronously or asynchronously.
When you must know the result of the invoked function's execution before moving
on to another action, use the synchronous function lambda_sync.
When you don't need to know the result of the execution before moving on to
another action, use the asynchronous function lambda_async.

The user invoking a native function must be granted the INVOKE LAMBDA
privilege. To grant this privilege to a user, connect to the DB instance as the
master user, and run the following statement.

GRANT INVOKE LAMBDA ON *.* TO user@domain-or-ip-address

You can revoke this privilege by running the following statement.

REVOKE INVOKE LAMBDA ON *.* FROM user@domain-or-ip-address

Syntax for the lambda_sync Function

You invoke the lambda_sync function synchronously with the
RequestResponse invocation type. The function returns the
result of the Lambda invocation in a JSON payload. The function has the
following syntax.

lambda_sync (
lambda_function_ARN,
JSON_payload
)

Note

You can use triggers to call Lambda on data-modifying statements.
Remember that triggers are not executed once per SQL statement, but
once per row modified, one row at a time. Trigger execution is synchronous,
and the data-modifying statement will not return until trigger execution
completes.

Be careful when invoking an AWS Lambda function from triggers on tables
that experience high write traffic. INSERT, UPDATE, and DELETE triggers
are activated per row. A write-heavy workload on a table with INSERT,
UPDATE, or DELETE triggers results in a large number of calls to your
AWS Lambda function.

Parameters for the lambda_sync Function

The lambda_sync function has the following parameters.

lambda_function_ARN

The Amazon Resource Name (ARN) of the Lambda function to
invoke.

JSON_payload

The payload for the invoked Lambda function, in JSON
format.

Note

Aurora MySQL doesn't support JSON parsing. JSON parsing isn't required when a Lambda
function returns an atomic value, such as a number or a string.

Example for the lambda_sync Function

The following query based on lambda_sync invokes the Lambda
function BasicTestLambda synchronously using the function ARN.
The payload for the function is {"operation":
"ping"}.

Related
Topics

Invoking a Lambda Function with an Aurora MySQL Stored Procedure

You can invoke an AWS Lambda function from an Aurora MySQL DB cluster by calling the
mysql.lambda_async procedure. This approach can be useful when you want
to integrate your database running on Aurora MySQL with other AWS services. For example,
you might want to send a notification using Amazon Simple Notification Service (Amazon
SNS) whenever a row is inserted
into a specific table in your database.

Aurora MySQL Version Considerations

In Aurora MySQL version 1.8 and later, you can use the native function method instead
of these stored procedures to invoke a Lambda function.
Starting with Amazon Aurora version 1.16, the stored procedure mysql.lambda_async is deprecated.
If you are using Aurora version 1.16 or later, we strongly recommend that you work
with native Lambda functions instead.
For more information about the native functions, see Working with Native Functions to Invoke
a Lambda Function.

Currently, in Aurora MySQL 2.* you cannot use the native function technique to invoke
a lambda function.
For an Aurora MySQL 5.7-compatible cluster, use the stored procedure technique described
in the following section.

Working with the
mysql.lambda_async Procedure to Invoke a Lambda Function

The mysql.lambda_async procedure is a built-in stored procedure that
invokes a Lambda function asynchronously. To use this procedure, your database user
must have execute privilege on the mysql.lambda_async stored
procedure.

Examples

As a best practice, we recommend that you wrap calls to the
mysql.lambda_async procedure in a stored procedure that can be
called from different sources such as triggers or client code. This approach can
help to avoid impedance mismatch issues and make it easier to invoke Lambda
functions.

Note

Be careful when invoking an AWS Lambda function from triggers on tables
that experience high write traffic. INSERT, UPDATE, and DELETE triggers
are activated per row. A write-heavy workload on a table with INSERT,
UPDATE, or DELETE triggers results in a large number of calls to your
AWS Lambda function.

Although calls to the mysql.lambda_async procedure are
asynchronous, triggers are synchronous. A statement that results in a
large number of trigger activations doesn't wait for the call to the
AWS Lambda function to complete, but it does wait for the triggers to
complete before returning control to the client.

Example: Invoke an AWS Lambda Function to Send Email

The following example creates a stored procedure that you can call in your
database code to send an email using a Lambda function.