Coder Social home page Coder Social logo

serverless-offline-eventbridge's Introduction

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

Note that if you want to use the beta feature for scheduled events install the 1.3.1 beta version from npm.

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 as of 1.3.2 (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": "[email protected]" }`,
          },
        ]
      }).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": "[email protected]" }`,
      }

      * 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": "[email protected]" 
          }
        }
      }
    */
    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

serverless-offline-eventbridge's People

Contributors

aaronnugget avatar bigirishlion avatar rubenkaiser avatar sndpl avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.