author image

Ravi Lingineni

Published Jun 19, 2021

Free Form submissions with CloudWatch Logs

Using cloudwatch log analytics to do more

In 2019, Amazon introduced Cloudwatch Log Insights. Cloudwatch is typically used to log and monitor information for different backend services. So naturally, it’s a replacement for tools like Datadog or Splunk: log analysis in the AWS console.

However, you can also run with it for product analytics and simple dashboards. I found that log insights was a low cost way to:

  1. Handle Form Submissions (like Formspree)
  2. Perform Product Analytics (like Amplitude)

The best thing is that you can visualize all of the information you get in neat graphs.

If you don’t want to keep up with one extra tool and are happy with keeping things in AWS, Cloudwatch logs is a nifty tool for product analytics.

How it works

If you don’t know much about setting up Lambda functions in AWS or deploying APIs, everything I mention below might be a blur. This tutorial can help you get up and running with a serverless API.

But if you have used Lambda with a console.log() statement or a print function, you’re pretty much good to go.

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. Log 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. It makes them queryable either with Log InsightsQL or SQL. No tables, no schemas. All you need to do is print a JSON object.

For example, if your log output looked like this:

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

We can write a query in log insights to group all the visit-landing-page events by user id.

fields event, userid |
filter event == 'visit-landing-page' |
stats count() by userid

You can pin this query to a dashboard. Have it update as live bar graphs, line graphs or flat tables.

Cloudwatch dashboards can also be shared with non-engineers. They can easily be shared via email or locked behind SSO.

Handling Form Submissions

To handle form submissions, I follow a similar process. Instead I make a POST endpoint called /submit. This works with a typical client-side 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. To get all of the responses, I would have to query the form-submission events.

For most of my solo and indie projects, I was happy that Cloudwatch fit my needs. I didn’t need to jump to yet another service.