Format Lambda invocation input from EventBridge using AWS CDK

Amazon EventBridge can be customized to transform input parameters using an “input transformer.” The transformer allows the input parameter to be defined the same way as a direct invocation or API Gateway input. To define the input transformer, the AWS CDK can be used with the appropriate code. The input parameter format can be defined as text using RuleTargetInput.fromText instead. The input transformer is useful for obtaining the input parameter the way you want it instead of the default parameter provided by EventBridge.

広告ここから
広告ここまで

目次

    By default, Amazon EventBridge will wrap the event parameter like this.

    {
       .../* EventBridge execution parameters */
       detail: {
        /* input payload */
       }
    }

    But sometime we want to get the input parameter same as dirctory invocation or API Gateway input.

    {
      /* input payload */
    }

    Define input transformer by using AWS CDK

    We can customize the input parameter using “input transformer“.

    And we can define it by using AWS CDK

    import { EventBus, Rule, RuleTargetInput, EventField } from '@aws-cdk/aws-events';
    import {
    LambdaFunction,
    } from '@aws-cdk/aws-events-targets'
    
        const bus = new EventBus(this, 'EventBus', {
          eventBusName: 'ExampleBusName1'
        })
        const eventRule = new Rule(this, 'EventRule', {
          description: 'example event rule',
          enabled: true,
          eventBus: bus,
          eventPattern: {
            detailType: [
              "customer.created",
              "customer.updated"
            ],
            source: [
              "Stripe"
            ]
          },
          ruleName: 'ExampleRule1',
          targets: [
            new LambdaFunction(Lambda, {
              event: RuleTargetInput.fromObject({
                name: EventField.fromPath('$.detail.name'),
                detail: EventField.fromPath('$.detail.detail')
              })
            })
          ]
        })

    If you want to put as a text, we can use RuleTargetInput.fromText instead.

    広告ここから
    広告ここまで

    Random posts

    Home
    Search
    Bookmark