Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AWS Lambda before publish rule #2484

Open
wants to merge 1 commit into
base: integration/chat-moderation
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions content/chat/rooms/moderation/lambda.textile
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
---
title: AWS Lambda
meta_description: "Detect and remove unwanted content in a Chat Room using AWS Lambda."
product: chat
languages:
- javascript
- react
- swift
- kotlin
---

The AWS Lambda rule is a powerful way to bring your own moderation solution to Ably Chat. It allows you to run custom moderation logic or integrate with your preferred moderation provider by configuring an AWS Lambda function that will be invoked before messages are published to a chat room.

This rule is particularly useful when you want to:
* Integrate with a custom moderation service
* Implement your own moderation logic
* Use a moderation provider that isn't directly supported by Ably

h2(#fields). Rule fields

- AWS Region := The region where your Lambda function is deployed.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On other PRs, we've been asked to sentence-case these, i.e. AWS region - so we should do the same here.

Comment for reference: #2481 (comment)

- Function Name := The name of your AWS Lambda function.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can also be the function ARN, if I'm not mistaken?

- AWS Authentication Scheme := The authentication method to use. Either @AWS Credentials@ or @ARN of an assumable role@. See the "Ably AWS authentication documentation":https://ably.com/docs/integrations/webhooks/lambda#aws-authentication.
- Retry Timeout := Maximum duration (in milliseconds) that an attempt to invoke the rule may take (including any retries).
- Max Retries := Maximum number of retries after the first attempt at invoking the rule.
- Failed Action := The action to take in the event that a rule fails to invoke. Options are reject the request or publish anyway.
- Room Filter (Optional) := A regular expression to match to specific chat rooms.

h2(#lambda_setup). Lambda Function Setup

To use the AWS Lambda rule, you need to create a Lambda function that adheres to the following API contract:

h3(#request). Request Format

<pre><code class="json">{
"source": "string",
"appId": "string",
"roomId": "string",
"site": "string",
"ruleId": "string",
"message": {
"roomId": "string",
"clientId": "string",
"text": "string",
"metadata": {
"key": "any"
},
"headers": {
"key": "string"
}
}
}</code></pre>

h3(#response). Response Format

Your Lambda function must return a response in the following format:

<pre><code class="json">{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For Lambda specifically, we have the special body with statusCode that lets us handle user-code level retries. I've sent you a slack message on this :)

"action": "accept|reject",
"message": {
"text": "string",
"metadata": {
"key": "any"
},
"headers": {
"key": "string"
}
},
"rejectionReason": {
"key": "any"
}
}</code></pre>

* @action@: Must be either @accept@ or @reject@
* @message@: Optional. If provided with @action: "accept"@, it allows you to modify the message before it's published. You can modify the @text@, @metadata@, and @headers@ fields.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This functionality doesn't yet exist, so we shouldn't put it in the docs

* @rejectionReason@: Optional. If provided with @action: "reject"@, it can contain any information about why the message was rejected. This information may be sent back to clients.

h2(#rejections). Handling rejections

Since the invocation of the Lambda function is synchronous relative to the publish operation, any rejection by the moderation function will result in the publish attempt being refused immediately. The rejection will use error code @42213@.

h2(#best_practices). Best Practices

When implementing your Lambda function, consider the following:

* Keep your function execution time as low as possible to minimize latency
* Implement proper error handling and logging
* Consider implementing rate limiting if you're using a third-party moderation service
* Use appropriate IAM roles and permissions for your Lambda function
* Consider implementing caching for frequently occurring content
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the situation with the lambda returning retriable vs non-retriable errors?

What's the form of the error response (ie what errors are returned by the function) in either case?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added those details


h3(#error_handling). Error Handling

The Lambda function can return two types of errors:

* Retriable errors: HTTP status codes 429 (Too Many Requests) and 500-599 (Server Errors)
* Non-retriable errors: All other HTTP status codes

When a retriable error occurs, Ably will retry the invocation according to the configured retry settings. For non-retriable errors, the publish attempt will be refused immediately with the error.

For more information on AWS Lambda integration with Ably, please refer to the "Ably AWS Lambda documentation":https://ably.com/docs/integrations/webhooks/lambda.