Skip to content

M1ke/serverless-webhook-logger

Repository files navigation

Serverless Webhook Logger

There are times where you know your application will receive some webhooks but ahead of time you may not know the range of values or possibilities. Alternatively you might not want to expose a route in your own application but instead to process records from a decoupled source.

This project uses AWS Lambda (via Bref PHP), API Gateway and DynamoDB to provide a simple tool that can log inbound webhooks. You could use this for analysis, or via a stream into another tool to process them.

Existing tools

Pre-install

Copy the terraform.tfvars.example file to terraform.tfvars. You will then want to check the default variables and change them if helpful:

  • region choose your desired AWS region (e.g. eu-west-1)
  • deploy_bucket choose a globally unique bucket name to host your code (e.g. my-company-webhook-logger-abc123)

Note when running all commands below that touch AWS you'll need AWS credentials in your environment. There are a variety of ways to do this; using aws-vault (e.g. aws-vault exec YOUR_PROFILE -- terraform plan) is recommended

Install

You must first create resources via Terraform before deploying the application stack:

terraform init
terraform plan
terraform apply

Type "yes" after the Terraform apply step. Once Terraform runs it will output an "ARN" of a role that the serverless application will need:

webhook-logger-role-arn = arn:aws:iam::XXXX:role/webhook-logger

Copy this "ARN" over the string YOUR_ROLE_HERE in template.yml

Now run bin/deploy which executes a series of AWS SAM commands to upload your code and deploy the function. At the end of the run it will output as follows:

Successfully created/updated stack - webhook-logger
https://xxxxxxx.execute-api.YOUR_REGION_HERE.amazonaws.com/dev

NB if you miss the output run bin/output to see it again without deploying the stack.

If you run a request to that endpoint you should see the result logged:

curl -v -XPOST -H 'Content-type: application/json' -d '{"test":"abc123"}' ' https://xxxxxxx.execute-api.eu-west-1.amazonaws.com/dev'

Go to the DynamoDB console and view the table "webhook-logger" to see logs. If this fails check the Cloudwatch console where a log group like /aws/lambda/webhook-logger should exist (remember to choose the correct region in the top bar of the console) with a stream for each series of invocations.

Modifying

Some config items can be changed easily in config.php

Acknowledgements

The basis of this function came from the Bref documentation for HTTP endpoints

Related

This project uses m1ke/json-explore to create a parsed set of keys found in the JSON sent via webhook. This library has other uses for API exploration, and different methods for rendering the data it analyses about a JSON payload.

About

Serverless webhook logger using AWS Lambda, PHP, and Bref.sh

Resources

License

Stars

Watchers

Forks

Packages

No packages published