aws-lambda-event-sources: S3EventSource Requires Bucket, but only needs an IBucket
Describe the bug
I want to use an externally created bucket as an event notification source for a lambda
Expected Behavior
An IBucket can be used as the notification source
Current Behavior
Type errors. An IBucket cannot be assigned to a Bucket.
Reproduction Steps
import * as core from "aws-cdk-lib";
import * as event_sources from "aws-cdk-lib/aws-lambda-event-sources";
import * as s3 from "aws-cdk-lib/aws-s3";
const app = new core.App()
const ibucket = s3.Bucket.fromBucketName(app, "IBucket", "IBucket");
const eventSource = new event_sources.S3EventSource(ibucket, {events: [s3.EventType.OBJECT_CREATED]})
TS2345: Argument of type 'IBucket' is not assignable to parameter of type 'Bucket'.
Possible Solution
Since addEventNotification is the only method used by S3EventSource and is available on an IBucket, I think it should be safe to loosen the type restriction to IBucket.
Coercing the type and deploying works.
Additional Information/Context
No response
CDK CLI Version
2.40.0
Framework Version
No response
Node.js Version
16
OS
OSX
Language
Typescript
Language Version
No response
Other information
No response
Thanks for this request, it looks to me like it's not necessary to only allow a Bucket, and that having IBucket here would be fine. However, this would cause issues when adding a notification for a LambdaDestination, as that requires having the Bucket construct
However there is already an existing check for this: https://github.com/aws/aws-cdk/blob/c164a49c67def802a84f37b6ad779a78442e5a53/packages/%40aws-cdk/aws-s3-notifications/lib/lambda.ts#L17-L20
If properly documented we could be able to allow for imported buckets to be used here to allow for more use cases
I am marking this issue as p2, which means that we are unable to work on this immediately.
+1
+1
Tested on 2.81.0, this is not fixed yet. In case you are having the same issue, here's my alternative solution:
bucket = s3.Bucket.from_bucket_attributes(
self,
"ImportedBucket",
bucket_arn=f"arn:aws:s3:::{self.env.s3dir_source.bucket}",
)
use latest
bucket.add_event_notification(
s3.EventType.OBJECT_CREATED,
s3_notifications.LambdaDestination(
self.lambda_func_mapper[self.env.lbd_s3sync.name][KEY_FUNC],
),
s3.NotificationKeyFilter(
prefix=f"{self.env.s3dir_source.key}",
),
)
use alias
bucket.add_event_notification(
s3.EventType.OBJECT_CREATED,
s3_notifications.LambdaDestination(
lambda_.Function.from_function_attributes(
self,
f"LambdaAliasAttribute{self.env.lbd_s3sync.short_name_camel}",
function_arn=self.lambda_func_mapper[self.env.lbd_s3sync.name][KEY_ALIAS].function_arn,
same_environment=True,
),
),
s3.NotificationKeyFilter(
prefix=f"{self.env.s3dir_source.key}",
),
)
This appears to be a duplicate of this older issue: https://github.com/aws/aws-cdk/issues/4323. I'm going to close this in order to consolidate issues.
⚠️COMMENT VISIBILITY WARNING⚠️
Comments on closed issues are hard for our team to see. If you need more assistance, please either tag a team member or open a new issue that references this one. If you wish to keep having a conversation with other community members under this issue feel free to do so.