How to use Databases for Elasticsearch to index and search your VPC Flow Logs.
You may have set up VPC Flow Logs for your entire VPC to write to a Cloud Object Storage (COS) bucket. I did it a few weeks ago for a VPC that I use mainly for short-term projects and with no real-world production workload. Recently, I was looking for a method to search through the data stored in the Flow Logs COS bucket that had been accumulating for over one month. I knew of this excellent post and associated Python code repository from my colleague Powell Quiring that walk you through ingesting the Flow Logs objects into IBM Log Analysis with LogDNA.
In addition to searching through this data, I envisioned integrating the search results with an ongoing pet development project of mine. In that other project, the environment is built and destroyed frequently, and I needed a fast method to bring back the data from the COS bucket. The Python sample referenced above is geared mostly to handle objects as they are added to the bucket, not for weeks worth of Flow Logs objects. For my use case, I determined that having a tool written in the same language I was using in my project — Golang — and indexing in IBM Cloud Databases for Elasticsearch was going to be the solution.
In this post, I will share my configuration and a sample of the code I am using to index VPC Flow Logs to Elasticsearch.
As mentioned earlier, I started with an existing VPC and already had a Flow Logs collector configured to a COS bucket. If you are new to VPC or VPC Flow Logs, you can read through Powell's post and code sample — he provides the steps and scripts to generate VPC resources and configure Flow Logs. You can also check out our more extensive tutorials on VPC.
- A Flow Logs collector is configured for the VPC.
- The collector interfaces with IBM Cloud Object Storage and writes to the "flowlogs" bucket.
- A Databases for Elasticsearch is provisioned to be used for indexing and searching of the Flow Logs.
- A second COS bucket "indexed-flowlogs" is created to store objects that have already been indexed in Elasticsearch.
- The vpc-flowlogs-elasticsearch tool is configured to read the objects from the flowlogs bucket...
- ..., index to Elasticsearch ...
- ... and write the indexed objects to the indexed-flowlogs bucket and deleted them from the flowlogs bucket.
- Once indexed, the tool, Postman, or any other application can be used to query Elasticsearch.
Downloading, configuring, and running the tool
The tool demonstrates how to do the following:
- Read/write to IBM Cloud Object Storage using the IBM Cloud Object Storage Go SDK
- Index/search Databases for Elasticsearch using the official Go client for Elasticsearch
The source code is available on GitHub, and you can copy and tailor it to your needs. The README.md in the repository will guide you on how to create a Databases for Elasticsearch instance, clone the repository, configure, and run the tool.
Once configured to interact with your COS and Elasticsearch instances, indexing is a simple command:
Searching can be performed straight from the tool using some pre-configured queries (are you one of the IPs trying to hack my server?):
You can also use Postman or another client; some example Elasticsearch queries are also made available in the repository.
Questions and feedback
The GitHub repository has an Issues tab where you can comment on the content and code. If you have suggestions or issues, please submit your feedback.