Skip to content

Commit a22544c

Browse files
brandonwatsonBrandon Watsonjosefaidt
authored
Updating the lambda-trigger mdx with an example (#8008)
* Updating the lambda-trigger mdx with an example * Update src/pages/[platform]/build-a-backend/storage/lambda-triggers/index.mdx * Update src/pages/[platform]/build-a-backend/storage/lambda-triggers/index.mdx --------- Co-authored-by: Brandon Watson <blwatson@kompartners.com> Co-authored-by: josef <josef.aidt@gmail.com>
1 parent cd0ea4e commit a22544c

File tree

1 file changed

+32
-0
lines changed
  • src/pages/[platform]/build-a-backend/storage/lambda-triggers

1 file changed

+32
-0
lines changed

src/pages/[platform]/build-a-backend/storage/lambda-triggers/index.mdx

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,35 @@ export const handler: S3Handler = async (event) => {
7676
</Callout>
7777

7878
Now, when you deploy your backend, these functions will be invoked whenever an object is uploaded or deleted from the bucket.
79+
80+
## More Advanced Triggers
81+
82+
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.
83+
84+
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.
85+
86+
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`.
87+
88+
```ts title="amplify/backend.ts"
89+
import { EventType } from 'aws-cdk-lib/aws-s3';
90+
import { LambdaDestination } from 'aws-cdk-lib/aws-s3-notifications';
91+
import { defineBackend } from '@aws-amplify/backend';
92+
import { storage } from './storage/resource';
93+
import { yourLambda } from './functions/yourLambda/resource';
94+
95+
const backend = defineBackend({
96+
storage,
97+
yourLambda,
98+
});
99+
100+
backend.storage.resources.bucket.addEventNotification(
101+
EventType.OBJECT_CREATED_PUT,
102+
new LambdaDestination(backend.yourLambda.resources.lambda),
103+
{
104+
prefix: 'protected/uploads/',
105+
suffix: '-uploadManifest.json',
106+
}
107+
);
108+
```
109+
110+
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.

0 commit comments

Comments
 (0)