Skip to content
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

Adding s3 event notification - add_event_notification() got an unexpected keyword argument 'filters' #8100

Closed
denmat opened this issue May 20, 2020 · 2 comments
Assignees
Labels
@aws-cdk/aws-s3-notifications guidance Question that needs advice or information.

Comments

@denmat
Copy link

denmat commented May 20, 2020

When adding an event notification to a s3 bucket, I am getting the following error.

add_event_notification() got an unexpected keyword argument 'filters'

Reproduction Steps

        bucket = s3.Bucket(self, 'abucket')

        bucket.add_event_notification(
            event=s3.EventType.OBJECT_CREATED,
            dest=s3.BucketNotificationDestinationConfig(
                arn="arn:aws:lambda:ap-southeast-2:<account_id>:function:bulk-load-BulkLoadLoader3C91558D-8PD5AGNHA1CZ",
                type=s3.BucketNotificationDestinationType.LAMBDA,
            ),
            filters=s3.NotificationKeyFilter(
                prefix="the/place",
                suffix="*.mp3",
            ),
        )

cdk synth

Error Log

Trace is:

Traceback (most recent call last):
  File "app.py", line 20, in <module>
    ttt = TestcaseVpcIdStack(app, "testcase-vpc-id", config=config, env=env)
  File "/Users/denmat/.pyenv/versions/3.8.1/lib/python3.8/site-packages/jsii/_runtime.py", line 66, in __call__
    inst = super().__call__(*args, **kwargs)
  File "/Users/denmat/tmp/cdk/testcase-vpc-id/testcase_vpc_id/testcase_vpc_id_stack.py", line 33, in __init__
    bucket.add_event_notification(
TypeError: add_event_notification() got an unexpected keyword argument 'filters'
Subprocess exited with error 1

Environment

  • CLI Version : CDK toolkit version: 1.39.0 (build 5d727c1)
  • Framework Version: 1.39.0 (node 12.10.0)
  • OS : Mac
  • Language : Python 3.8.1

This is 🐛 Bug Report

@denmat denmat added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels May 20, 2020
@iliapolo iliapolo added guidance Question that needs advice or information. and removed bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Jun 18, 2020
@iliapolo
Copy link
Contributor

Hi @denmat. Apologies for the delayed response.

There are two issues here:

  1. filters is not a regular argument, its variadic. From the docs:
add_event_notification(event, dest, *filters)

Which means you can't use it as a named argument. Also note this means you can't use any of the other arguments as named.

  1. dest is actually an interface (IBucketNotificationDestination), not a BucketNotificationDestinationConfig

The docs for this interface state:

Implemented by constructs that can be used as bucket notification destinations.

Which means that you should look for the relevant class that implements the destination you want. Unfortunately this is not trivial too find due to some limitations we have in python doc generation. But the typescript docs do provide this information:

Implemented by LambdaDestination, SnsDestination, SqsDestination

All in all, here is how the invocation should look like:

from aws_cdk import core
from aws_cdk import aws_s3 as s3
from aws_cdk import aws_s3_notifications as s3_notifications
from aws_cdk import aws_lambda as s3_lambda


class Issues8100Stack(core.Stack):

    def __init__(self, scope: core.Construct, id: str, **kwargs) -> None:
        super().__init__(scope, id, **kwargs)

        # The code that defines your stack goes here

        bucket = s3.Bucket(self, 'abucket')

        lamb = s3_lambda.Function.from_function_arn(self, 'Function', 'arn:aws:lambda:ap-southeast-2:<account_id>:function:bulk-load-BulkLoadLoader3C91558D-8PD5AGNHA1CZ')

        bucket.add_event_notification(
            s3.EventType.OBJECT_CREATED,
            s3_notifications.LambdaDestination(lamb),
            s3.NotificationKeyFilter(
                prefix="the/place",
                suffix="*.mp3",
            ),
        )

Notice you have to add the "aws-cdk.aws_s3_notifications==1.39.0" dependency in your setup.py. And for completeness, so that you don't import transitive dependencies, also add "aws-cdk.aws_lambda==1.39.0".

Hope this helps.

@iliapolo iliapolo added the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label Jun 18, 2020
@denmat
Copy link
Author

denmat commented Jun 25, 2020

Thank you for your detailed response. *filters had me stumped and trying to come up with a google search for an * did my head in :)

@denmat denmat closed this as completed Jun 25, 2020
@iliapolo iliapolo removed the response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. label Jun 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-s3-notifications guidance Question that needs advice or information.
Projects
None yet
Development

No branches or pull requests

3 participants