2.1.0 • Published 7 months ago

serverless-offline-aws-eventbridge v2.1.0

Weekly downloads
1,567
License
MIT
Repository
github
Last release
7 months ago

serverless-offline-aws-eventbridge

A serverless offline plugin that enables aws eventBridge events

serverless npm version License: MIT

Docs

Installation

Install the plugin

npm install serverless-offline-aws-eventbridge --save

Let serverless know about the plugin, also note the order when combined with serverless webpack and offline

plugins:
  - serverless-webpack
  - serverless-offline
  - serverless-offline-aws-eventbridge

Configuring the plugin

custom:
  serverless-offline-aws-eventbridge:
    port: 4010 # port to run the eventbridge mock server on
    debug: false # flag to show debug messages
    account: '' # account id that gets passed to the event
    convertEntry: false # flag to convert entry to match cloudwatch

Publishing and subscribing

Checkout the documentation for AWS eventbridge in serverless framework and the AWS SDK for publishing and subscribing to events.

Scheduled events are also partially supported in version 1.3.0@beta (only cron, rate events are coming soon). The cron job must be provided fully as shown in the example below. When a cron fires the event object that is sent along is an empty object.

A simple example configuration in serverless with a Lambda function that publishes an event and a Lambda that subscribes to the event.

functions:
  publishEvent:
    handler: events.publish
    events:
      - http:
          path: publish
          method: get

  consumeEvent:
    handler: events.consume
    events:
      - eventBridge:
          eventBus: marketing
          pattern:
            source:
              - acme.newsletter.campaign

  scheduledEvent:
    handler: events.scheduled
    events:
      - eventBridge:
          eventBus: marketing
          # run every 5 minutes
          schedule: "cron(0/5 * * * ? *)"

The events handler with two functions (publish and consume)

  import AWS from 'aws-sdk';

  export const publish = async () => {
    try {
      const eventBridge = new AWS.EventBridge({
        endpoint: 'http://127.0.0.1:4010',
        accessKeyId: "YOURKEY",
        secretAccessKey: "YOURSECRET",
        region: "eu-west-1"
      });

      await eventBridge.putEvents({
        Entries: [
          {
            EventBusName: 'marketing',
            Source: 'acme.newsletter.campaign',
            DetailType: 'UserSignUp',
            Detail: `{ "E-Mail": "some@someemail.some" }`,
          },
        ]
      }).promise();
      return { statusCode: 200, body: 'published' };
    } catch (e) {
      console.error(e);
      return { statusCode: 400, body: 'could not publish' };
    }
  }

  export const consume = async (event, context) => {
    console.log(event);
    /*
      {
        EventBusName: 'marketing',
        Source: 'acme.newsletter.campaign',
        DetailType: 'UserSignUp',
        Detail: `{ "E-Mail": "some@someemail.some" }`,
      }

      * If 'convertEntry' flag is true, out output will be
      {
        version: "0",
        id: "xxxxxxxx-xxxx-xxxx-xxxx-1234443234563",
        source: "acme.newsletter.campaign",
        account: "",
        time: "2020-06-19T16:37:00Z",
        region: "us-east-1",
        resources: [],
        detail: {
          { 
            "E-Mail": "some@someemail.some" 
          }
        }
      }
    */
    return { statusCode: 200, body: JSON.stringify(event) };
  }

  export const scheduled = async (event, context) => {
    console.log('scheduled event');
    return { statusCode: 200, body: 'scheduled event' };
  }

Versions

This plugin was created using node 12.16.1 and serverless framework core 1.67.0.

Thanks

This plugin was inspired by the serverless-offline-sns plugin

2.2.0-beta.0

7 months ago

2.2.0-beta.1

7 months ago

2.1.0

11 months ago

2.0.5

1 year ago

2.1.0-beta.0

1 year ago

2.0.4

1 year ago

2.0.4-beta.1

1 year ago

2.0.4-beta.0

1 year ago

2.0.3

2 years ago

2.0.2

2 years ago

2.0.1

2 years ago

2.0.0

2 years ago

2.0.0-beta.0

2 years ago

1.6.7

2 years ago

1.6.5-beta.0

2 years ago

1.6.6

2 years ago

1.6.5

2 years ago

1.6.4

3 years ago

1.6.4-beta.1

3 years ago

1.6.4-beta.0

3 years ago

1.6.3

3 years ago

1.6.2

3 years ago

1.6.1

3 years ago

1.6.2-beta.1

3 years ago

1.6.1-beta.2

3 years ago

1.6.1-beta.1

3 years ago

1.6.0

3 years ago

1.6.0-beta.0

3 years ago

1.6.0-beta.1

3 years ago

1.5.2

3 years ago

1.5.1

3 years ago

1.5.0

3 years ago

1.4.2

3 years ago

1.4.1

3 years ago

1.4.0

3 years ago

1.4.0-beta.5

3 years ago

1.4.0-beta.3

3 years ago

1.4.0-beta.2

3 years ago

1.4.0-beta.1

3 years ago

1.4.0-beta.0

3 years ago

1.3.5

4 years ago

1.3.4

4 years ago

1.3.3

4 years ago

1.3.2

4 years ago

1.3.1

4 years ago

1.3.0

4 years ago

1.2.2

4 years ago

1.2.1

4 years ago

1.2.0

4 years ago

1.1.0

4 years ago

1.0.0

4 years ago

0.0.5

4 years ago

0.0.4

4 years ago

0.0.6

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago