In BlogPost, Elixir

Storing JSON Blobs in Amazon S3 With Elixir

The idea for this project of using S3 with Elixir came from an actual project and a blog post I stumbled on.

I was working on a Ruby on Rails application with millions of rows of data quite a while ago. We would often pull reports from this data and since it was on the Heroku platform, we would often see these warning messages in our slack monitoring channel about memory consumption being over 100%.

We were worried that we wouldn’t be able to sustain this approach anymore, and so we wanted to start archiving the data that didn’t need to be used in real time anymore. I found this great post on the moz blog about how they essentially used a cold storage solution in Amazon S3 buckets (and here, I assume they mean they used something similar to a nested json data file based on their description).

JSON Blob Experiment

This led me to start experimenting with storing json files (let’s call them “json blobs”) on Amazon S3 with Elixir. This followed the approach from Moz described in the aforementioned blog post as well as allowing me to play with Elixir.

Here is a 3 step approach I used to have an Elixir project store json blobs on S3.

Note: At the time I created this, Phoenix 1.2 was the current version. I’m updating the commands to use phoenix 1.3, although the structure of the code may resemble Phoenix 1.2.

Step 1: Create Mix Project with ex_aws

First, I generated a new Phoenix project.

In the mix.exs file, I also added the ex_aws package as a dependency for storing files in Amazon S3 as shown below.

Step 2: Module to Generate the JSON records

The next step was to create a JsonGenerator module. This module simply generated the “json blobs” I wanted to store on S3 as shown below.

Step 3: Module to Write to AWS

To hew to the “separation of concerns” principle, I created another module specifically to write to AWS. Using Elixir’s import statement, I imported the JsonGenerator module to use the json blob generating functionality I created in Step 2.

The API for the JsonAws module is simple. The write_records method writes a user-specified number of json blobs up to S3 using the configured S3 bucket. The read_file method then reads the file that stores the json blobs.

Step 4: Set Up Your AWS Keys

One of the last few steps is to configure ex_aws. You can simply copy the config.exs file below.

In config.exs:

To make this all work, you’ll have to setup your AWS keys in your Amazon account and then put those values in a .env file in the top level of your application.

Set up a .env file as follows:

You get these values from the Amazon AWS management console. You may have to sign up for an account if you don’t have one.

Step 5: Run Your New Program

Finally, boot up an iex session as follows and run write_records method specifying the number of json records you want to create and store in S3.


Boom! And that’s all there is to storing simple json data on S3. It’s a good way to get your feet in Elixir.

Recent Posts