author image

Ravi Lingineni

Published Jun 19, 2021

Free form submissions for everyone

Using cloudwatch log analytics to do more

In 2019, Amazon introduced Cloudwatch Log Insights. For most people, this is like a datadog or splunk replacement: free text log analysis without leaving the AWS console.

For me, it was way more than that. Log insights is a low cost way to:

  1. Handle Form Submissions (replaces formspree)
  2. Perform Product Analytics (replaces Amplitude)

Though far from feature rich and perfect, it fit right into my existing workflow, and it was enough to avoid paying for services that I didn’t need.

Prequisites

How it works

The general gist is logging output from an AWS Lambda function into your logs, and then analyzing those logs to get the responses.

To get started, you need to up a serverless API. I use AWS Lambda paired with API Gateway to set things up.

Logging to Cloudwatch

By default, any console.log statements from a Lambda function will log their output to AWS Cloudwatch.

To add product analytics to your client, you can create a route called /log to your api and log out the response as a JSON object.

router.post('/log'(ctx)=>{
    // request body as JSON
    const body = ctx.req.body;
    console.log(JSON.stringify(body))
})

As long as you print a JSON object, Log Insights will parse the fields and make them queryable.

For example, if our log output looked like this:

{"event":"visit-landing-page", "userid":"helloravi"}

We can write a [query] in log insights to select all of the fields we want

Using the stats operator, you can get counts for a certain type of event. Depending on your data, you can choose to view information as bar graphs, line graphs or a flat table.

I usually pin all of my queries into a dashboard.

You can also choose to share the dashboard with the team without giving everyone AWS account access.

Handling Form Submissions

To handle form submissions, I follow the exact same process, and add a route called submit to reflect a POST method. It works with the typical HTML <form> tag.

router.post('/submit'(ctx)=>{
    // request body
    const body = ctx.req.body;
    console.log({event:'form-submission', ...body})
})

The responses are logged to cloudwatch, and I can query for all the form-submission events to get values out. You can even run simple analytics and export to a CSV. All I need for now.