Skip to content

Instantly share code, notes, and snippets.

@somazx
Last active February 1, 2017 20:03
Show Gist options
  • Save somazx/362a4cd11f3c736fbde9 to your computer and use it in GitHub Desktop.
Save somazx/362a4cd11f3c736fbde9 to your computer and use it in GitHub Desktop.
Using Amazon Elasticsearch securely (signed requests) with Rails & searchkick gem on Heroku.
# config/initializers/elasticsearch.rb
# ensure you set AWS_ES_URL, AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY
# change region as needed
require 'patron'
require 'faraday_middleware/aws_signers_v4'
# arguably this should be defined elsewhere
class AmazonElasticSearchClient
def self.client
return Elasticsearch::Client.new(url: ENV['AWS_ES_URL']) do |f|
f.request :aws_signers_v4,
credentials: Aws::Credentials.new(ENV['AWS_ACCESS_KEY_ID'], ENV['AWS_SECRET_ACCESS_KEY']),
service_name: 'es',
region: 'us-west-2'
end
end
end
# for local development I run a local elasticsearch,
# so I only override the Searchkick.client in production environments
Searchkick.client = AmazonElasticSearchClient.client if Rails.env.production?
gem 'elasticsearch', '>= 1.0.15'
gem 'elasticsearch-model'
gem 'elasticsearch-rails'
gem 'patron'
gem 'faraday_middleware-aws-signers-v4'
gem 'searchkick'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment