I'm using the new AWS CDK(Cloud Development Toolkit) to build the infrastructure on AWS in Java.
What I have to do: lookup an s3 bucket and add a trigger that invokes a lambda function.
What I have done:
Looked up s3 bucket:
IBucket bucket = Bucket.fromBucketName(scope, bucketId, bucketName);
Add a new event source to the existing lambda:
IEventSource eventSource = getObjectCreationEvent();
lambda.addEventSource(eventSource);
Where getObjectCreationEvent() is:
private S3EventSource getObjectCreationEvent() {
return new S3EventSource(bucket, new S3EventSourceProps() {
@Override
public List<EventType> getEvents() {
return Collections.singletonList(EventType.OBJECT_CREATED);
}
});
}
What is the problem:
The type of bucket parameter in the S3EventSource constructor is Bucket but every lookup method (e.g. the Bucket.fromBucketName()) returns an IBucket and not a Bucket, so there is a signature mismatch. If I cast IBucket to Bucket I have a ClassCastException.
From the git issue tracker https://github.com/aws/aws-cdk/issues/2004#issuecomment-479923251
Due to current limitations with CloudFormation and the way we implement bucket notifications in the CDK, it is impossible to add bucket notifications to an imported bucket. This is why the event source uses s3.Bucket instead of s3.IBucket.
You could use outPutObject:
const bucket = s3.Bucket.import(this, 'B', { bucketName: 'my-bucket' }); const fn = new lambda.Function(this, 'F', { code: lambda.Code.inline('boom'), runtime: lambda.Runtime.NodeJS810, handler: 'index.handler' }); bucket.onPutObject('put-object', fn);But reading further, this doesn't seem to work either.
It seems that the answer currently is:
It is impossible to configure.