I am trying to deploy my rails application with ruby version 3.2.2 and rails 7.1 and bundler 2.5.23 on AWS elastic beanstalk, and I am able to deploy it successfully. I can access default route as expected, but when I try to hit other APIs like login that includes DB query I got db connection error, I have my db hosted on AWS RDS, in my app/config/database.yml, I am providing all things like host, db name, password and username, but unable to connect and get this error:
{
"error": "Database connection error. Please check your database configuration.",
"details": "We could not find your database: my_db. Available database configurations can be found in config/database.yml file.\n\nTo resolve this error:\n\n- Did you create the database for this app, or delete it? You may need to create your database.\n- Has the database name changed? Check your database.yml config has the correct database name.\n\nTo create your database, run:\n\n bin/rails db:create\n",
although when I try to connect manually in ec2 I am able to connect and view my db it's available with same name and I am providing exact creds: here is my database.yml file
development:
adapter: postgresql
encoding: utf8
database: <%= ENV['database_name'] %> #musea_staging
pool: 5
username: <%= ENV['database_username'] %>
password: <%= ENV['database_password'] %>
host: <%= ENV['database_url'] %>
port: <%= ENV['database_port'] %>
reaping_frequency: 10
staging:
adapter: postgresql
encoding: utf8
database: musea_staging
pool: <%= ENV['database_pool'] %>
username: <%= ENV['database_username'] %>
password: <%= ENV['database_password'] %>
host: <%= ENV['database_url'] %>
port: <%= ENV['database_port'] %>
reaping_frequency: 10
beta:
adapter: postgresql
database: musea_beta
encoding: utf8
username: <%= ENV['database_username'] %>
port: <%= ENV['database_port'] %>
password: <%= ENV['database_password'] %>
host: <%= ENV['database_url'] %>
pool: <%= ENV['database_pool'] %>
reaping_frequency: 10
production:
adapter: postgresql
database: musea
encoding: utf8
username: <%= ENV['database_username'] %>
port: <%= ENV['database_port'] %>
password: <%= ENV['database_password'] %>
host: <%= ENV['database_url'] %>
pool: <%= ENV['database_pool'] %>
reaping_frequency: 10
I have also tried hardcoding values and my elasticbean config are also correct and here is my code to deploy
require 'aws-sdk'
require 'fileutils'
require 'yaml'
def current_branch
`git rev-parse --abbrev-ref HEAD`.chomp
end
def current_sha
`git rev-parse --short HEAD`.chomp
end
def archive_path(name)
File.join(File.expand_path(File.dirname(__FILE__)), "#{name}-#{current_sha}.zip")
end
def export_archive(archive_path, branch)
`git archive --format zip --output #{archive_path} #{branch}`
end
def upload_archive(key, data_path)
Aws.config.update({
region: 'us-east-1',
credentials: Aws::Credentials.new(
ENV['deploy_aws_key'],
ENV['deploy_aws_secret'])})
s3_uploader = Aws::S3::FileUploader.new
bucket_name = ENV['app_backup_bucket']
retry_on_failure do
s3_uploader.upload(data_path ,{bucket: bucket_name, key: key} )
#s3.put_object(
#bucket: bucket_name,
#key: key,
#content_type: "application/zip",
#body: open(data_path)
#)
end
end
def retry_on_failure
3.times do
begin
return yield
rescue => e
puts "#{e}. retrying operation"
end
end
fail "3 attempts failed !!!"
end
def create_application_version(beanstalk, config, data_key)
version_label = File.basename(data_key, '.zip')
unless has_application_version?(beanstalk, config['application_name'], version_label)
bucket_name = ENV['app_backup_bucket']
beanstalk.create_application_version(
application_name: config['application_name'],
version_label: version_label,
description: "Created by deployment script.",
source_bundle: {
s3_bucket: bucket_name,
s3_key: data_key,
},
auto_create_application: false,
)
end
end
def has_application_version?(beanstalk, application_name, version_label)
response = beanstalk.client.describe_application_versions(
application_name: application_name
)
response.data[:application_versions].any? {|version| version[:version_label] == version_label}
end
def update_environment(beanstalk, config, data_key)
beanstalk.client.update_environment(
environment_name: config['environment_name'],
version_label: File.basename(data_key, '.zip'),
)
end
def deploy_application(config, branch)
data_path = archive_path(config['application_name'])
data_key = "#{config['application_name']}/#{File.basename(data_path)}"
export_archive(data_path, branch)
upload_archive(data_key, data_path)
Aws.config.update({
region: 'us-east-1',
credentials: Aws::Credentials.new(
ENV['deploy_aws_key'],
ENV['deploy_aws_secret'])})
beanstalk = Aws::ElasticBeanstalk::Client.new(region: 'us-east-1')
create_application_version(beanstalk, config, data_key)
update_environment(beanstalk, config, data_key)
cleanup_local_archive(data_path)
end
def cleanup_local_archive(archive_file)
FileUtils.rm_f(archive_file)
puts "Cleaned up local archive"
end
def get_configuration
yaml = File.join(File.dirname(File.expand_path(__FILE__)),'/beanstalk.yml')
config = YAML.load_file(yaml)
end
namespace :beanstalk do
desc 'Deploy application to Elastic Beanstalk'
task :deploy => :environment do
branch_config = get_configuration
target = current_branch
puts "Deploying branch #{target}."
deploy_application(branch_config[target], target)
puts "Deployed #{current_sha}."
end
desc 'Create archive without uploading'
task :dry_run => :environment do
branch_config = get_configuration
target = current_branch
puts "Creating archive for branch #{target}."
dry_run(branch_config[target], target)
end
end
I want to connect with AWS RDS but got db not found error, although it exists there and I can connect with it