Home > database >  AWS CDK Events Add Target with Input Template
AWS CDK Events Add Target with Input Template

Time:01-19

I am creating an event in AWS that when a file is dropped into S3, this rule is triggered and starts a step function (SfnStateMachine)

I have some cdk code (node.js) where I add the SfnStateMachine as a target of that rule.

I want to take the payload of the S3 event and use inputPathsMap to map to a readable format, then I can use the inputTemplate to set the payload that will be sent to the Step Function.

 const inputTemplate = {
    Payload: {
      TriggerType: "<detailType>",
      TriggerReason: "<reason>",
      Version: "<versionId>"
    },
  }


rule.addTarget(new events_targets.SfnStateMachine(mystateMachine.stateMachine, {
  input: events.RuleTargetInput.fromObject({ 
    inputPathsMap: {
      detailType: "$.detail-type", 
      reason:"$.detail.reason",
      versionId:"$.detail.object.version-id"
    },
    inputTemplate: JSON.stringify(inputTemplate) }),
  role,
}))

When I use this, it is literally sending the value within withObject

I have looked at the docs and the RuleTargetInput.bind() seems like what I want to use, but I can't see an example of how its used. https://docs.aws.amazon.com/cdk/api/v1/docs/@aws-cdk_aws-events.RuleTargetInput.html

I wanted to do something like

rule.addTarget(new events_targets.SfnStateMachine(ddStateMachine.stateMachine, {
  input: events.RuleTargetInput.bind(),
  role,
}))

but I don't know where to set the inputTemplate or the inputPathsMap because they are the return type rather than the input.

CodePudding user response:

Use the EventField helpers to define input, which defines the Step Function payload:

rule.addTarget(
  new events_targets.SfnStateMachine(ddStateMachine.stateMachine, {
    input: events.RuleTargetInput.fromObject({
      detailType: events.EventField.detailType,
      reason: events.EventField.fromPath("$.detail.reason"),
      versionId: events.EventField.fromPath("$.detail.object.version-id"),
    }),
    role,
  })
);

Building the InputPathMap and InputTemplate is a job for the CDK. The event target construct's synthesized output in the Rule > Targets resource definition is:

"InputTransformer": {
  "InputPathsMap": {
    "detail-type": "$.detail-type",
    "detail-reason": "$.detail.reason",
    "detail-object-version-id": "$.detail.object.version-id"
  },
  "InputTemplate": "{\"detailType\":<detail-type>,\"reason\":<detail-reason>,\"versionId\":<detail-object-version-id>"
  • Related