Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
RDS log downloader sample
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'aws-sdk'
require 'aws-sigv4'
require 'pathname'
instance_id = ARGV[0]
region = 'ap-northeast-1'
out_dir ='/tmp/rds-log-full')
filename_contains = 'audit/'
SERVICE = 'rds'
URL_BASE = "https://#{SERVICE}.#{region}"
credential =
signer =
service: SERVICE,
region: region,
credentials_provider: credential,
rds = region)
files = rds.describe_db_log_files(
db_instance_identifier: instance_id,
filename_contains: filename_contains,
files.describe_db_log_files.each do |file|
name = file.log_file_name
file_name = name.gsub(%r{.+/}, '')
out_file = out_dir / file_name
puts "Downloading #{file_name} to #{out_file}"
log_api_url = "#{URL_BASE}/#{instance_id}/#{name}"
signature = signer.sign_request(
http_method: 'GET',
url: log_api_url,
uri = URI.parse(log_api_url)
open(out_file.to_s, 'w+') do |f|
http =, uri.port)
http.use_ssl = true
header = signature.headers
http.get(uri.path, header) do |body|
puts "Downloading #{file_name} finished"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment