[s3-notifications] s3 notification to lambda functions not working with existing Buckets
Using S3EventSource to create a lambda triggered by S3 Object_Created events of a bucket which already exists.
If you follow the instruction in the documentation (https://docs.aws.amazon.com/cdk/api/latest/docs/aws-lambda-event-sources-readme.html#s3) the resources cannot be created due to the following type mismatch:
Argument of type 'IBucket' is not assignable to parameter of type 'Bucket'.
Type 'IBucket' is missing the following properties from type 'Bucket': autoCreatePolicy, lifecycleRules, notifications, metrics, and 33 more.
Reproduction Steps
- Create a Function with "@aws-cdk/aws-lambda"
- Add EventSource using S3EventSource ("@aws-cdk/aws-lambda-event-sources") of external bucket ("@aws-cdk/aws-s3")
const function = new Function(stack, 'SomeFunction', { ... })
const bucket = Bucket.fromBucketName(stack, 'SomeBucket', bucketName)
function.addEventSource(
new S3EventSource(bucket, {
events: [EventType.OBJECT_CREATED]
})
)
What did you expect to happen?
I expected that creating a S3/Lambda trigger would be very easy with "@aws-cdk/aws-lambda-event-sources" without type errors. The deployment of such easy things should be so easy.
This is not the first time I have experienced problems with types or functions that do not allow to do what they should be able to do...
I don't understand why the Bucket returned by Bucket.fromBucketName is something different from new Bucket(). Both Bucket(s) are from the same lib and nevertheless have different types.
What actually happened?
Type Errors during development and deployment
Environment
- **CLI Version : "aws-cdk": "1.60.0",
- **Node.js Version: 12
- **OS : mac/ linux
- **Language (Version): TypeScript (3.9.3)
Other
/Volumes/Workspace/project/node_modules/@aws-cdk/aws-lambda-event-sources/lib/s3.ts:31
this.bucket.addEventNotification(event, new notifs.LambdaDestination(target), ...filters);
^
TypeError: this.bucket.addEventNotification is not a function
at S3EventSource.bind (/Volumes/Workspace/project/node_modules/@aws-cdk/aws-lambda-event-sources/lib/s3.ts:31:19)
at Function.addEventSource (/Volumes/Workspace/project/node_modules/@aws-cdk/aws-lambda/lib/function-base.ts:309:12)
at Object.<anonymous> (/Volumes/Workspace/project/cdk/functions/somefunction.ts:33:14)
at Module._compile (internal/modules/cjs/loader.js:959:30)
at Module.m._compile (/Volumes/Workspace/project/node_modules/ts-node/src/index.ts:858:23)
at Module._extensions..js (internal/modules/cjs/loader.js:995:10)
at Object.require.extensions.<computed> [as .ts] (/Volumes/Workspace/project/node_modules/ts-node/src/index.ts:861:12)
at Module.load (internal/modules/cjs/loader.js:815:32)
at Function.Module._load (internal/modules/cjs/loader.js:727:14)
at Module.require (internal/modules/cjs/loader.js:852:19)
This is :bug: Bug Report
@nija-at do you have any idea how to get this done with the latest version of aws-cdk?
The limitation is coming from the underlying BucketNotifications construct that has this caveat - https://github.com/aws/aws-cdk/blob/0ccbc5dfe5c841ec821ac98ac219e98984237cba/packages/%40aws-cdk/aws-s3/lib/notifications-resource/notifications-resource.ts#L10
Need to dive deeper to see why this restriction is in place.
The underlying problem is that S3 bucket notifications in the CDK uses a CloudFormation custom resource to call the PutBucketNotifications API under the hood (to get around the circular dependencies problem documented here).
For imported buckets, the current implementation would override any existing bucket notifications, and hence not supported on imported buckets.
Transferring this issue to @iliapolo who owns this area of the CDK.
@nija-at Thanks for the investigations
@ThomasKiec We already have an issue for this: https://github.com/aws/aws-cdk/issues/2004.
Can you look it over and let us know if its ok to close this one as duplicate?
@iliapolo Yes we can close this one as duplicate