pulumi-aws icon indicating copy to clipboard operation
pulumi-aws copied to clipboard

CloudWatch EventRule onEvent does not use eventBusName

Open austinbutler opened this issue 4 years ago • 3 comments

When specifying the eventBusName on an EventRule, the onEvent seems to just use the default event bus.

import * as aws from "@pulumi/aws";

const eventBusHealth = new aws.cloudwatch.EventBus("health");

const ec2IssueEventRule = new aws.cloudwatch.EventRule("ec2-issue", {
  description: "Trigger Lambda to respond to EC2 issue events",
  eventPattern: JSON.stringify({
    source: ["aws.health"],
    "detail-type": ["AWS Health Event"],
    detail: {
      service: ["EC2"],
      eventTypeCategory: ["issue"],
    },
  }),
  eventBusName: eventBusHealth.name,
});

ec2IssueEventRule.onEvent("ec2-issue", (event) => {
  console.log(event);
});

This results an error since the rule doesn't exist on the default bus.

  aws:cloudwatch:EventTarget (ec2-issue):
    error: 1 error occurred:
        * Creating CloudWatch Events Target failed: ResourceNotFoundException: Rule ec2-issue-3bc3cfa does not exist on EventBus default.

austinbutler avatar Jan 26 '21 00:01 austinbutler

Specifically, it looks like there's no way to specify the bus name (https://github.com/pulumi/pulumi-aws/blob/master/sdk/nodejs/cloudwatch/eventTarget.ts#L414) via https://github.com/pulumi/pulumi-aws/blob/master/sdk/nodejs/cloudwatch/eventRuleMixins.ts#L106

As a workaround, you can specify a transformation to the resource options in onEvent to modify the underlying EventRule to include this as an input properly.

leezen avatar Jan 31 '21 23:01 leezen

Would be great to have a proper fix for this. I'm not sure name-spacing my default eventbus with an environment (stack) property on the EventRule is going to be sustainable in the long term.

(and yes, I should have separate AWS accounts but I don't... ok :) )

serenitus avatar Jul 29 '21 15:07 serenitus

The workaround mentioned may look like this.

new aws.cloudwatch.EventRuleEventSubscription('eventRuleEventSubscription', eventRule, lambda, {}, {
  transformations: [
    (x) => {
      if (x.type === 'aws:cloudwatch/eventTarget:EventTarget') {
        x.props.eventBusName = eventBusName;
      }
      return x;
    }],
});

and it worked well. Thanks @leezen. However, I think that the current behavior is cleary a bug and I hope this will be fixed soon.

bglgwyng avatar Oct 15 '22 06:10 bglgwyng