S3 Bucket with Terraform + Uploading Files with ActiveStorage
By: Jorge Junior
Tags: Ruby on Rails, S3, English, AWS, DevOps

Hello guys, how are you doing today? I was studing a little bit of terraform this week and the idea of this post is just share the knowledge that I got

So, how to create an S3 Bucket and upload a File to it with Rails (ActiveStorage)? That was my question when I started my studies. I started reading the Terraform documentation on the AWS S3 Bucket and it didn’t seem too difficult.

Creating the S3 Bucket

First of all, we need create the module of our S3 Bucket. If you don’t understand very well how to configure your Terraform project, please read this post Getting Started with Terraform: Infrastructure as Code.

So I created a module with three files, main.tf, bucket.tf and output.tf. The output and main files are only for inputs and outputs so will only share bellow the bucket.tf file (but the repository will be in the end of this post)

Basically, we will create a private bucket in this case, for dev environment. We are using the bucket_name variable that will be provided by the main.tf file of the project:

You can see all the project and necessary files in my repo:

And finally, you can run this three comands to create the bucket:

terraform init

terraform plan (to show what will be created)

terraform apply

We will have the bucket created after few minutes

Uploading Files with Rails Active Storage

Well to test the bucket, I created a simple Rails application to upload some files. I created a simple scaffold for User with name email nickname with this command:

rails g scaffold User name email nickname

The Avatar information will be added later

After this, I installed the active storage with rails active_storage:install . This command will create the necessary migrations to have active_storage working. Finally, rails db:migrate to migrate User and the Active Storage Tables.

Configuring Amazon as storage in Rails App

First of all, we need update our config/storage.yml with the amazon bucket informations . You need add the informations of the bucket created with terraform before.

To update the credentials, assuming that you are in linux and have nano as editor, run this command on terminal: EDITOR="nano" rails credentials:edit . You will edit the file putting the credentials of your amazon account (secret_id and access_id)

In my case, only for Study purposes, I put the active_storage service as :amazon in development, but you just do this for production. To configure the active_storage service go to config/environments/<enviroment>.rb and search for:

# Store uploaded files on the local file system (see config/storage.yml for options). config.active_storage.service = :amazon

Final Steps

Now, we just need add to our user model what we are attaching and add the file field in form:

We need add the :avatar field in the params white list, in the user Controller:

def user_params params.require(:user).permit(:name, :email, :nickname, :avatar) end

And this is all we need do! Everything should be working and you can upload files directly to Amazon S3. If you wanna see the file url, you should use the service_url method, like user.avatar.service_url

That’s all for today guys. I hope you enjoyed!