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

Ability to provide EventPattern #816

Closed
takeda opened this issue May 2, 2017 · 6 comments
Closed

Ability to provide EventPattern #816

takeda opened this issue May 2, 2017 · 6 comments

Comments

@takeda
Copy link

takeda commented May 2, 2017

It looks like implementing this through zappa is currently not possible: https://aws.amazon.com/blogs/compute/building-a-dynamic-dns-for-route-53-using-cloudwatch-events-and-lambda/

Particularly equivalent of this command:
aws events put-rule --event-pattern "{\"source\":[\"aws.ec2\"],\"detail-type\":[\"EC2 Instance State-change Notification\"],\"detail\":{\"state\":[\"running\",\"shutting-down\",\"stopped\"]}}" --state ENABLED --name ec2_lambda_ddns_rule

@Miserlou
Copy link
Owner

Miserlou commented May 5, 2017

Why not possible? These should all be supported: http://docs.aws.amazon.com/lambda/latest/dg/invoking-lambda-function.html#supported-event-source-cloudwatch-events

Feel free to re-open if not, but I think it is.

@Miserlou Miserlou closed this as completed May 5, 2017
@takeda
Copy link
Author

takeda commented May 5, 2017

@Miserlou could you show how to define this in Zappa? I could not find this anywhere in the documentation.

I ended up modifying the code to get the functionality, but if there is a way to do it I would prefer to do it properly.

@Miserlou
Copy link
Owner

Miserlou commented May 5, 2017 via email

@takeda
Copy link
Author

takeda commented May 6, 2017

Added my changes (it's not suitable to be merged yet)

@mjschultz
Copy link

@Miserlou as far as I can tell this is not supported by Zappa right now; at least for EC2 events from Cloudwatch Events.

I was kind of able to get it working by using the following:

        "events": [{
            "function": "my_lambda.ec2_event",
            "event_source": {
                "arn": "arn:aws:events:us-east-1:<account-id>:rule/ec2-notifier",
                "enabled": true,
                "pattern": "{\"source\": [\"aws.ec2\"], \"detail-type\": [\"EC2 Instance State-change Notification\"]}",
                "events": []
            },
        }]

This allows kappa to create/update/delete the event (I believe). However, I had to tweak the zappa code that defines funk.name as the full lambda_arn to just the actual lambda name (the full ARN is too long and contains invalid characters for the CloudwatchEvents trigger). I just changed it to lambda_arn.split(':')[-1], not sure if that's acceptable in the general case.

I also had to override the lambda_handler in the config so it could pick up the 'EC2 Instance State-change Notification' detail-type in the event message (similar to the way the default handler takes the 'Scheduled Event' to dispatch to the defined function).

I'm not sure if all that is needed/safe to do in general though, so I'm not ready to make a PR.

@braghetto
Copy link

braghetto commented Nov 8, 2018

@mjschultz is rigth! Thanks you put me on the rigth track.

The CloudWatchEventSource class is using a wrong target name for the lambda function.
Instead of using lambda arn as the target name, only the name of the function should be used.

I made a small change in the class to work around the problem for now.
Here is:

def add(self, function): function.name = function.name.split(":")[-1:][0]
def remove(self, function): function.name = function.name.split(":")[-1:][0]
def status(self, function): function.name = function.name.split(":")[-1:][0]

Just overriding the function.name on the beginning of each method.
In this way each method who uses function.name inside now has the corretc value for the target function name.

This is my zappa conf:
"function": "core.workflow.events.event_mediaconvert", "event_source": { "arn": "arn:aws:events:us-east-1:X:rule/mediaconvertevents", "enabled": true, "pattern": "{\"source\": [\"aws.mediaconvert\"], \"detail-type\": [\"MediaConvert Job State Change\"]}", "events": [] } }

P.S.: If you have stale configs in cloudwatch rules and lambda triggers about this event, remove that on console first. After that zappa can schedule and unschedule normally using this patch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants