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 = Pathname.new('/tmp/rds-log-full')
filename_contains = 'audit/'
SERVICE = 'rds'
URL_BASE = "https://#{SERVICE}.#{region}.amazonaws.com/v13/downloadCompleteLogFile"
credential = Aws::SharedCredentials.new
signer = Aws::Sigv4::Signer.new(
service: SERVICE,
region: region,
credentials_provider: credential,
)
rds = Aws::RDS::Client.new(region: region)
files = rds.describe_db_log_files(
db_instance_identifier: instance_id,
filename_contains: filename_contains,
)
out_dir.mkpath
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 = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
header = signature.headers
http.get(uri.path, header) do |body|
f.write(body)
end
end
puts "Downloading #{file_name} finished"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment