-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Bucket eventbridge notifications #6866
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
Open
yotamishak
wants to merge
2
commits into
anomalyco:dev
Choose a base branch
from
yotamishak:bucket-eventbridge-notifications
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+153
−4
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,18 +9,21 @@ import { hashStringToPrettyString, logicalName } from "../naming"; | |
| import { Component, Prettify, Transform, transform } from "../component"; | ||
| import { Link } from "../link"; | ||
| import type { Input } from "../input"; | ||
| import { FunctionArgs, FunctionArn } from "./function.js"; | ||
| import { Function, FunctionArgs, FunctionArn } from "./function.js"; | ||
| import { Workflow } from "./workflow.js"; | ||
| import { Duration, DurationDays, toSeconds } from "../duration"; | ||
| import { VisibleError } from "../error"; | ||
| import { parseBucketArn } from "./helpers/arn"; | ||
| import { BucketLambdaSubscriber } from "./bucket-lambda-subscriber"; | ||
| import { iam, s3 } from "@pulumi/aws"; | ||
| import { cloudwatch, iam, s3 } from "@pulumi/aws"; | ||
| import { permission } from "./permission"; | ||
| import { BucketQueueSubscriber } from "./bucket-queue-subscriber"; | ||
| import { BucketTopicSubscriber } from "./bucket-topic-subscriber"; | ||
| import { Queue } from "./queue"; | ||
| import { SnsTopic } from "./sns-topic"; | ||
| import { BucketNotification } from "./bucket-notification"; | ||
| import { BusLambdaSubscriber } from "./bus-lambda-subscriber"; | ||
| import type { BusSubscriberArgs } from "./bus"; | ||
|
|
||
| interface BucketCorsArgs { | ||
| /** | ||
|
|
@@ -532,10 +535,29 @@ export interface BucketArgs { | |
| } | ||
|
|
||
| export interface BucketNotificationsArgs { | ||
| /** | ||
| * Enable EventBridge notifications for this bucket. This publishes S3 events to | ||
| * the account's default EventBridge bus. | ||
| * | ||
| * @default `false` | ||
| * @example | ||
| * ```js | ||
| * { | ||
| * eventBridge: true, | ||
| * notifications: [ | ||
| * { | ||
| * name: "MySubscriber", | ||
| * queue: myQueue | ||
| * } | ||
| * ] | ||
| * } | ||
| * ``` | ||
| */ | ||
| eventBridge?: Input<boolean>; | ||
| /** | ||
| * A list of subscribers that'll be notified when events happen in the bucket. | ||
| */ | ||
| notifications: Input< | ||
| notifications?: Input< | ||
| Input<{ | ||
| /** | ||
| * The name of the subscriber. | ||
|
|
@@ -711,6 +733,54 @@ export interface BucketNotificationsArgs { | |
| }; | ||
| } | ||
|
|
||
| export interface BucketEventBridgeSubscriberArgs | ||
| extends Omit<BusSubscriberArgs, "pattern"> { | ||
| /** | ||
| * Filter the S3 events that'll be processed by the subscriber. The `source` and | ||
| * bucket name are automatically added to the EventBridge pattern. | ||
| */ | ||
| pattern?: Input<{ | ||
| /** | ||
| * A list of `detail-type` values to match against. | ||
| * | ||
| * @example | ||
| * ```js | ||
| * { | ||
| * detailType: ["Object Created"] | ||
| * } | ||
| * ``` | ||
| */ | ||
| detailType?: Input< | ||
| Input< | ||
| | "Object Created" | ||
| | "Object Deleted" | ||
| | "Object Restore Initiated" | ||
| | "Object Restore Completed" | ||
| | "Object Restore Expired" | ||
| | "Object Tags Added" | ||
| | "Object Tags Deleted" | ||
| | "Object ACL Updated" | ||
| | string | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should be closed list, but left room for error just incase |
||
| >[] | ||
| >; | ||
| /** | ||
| * An object of `detail` values to match. | ||
| * | ||
| * @example | ||
| * ```js | ||
| * { | ||
| * detail: { | ||
| * object: { | ||
| * key: [{ prefix: "images/" }] | ||
| * } | ||
| * } | ||
| * } | ||
| * ``` | ||
| */ | ||
| detail?: Record<string, any>; | ||
| }>; | ||
| } | ||
|
|
||
| /** | ||
| * @internal | ||
| */ | ||
|
|
@@ -1269,6 +1339,15 @@ export class Bucket extends Component implements Link.Linkable { | |
| * }); | ||
| * ``` | ||
| * | ||
| * Or enable EventBridge notifications for this bucket. This publishes S3 events | ||
| * to the account's default EventBridge bus. | ||
| * | ||
| * ```js title="sst.config.ts" | ||
| * bucket.notify({ | ||
| * eventBridge: true | ||
| * }); | ||
| * ``` | ||
| * | ||
| * You can also set it to only send notifications for specific S3 events. | ||
| * | ||
| * ```js {6} | ||
|
|
@@ -1303,6 +1382,11 @@ export class Bucket extends Component implements Link.Linkable { | |
| `Cannot call "notify" on the "${this.constructorName}" bucket multiple times. Calling it again will override previous notifications.`, | ||
| ); | ||
| } | ||
| if (!args.eventBridge && !args.notifications) { | ||
| throw new VisibleError( | ||
| `Cannot call "notify" on the "${this.constructorName}" bucket without notifications or EventBridge enabled.`, | ||
| ); | ||
| } | ||
| this.isSubscribed = true; | ||
| const name = this.constructorName; | ||
| const opts = this.constructorOpts; | ||
|
|
@@ -1317,6 +1401,70 @@ export class Bucket extends Component implements Link.Linkable { | |
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Subscribe to S3 EventBridge notifications from an existing bucket with a function. | ||
| * | ||
| * This creates an EventBridge rule and target only. The bucket owner must enable | ||
| * EventBridge notifications separately with `notify`. | ||
| * | ||
| * @param name The name of the subscription. | ||
| * @param bucketName The name of the S3 bucket to subscribe to. | ||
| * @param subscriber The function that'll be notified. | ||
| * @param args Configure the subscription. | ||
| * | ||
| * @example | ||
| * ```js title="sst.config.ts" | ||
| * sst.aws.Bucket.subscribeEventBridge("Uploads", "my-bucket", "src/subscriber.handler", { | ||
| * pattern: { | ||
| * detailType: ["Object Created"], | ||
| * detail: { | ||
| * object: { | ||
| * key: [{ prefix: "uploads/" }] | ||
| * } | ||
| * } | ||
| * } | ||
| * }); | ||
| * ``` | ||
| */ | ||
| public static subscribeEventBridge( | ||
| name: string, | ||
| bucketName: Input<string>, | ||
| subscriber: Input< | ||
| string | Workflow | Function | FunctionArgs | FunctionArn | ||
| >, | ||
| args: BucketEventBridgeSubscriberArgs = {}, | ||
| ) { | ||
| return all([bucketName, args]).apply(([bucketName, args]) => { | ||
| const bucket = logicalName(bucketName); | ||
| const bus = cloudwatch.EventBus.get( | ||
| `${bucket}EventBridgeSubscriber${name}DefaultBus`, | ||
| "default", | ||
| ); | ||
| const pattern = output(args.pattern ?? {}).apply((pattern) => { | ||
| const detail = pattern.detail ?? {}; | ||
|
|
||
| return { | ||
| source: ["aws.s3"], | ||
| detailType: pattern.detailType, | ||
| detail: { | ||
| ...detail, | ||
| bucket: { | ||
| ...(detail.bucket ?? {}), | ||
| name: [bucketName], | ||
| }, | ||
| }, | ||
| }; | ||
| }); | ||
|
|
||
| return new BusLambdaSubscriber(`${bucket}EventBridgeSubscriber${name}`, { | ||
| bus: { name: bus.name, arn: bus.arn }, | ||
| subscriber, | ||
| ...args, | ||
| pattern, | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Subscribe to events from this bucket. | ||
| * | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
changed this to optional, allowing only to pass eventbridge true
defaults to empty array uptop