CODEX

ActiveStorage to Cloud-based Storage

Persist Heroku App Data with AWS

Lauren Cunningham
Feb 26 · 4 min read
Photo by Magda Ehlers from Pexels

Here Today, Gone Tomorrow

The problem is that Heroku’s drive is ‘ephemeral’ — meaning you can write files to it, but they will not be persisted when the app is re-started. If you’re using Ruby on Rails’ Active Storage, the information will be accessible through local storage temporarily but will disappear after 24 hours.

So, what now?

Enter Cloud-based Solutions

S3 is built for storing objects with a focus on security and scalability. It’s also free to use with some limitations. Storing data for a small application will likely not cost a dime.

The way that it works is by uploading your data in a ‘bucket’. You can have multiple buckets if you need to keep some data separated. Once you have that setup, you’ll need to make some small changes to your application to hook it up.

Initial Set-up

Pro-tip: Defining a dynamic API variable that allows you to switch from production and development URLs can save you time and possible confusion when testing your local storage vs. S3 storage. For example, if you’re using a React front-end you can create a file labeled API.js and add the following:

const PROD_URL = 'https://your-app.herokuapp.com'const DEV_URL = 'https://localhost:3000'const API = process.env.NODE_ENV === 'development' ? DEV_URL : PROD_URL;export default API;

Getting Started on AWS

After you click on the orange ‘Create Bucket’ button, enter a name for your bucket that is fitting for your application and/or the data you plan on storing there. Pay close attention to the AWS Region you have chosen. You’ll need it later along with your secret access key and secret access id. You can leave the other settings alone for now.

Link AWS S3 Data to Application

To keep your data secure, you’ll need to set up a hidden variable containing your AWS secrets. In case you didn’t save the secret key and id from when you created your bucket, you can create a new access key to use by logging into your AWS account, clicking on your username on the top right, and selecting My Security Credentials.

Once you have that, visit your config/storage.yml file. You’ll find a hint here for your next step.

#Use rails credentials:edit to set the AWS secrets (as aws:access_key_id|secret_access_key)

Follow those instructions first, then include this code below it:

amazon:service: S3access_key_id: <%= Rails.application.credentials.dig(:aws, :access_key_id) %>secret_access_key: <%= Rails.application.credentials.dig(:aws, :secret_access_key) %>region: enter-region-herebucket: 'enter-bucket-name-here'

Now make sure config/environments/production.rb includes:

config.active_storage.service = :amazon

In your gemfile you’ll need to add:

gem 'aws-sdk-s3', require: false

Don’t forget to bundle install after!

Possible Hang-ups

Another important thing to note is that you may not be able to use Rails’ :includes method. If this gives you problems, simply use a serializer instead. This blog is a great resource for setting up serializers, in case you haven’t tried it. It’s a game-changer.

Final Tip: Be Patient

CodeX

Everything connected with Tech & Code

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store