In this tutorial, you create a Database Trigger. The Trigger calls a third-party API when a document is inserted into a collection. The Trigger's Function adds the official AWS Software Development Kit (SDK) as an external dependency. The Function then uses the SDK to write the inserted document to an S3 bucket as an Extended JSON object.
About This Task
The AWS S3 example in this tutorial illustrates a pattern that applies to any third-party service that has a Node.js library:
Add the service's npm package as an external dependency.
Import the package with
require()inside your Function and call it using the credentials you stored.
Before You Begin
To complete this tutorial, you must have:
An Atlas cluster with a test collection. To get started, see Create an Atlas Infinite or Atlas Core Cluster.
An AWS S3 bucket. To learn how to create one, see Create a general purpose bucket in the AWS documentation. Note the following for later use:
The full bucket name
The AWS region
An AWS IAM policy that allows
s3:PutObjecton theinserted-documents/*prefix of your bucket. To learn how to create one, see Create IAM policies in the AWS documentation.An AWS IAM user with the policy attached. To learn how to create one, see Create an IAM user in your AWS account in the AWS documentation.
An access key and secret access key for the IAM user. To learn how to create them, see Create access keys in the AWS documentation.
Steps
Store Your AWS Credentials as Values and Secrets
Create the following Values and Secrets to store your AWS credentials:
AwsAccessKeyId: Value that contains your AWS access key ID.AwsSecretAccessKeySecret: Secret that contains your AWS secret access key.AwsSecretAccessKey: Value that links to the Secret. This enables you to access the secret access key in your Function, while still keeping it stored securely as a Secret.
Navigate to the Values page.
In Atlas, go to the Triggers page.
If it's not already displayed, select the organization that contains your project from the Organizations menu in the navigation bar.
If it's not already displayed, select your project from the Projects menu in the navigation bar.
In the sidebar, click Triggers under the Streaming Data heading.
The Triggers page displays.
Click the View All Apps button.
Click the app that you want to create the Trigger in.
In the sidebar, click Values under the Build heading.
Store the secret access key.
Note
Secret values cannot be accessed directly, so you must create a second Value that links to the Secret.
Click Create a Value.
Enter
AwsSecretAccessKeyas the Value Name.Select the Value type.
Select the Link to Secret option.
Enter
AwsSecretAccessKeySecretand click Create "AwsSecretAccessKeySecret" to name the Secret.Paste your AWS secret access key into the field that appears below the Secret name.
Click Save Draft to create both the Secret and the Value.
Create the Database Trigger and Its Function
Create a Database Trigger that calls a new Function when a document is inserted. You add the aws-sdk dependency and define the Function code in the same form.
Navigate to the Triggers page.
In Atlas, go to the Triggers page.
If it's not already displayed, select the organization that contains your project from the Organizations menu in the navigation bar.
If it's not already displayed, select your project from the Projects menu in the navigation bar.
In the sidebar, click Triggers under the Streaming Data heading.
The Triggers page displays.
Create the Trigger.
Click Add Trigger to open the Trigger configuration page.
Select the Database Trigger type, then configure the Trigger using the following values:
SettingValueTrigger NameWriteDocumentToS3TriggerWatch AgainstCollectionCluster NameName of your test cluster
Database NameName of your test database
Collection NameName of your test collection
Operation TypeSelect only
Insert.Event TypeFunctionIn the Function section, select + New Function.
Enter the following code to define the Function.
Replace YOUR_AWS_REGION and YOUR_S3_BUCKET_NAME with your own AWS Region and the full bucket name from the AWS Management Console.
1 /* 2 * Write a newly-inserted document to an S3 bucket as an Extended JSON object. 3 */ 4 exports = async function writeDocumentToS3(changeEvent) { 5 const { fullDocument } = changeEvent; 6 7 // require calls must be in the exports function 8 const S3 = require("aws-sdk/clients/s3"); 9 const s3 = new S3({ 10 accessKeyId: context.values.get("AwsAccessKeyId"), 11 secretAccessKey: context.values.get("AwsSecretAccessKey"), 12 region: "YOUR_AWS_REGION", 13 }); 14 15 const key = `inserted-documents/${fullDocument._id.toString()}.json`; 16 17 // https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#putObject-property 18 await s3.putObject({ 19 Bucket: "YOUR_S3_BUCKET_NAME", 20 Key: key, 21 Body: EJSON.stringify(fullDocument), 22 ContentType: "application/json", 23 }).promise(); 24 25 return `Wrote document ${fullDocument._id} to ${key}`; 26 };
Each time a document is inserted, the Function does the following:
Reads the inserted document from the change event's
fullDocumentfield.Creates an S3 client that authenticates with the AWS credentials stored in your Values.
Writes the document to the
inserted-documents/prefix of your bucket as<_id>.json.
Note
The Function uses the document's _id as part of the S3 object key. If your documents use an _id type other than ObjectId, the key might not be what you expect.
Test the Trigger
Insert a test document into the collection that the Trigger watches. Confirm that a corresponding Extended JSON object appears in your S3 bucket under the inserted-documents/ prefix.
Connect to your cluster with mongosh.
To learn how, see Connect to a Cluster via mongosh.
Insert a document into the collection that the Trigger watches.
The following example inserts one document into the test collection. Replace YOUR_DATABASE_NAME and YOUR_COLLECTION_NAME with the database and collection that you entered when you created the Trigger.
// Replace YOUR_DATABASE_NAME and YOUR_COLLECTION_NAME with the // database and collection that the Trigger watches. use YOUR_DATABASE_NAME db.YOUR_COLLECTION_NAME.insertOne({ tutorialTest: "atlas-trigger-s3", createdAt: new Date() })
Note the insertedId value from your output. The Function uses this value as the name of the S3 object.
Confirm that the object exists in your S3 bucket.
In the AWS Management Console, open your S3 bucket and navigate to the inserted-documents/ prefix. An object named <insertedId>.json appears, where <insertedId> is the insertedId value from the previous step.
Note
If the object doesn't appear, review the Trigger's execution logs. To learn how, see Trigger Logs.