Home > Enterprise >  Should rule details be passed to Stepfunctions?
Should rule details be passed to Stepfunctions?

Time:01-16

I'm wondering if something is possible at all, or I'm trying to build something that is not possible from the start.

When an Event Bridge Rule with added event pattern, like below, triggers a step function. Should the information in detail be passed to the step input?

const rule = new Rule(this, 'Rule', {
  schedule: Schedule.expression('cron(0 18 ? * SUN-WED *)'),
}

rule.addEventPattern({
  detail: {
    example: [
      'hello-world',
    ],
  },
});

rule.addTarget(new SfnStateMachine(stateMachine));

Currently, the step input only shows, if possible what may I be missing?

{
  "version": "0",
  "id": "590c8f79-8bb5-d50b-30f7-1234567890",
  "detail-type": "Scheduled Event",
  "source": "aws.events",
  "account": "1234567890",
  "time": "2022-01-14T19:33:47Z",
  "region": "eu-west-1",
  "resources": [
    "arn:aws:events:eu-west-1:1234567890:rule/Example-Rule4C995B7F-UJ68BG8LJK54"
  ],
  "detail": {}
}

Update; Thanks to Fedonev managed to get it to work as followed;

rule.addTarget(new SfnStateMachine(stateMachine, {
  input: RuleTargetInput.fromObject({
    'version': 'custom',
    'id': EventField.fromPath('$.id'),
    'detail-type': EventField.fromPath('$.detail-type'),
    'source': EventField.fromPath('$.source'),
    'account': EventField.fromPath('$.account'),
    'time': EventField.fromPath('$.time'),
    'region': EventField.fromPath('$.region'),
    'resources': EventField.fromPath('$.resources'),
    'detail': {
      example: [
        'hello-world',
      ],
    },
  }),
}));

CodePudding user response:

The rule.addEventPattern method filters events. Instead, to add arbitrary data to scheduled event payloads, use the target's input:RuleTargetInput prop:

rule.addTarget(
  new targets.SfnStateMachine(stateMachine, {
    input: events.RuleTargetInput.fromObject({ example: ['hello-world'] }),
  })
);

Your Step Function executions will receive only the input you set, instead of the default event payload shown in the OP:

// Step Function input
{
  "example": [
    "hello-world"
  ]
}

If you also need fields from the default event payload, you can include them in your input by JSONPath reference with events.EventField.

  •  Tags:  
  • Related