Coder Social home page Coder Social logo

webclinic017 / serverless-event-collector Goto Github PK

View Code? Open in Web Editor NEW

This project forked from ethanrosenthal/serverless-event-collector

0.0 0.0 0.0 48 KB

It's what the name says it is.

Home Page: https://www.ethanrosenthal.com/2021/07/20/serverless-event-collector/

License: MIT License

Python 97.17% Makefile 2.83%

serverless-event-collector's Introduction

serverless-event-collector

Docs

This is an example repo for a serverless event collector built on AWS. The example involves collecting two types of events: page views from a website, and "clicks" of a button on a website. The diagram below shows the flow of the event data.

Diagram

Events are POSTed as JSON-serialized data to a public facing API. The API lives in API Gateway and consists of two Lambda functions: the authorizer checks the Basic Authentication info in the request and allows or denies the request. Allowed requests get forwarded to the collector Lambda function which is actually a FastAPI server deployed as a Lambda function. collector validates the request and then drops it into Kinesis Firehose. collector also updates DynamoDB tables which maintain a count of each event type for each user. These tables are used to power endpoints for GETting real time event counts.

Batches of events from Kinesis get dropped into a bucket on S3. Objects created in that bucket trigger the fan_out Lambda function in which events from the main Kinesis bucket get copied into user-specific buckets partitioned by event-type and time.

The entire serverless event collector is deployed together using the serverless framework with full configuration defined in serverless.yml.

Usage

The event collector collects events via HTTP POST requests. See the API docs for documentation. Requests must be authenticated in order to POST to the API. Currently, the authorizer function contains a hardcoded map of usernames and passwords for Basic Authentication. In practice, I would recommend using AWS Secrets Manager. Example Python code for collecting a button_click event is shown below.

import requests
url = "..."
username = "..."
password = "..."
event = {"button_id": "abc", "session_id": "xyz"}
response = requests.post(url, json=event, auth=(username, password))
response.json()
# {"message": "Received"}

Development

All Lambda functions are python-based and defined in their own directories in this repo. Each Lambda function has unique dependencies that are defined in their respective requirements.txt files.

Running make build builds virtual environments in each Lambda directory and runs their respective tests. You can also run make build from within each directory to just build that individual Lambda function.

The entire application can be deployed from the command line using the serverless framework. After installing serverless, run the following from the root directory of this repo to deploy, where $STAGE is dev, staging, or prod

serverless deploy --stage $STAGE

All AWS resources and components should be built during the deployment process with the exception of S3 buckets for each authenticated user. User-specific buckets should be created manually at $USER-raw-events.

Tests

Build lambda functions and run their tests:

make

serverless-event-collector's People

Contributors

ethanrosenthal 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.