Wednesday, June 6, 2018

[AWS RDS] Sending an email when there's a new version of RDS available

Something I needed recently.  Difficulty level: Get-RDSPendingMaintenanceAction ONLY returns info when there's something to be done.  If you're current, there's no way to find out what it looks like.

This will email you a HTML-formatted table with the details of each pending maintenance action for every instance in a region.


Remove-Variable -Name * -ErrorAction SilentlyContinue
import-module awspowershell
$myaccesskey = "youraccesskey"
$mysecretkey = "yoursecretkey"
$myregion = "yourregion"
$myemail = "youremailaddress"
$mysmtp = "smtp.yourcompany.com"
set-awscredentials -accesskey $myaccesskey -secretkey $mysecretkey
$now = get-date
$upgrade_info = Get-RDSPendingMaintenanceAction -Region $myregion
# Taking the results and parsing out the info for each instance
$Parsed_Results = $upgrade_info | select @{Label='ResourceIdentifier';Expression={$_.ResourceIdentifier}
}, @{Label='Action';Expression={$_.PendingMaintenanceActionDetails.Action}
}, @{Label='Description';Expression={$_.PendingMaintenanceActionDetails.Description}
}, @{Label='AutoAppliedAfterDate';Expression={$_.PendingMaintenanceActionDetails.AutoAppliedAfterDate}
}, @{Label='CurrentApplyDate';Expression={$_.PendingMaintenanceActionDetails.CurrentApplyDate}
}, @{Label='ForcedApplyDate';Expression={$_.PendingMaintenanceActionDetails.ForcedApplyDate}
}, @{Label='OptInStatus';Expression={$_.PendingMaintenanceActionDetails.OptInStatus}
}
# Making an HTML table
# https://jamesdatatechq.wordpress.com/2014/12/23/how-to-create-an-html-table-from-powershell/
$Style = "
<style>
TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}
TH{border-width: 1px;padding: 3px;border-style: solid;border-color: black;}
TD{border-width: 1px;padding: 3px;border-style: solid;border-color: black;}
</style>
"
#Convert the parsed info to a table
$error_table = $Parsed_Results| ConvertTo-HTML -AS Table -Fragment -PreContent "<h2>Aurora Updates as of $now</h2>"|Out-String
#Save the HTML Web Page with the table style
[string]$email_table = (ConvertTo-HTML -head $Style -PostContent $error_table)
# Now send the mail
if ($upgrade_info){
#Send email to $myemail. SMTP is a CNAME to the corporate SMTP server we use.
Send-MailMessage -smtpserver $mysmtp `
-To $myemail `
-From "My_AWS_Account_Do_Not_Reply <Dev@Null.Com>" `
-Subject "[AWS] Aurora updates available - time to test and mark for upgrades if good. $now" `
-body $email_table -BodyAsHtml
}

No comments: