From 8fa00638ce0ea005e0d24902e6bdeed3b3e65e4f Mon Sep 17 00:00:00 2001 From: Brandon Watson Date: Mon, 30 Sep 2024 15:34:44 -0600 Subject: [PATCH 1/3] Updating the lambda-trigger mdx with an example --- .../storage/lambda-triggers/index.mdx | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/pages/[platform]/build-a-backend/storage/lambda-triggers/index.mdx b/src/pages/[platform]/build-a-backend/storage/lambda-triggers/index.mdx index fea79c35700..44c50067e16 100644 --- a/src/pages/[platform]/build-a-backend/storage/lambda-triggers/index.mdx +++ b/src/pages/[platform]/build-a-backend/storage/lambda-triggers/index.mdx @@ -76,3 +76,32 @@ export const handler: S3Handler = async (event) => { Now, when you deploy your backend, these functions will be invoked whenever an object is uploaded or deleted from the bucket. + +## More Advanced Triggers + +The example listed above demonstrates what is exposed directly in your `storage` definition. Specifically, the use of the `triggers` option when you use `defineStorage`. This method is for simple triggers that always execute on file uploads or file deletions. There are no additional modifications you can make to the triggers defined in this way. + +If you want the ability to do something more than simply handle the events `onUpload` and `onDelete` you will have to use `.addEventNotification` in your `backend.ts`. If you use this method, the `triggers` section in your `storage/resource.ts` file should be removed. + +Here is an example of how you can add a lambda trigger for an s3 object PUT event. This trigger will execute when a file that has been uploaded to the bucket defined in your `storage/resource.ts` has a matching prefix and suffix as that listed in the function input of `addEventNotification`. + +```ts title="amplify/backend.ts" +import { defineBackend } from '@aws-amplify/backend'; +import { storage } from './storage/resource'; + +import { EventType } from 'aws-cdk-lib/aws-s3'; +import { LambdaDestination } from 'aws-cdk-lib/aws-s3-notifications'; + +import { yourLambda } from './functions/yourLambda/resource'; + +backend.storage.resources.bucket.addEventNotification( + EventType.OBJECT_CREATED_PUT, + new LambdaDestination(backend.yourLambda.resources.lambda), + { + prefix: 'protected/uploads/', + suffix: '-uploadManifest.json', + } +); +``` + +It's important to note that using this methodology does not require any changes your lambda function. This modification on your `backend.ts` file will create a new `AWS CloudFormation handler for "Custom::S3BucketNotifications" resources (@aws-cdk/aws-s3)` that specifically handles checking the prefix and suffix. From f2269f64bda204e135ba378471dfd24d34f35c7c Mon Sep 17 00:00:00 2001 From: josef Date: Tue, 1 Oct 2024 16:43:19 -0700 Subject: [PATCH 2/3] Update src/pages/[platform]/build-a-backend/storage/lambda-triggers/index.mdx --- .../build-a-backend/storage/lambda-triggers/index.mdx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pages/[platform]/build-a-backend/storage/lambda-triggers/index.mdx b/src/pages/[platform]/build-a-backend/storage/lambda-triggers/index.mdx index 44c50067e16..77ab3cfc324 100644 --- a/src/pages/[platform]/build-a-backend/storage/lambda-triggers/index.mdx +++ b/src/pages/[platform]/build-a-backend/storage/lambda-triggers/index.mdx @@ -83,7 +83,7 @@ The example listed above demonstrates what is exposed directly in your `storage` If you want the ability to do something more than simply handle the events `onUpload` and `onDelete` you will have to use `.addEventNotification` in your `backend.ts`. If you use this method, the `triggers` section in your `storage/resource.ts` file should be removed. -Here is an example of how you can add a lambda trigger for an s3 object PUT event. This trigger will execute when a file that has been uploaded to the bucket defined in your `storage/resource.ts` has a matching prefix and suffix as that listed in the function input of `addEventNotification`. +Here is an example of how you can add a Lambda trigger for an S3 object PUT event. This trigger will execute when a file that has been uploaded to the bucket defined in your `storage/resource.ts` has a matching prefix and suffix as that listed in the function input of `addEventNotification`. ```ts title="amplify/backend.ts" import { defineBackend } from '@aws-amplify/backend'; From 4f06c7e7405858469f95633c584dd0f68fcbed5f Mon Sep 17 00:00:00 2001 From: josef Date: Tue, 1 Oct 2024 16:44:49 -0700 Subject: [PATCH 3/3] Update src/pages/[platform]/build-a-backend/storage/lambda-triggers/index.mdx --- .../build-a-backend/storage/lambda-triggers/index.mdx | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/pages/[platform]/build-a-backend/storage/lambda-triggers/index.mdx b/src/pages/[platform]/build-a-backend/storage/lambda-triggers/index.mdx index 77ab3cfc324..86ac84f2b6d 100644 --- a/src/pages/[platform]/build-a-backend/storage/lambda-triggers/index.mdx +++ b/src/pages/[platform]/build-a-backend/storage/lambda-triggers/index.mdx @@ -86,14 +86,17 @@ If you want the ability to do something more than simply handle the events `onUp Here is an example of how you can add a Lambda trigger for an S3 object PUT event. This trigger will execute when a file that has been uploaded to the bucket defined in your `storage/resource.ts` has a matching prefix and suffix as that listed in the function input of `addEventNotification`. ```ts title="amplify/backend.ts" -import { defineBackend } from '@aws-amplify/backend'; -import { storage } from './storage/resource'; - import { EventType } from 'aws-cdk-lib/aws-s3'; import { LambdaDestination } from 'aws-cdk-lib/aws-s3-notifications'; - +import { defineBackend } from '@aws-amplify/backend'; +import { storage } from './storage/resource'; import { yourLambda } from './functions/yourLambda/resource'; +const backend = defineBackend({ + storage, + yourLambda, +}); + backend.storage.resources.bucket.addEventNotification( EventType.OBJECT_CREATED_PUT, new LambdaDestination(backend.yourLambda.resources.lambda),