Skip to content

Instantly share code, notes, and snippets.

@duboisf
Created February 20, 2024 20:43
Show Gist options
  • Save duboisf/2c545cff70b316e0fee882ca3ca160a7 to your computer and use it in GitHub Desktop.
Save duboisf/2c545cff70b316e0fee882ca3ca160a7 to your computer and use it in GitHub Desktop.
aws utils written in nu
export def --wrapped aws [...rest]: nothing -> list<any> {
if ($rest | last) == "help" {
^aws ...$rest
} else {
^aws --output json ...$rest | from json | get-first
}
}
export def "get-first" []: record -> any {
let input = $in
try {
let cellPath = $input | describe | parse --regex 'record\<(.+?):' | get capture0.0
$input | get $cellPath
} catch {
$input
}
}
export def "rds logs list" [
instance: string, # the RDS instance to list logs for
oldest?: duration, # return only the logs that were last written to before this time
]: nothing -> list<string> {
let logFiles = (aws rds describe-db-log-files --db-instance-identifier $instance
| update LastWritten { $in * 1_000_000 | into datetime }
| where Size > 0)
(if $oldest != null {
$logFiles | where LastWritten > ((date now) - $oldest)
} else {
$logFiles
}) | get LogFileName
}
export def "rds logs get" [
instance: string, # the RDS instance to list logs for
oldest?: duration, # return only the logs that were last written to before this time
]: nothing -> nothing {
rds logs list $instance $oldest
| par-each { |filename|
let cleanFilename = $filename | str replace "/" "_"
print $"Downloading ($filename)"
aws rds download-db-log-file-portion --db-instance-identifier $instance --log-file-name $filename --starting-token 0
| save $"($instance)_($cleanFilename)"
}
print "Done!"
}
export def "rds instances list" []: nothing -> list<string> {
aws rds describe-db-instances | get DBInstanceIdentifier
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment