Search code examples
ruby-on-railsamazon-web-servicesamazon-s3rails-activestorage

Configuring ActiveStorage to use S3 via ECS Credentials


I am trying to configure ActiveStorage to use ECS Credentials and can't find any documentation on how to configure it.

Right now I have:

staging:
  service: S3
  region: "MYREGION"
  bucket: "MYBUCKETNAME"
  credentials: Aws::ESCCredentials

But I don't think this is correct?

I am trying to follow the documentation on the ActiveStorage site: https://edgeguides.rubyonrails.org/active_storage_overview.html#s3-service-amazon-s3-and-s3-compatible-apis which leads to https://docs.aws.amazon.com/sdk-for-ruby/v3/api/Aws/S3/Client.html#initialize-instance_method . I just don't know how to set the right value here in the credentials field is.


Solution

  • What worked for me:

    In storage.yml:

    staging:
      service: S3
      region: "MYREGION"
      bucket: "MYBUCKETNAME"
    

    Then, create an extra file in config/initializers named aws.rb with the following content:

    require 'aws-sdk-core'
    
    if Rails.env === 'staging' || Rails.env === 'production'
      Aws.config.update(credentials: Aws::ECSCredentials.new)
    end
    

    Of course, the ECS container itself must be setup to include the necessary environment variables (these two resources helped me with that: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-ecs-taskdefinition.html and Deploying dockerised web-app on AWS with Fargate with terraform)

    Hope that this helps others trying to do similar.