diff --git a/ps-slack-notifier/PSAwsEventToSlack.ps1 b/ps-slack-notifier/PSAwsEventToSlack.ps1 new file mode 100644 index 0000000..d33b8cd --- /dev/null +++ b/ps-slack-notifier/PSAwsEventToSlack.ps1 @@ -0,0 +1,31 @@ +# PowerShell script file to be executed as a AWS Lambda function. +# +# When executing in Lambda the following variables will be predefined. +# $LambdaInput - A PSObject that contains the Lambda function input data. +# $LambdaContext - An Amazon.Lambda.Core.ILambdaContext object that contains information about the currently running Lambda environment. +# +# The last item in the PowerShell pipeline will be returned as the result of the Lambda function. +# +# To include PowerShell modules with your Lambda function, like the AWSPowerShell.NetCore module, add a "#Requires" statement +# indicating the module and version. + +#Requires -Modules @{ModuleName='AWSPowerShell.NetCore';ModuleVersion='3.3.335.0'} + +$jsonInput = (ConvertTo-Json -InputObject $LambdaInput -Compress -Depth 5) +$title = "AWS Health Event : "+($LambdaInput.detail.eventTypeCategory)+" : "+($LambdaInput.detail.service) +$message = $LambdaInput.detail.eventDescription.latestDescription +$message += " for details." +$payload = @{ + "attachments" = @(@{ + "color" = "danger" + "title" = $title + "text" = $message + }) + "icon_emoji" = ":rain_cloud:" + } +ConvertTo-Json -Compress -InputObject $payload +Invoke-WebRequest -UseBasicParsing ` + -Uri "https://hooks.slack.com/services/XXXX/XXXX/XXXX" ` + -Method "POST" ` + -Body (ConvertTo-Json -Compress -InputObject $payload) +Write-Output $jsonInput \ No newline at end of file diff --git a/ps-slack-notifier/readme.txt b/ps-slack-notifier/readme.txt new file mode 100644 index 0000000..be89c69 --- /dev/null +++ b/ps-slack-notifier/readme.txt @@ -0,0 +1,23 @@ +Using PowerShell 6.0 in AWS Lambda functions for forward AWS Health event to slack. + +This script require setting up a PowerShell Development Environment + 1. Install the correct version for PowerShell. AWS Lambda 's support PowerShell Core 6.0. We can find instructions in + https://docs.microsoft.com/en-us/powershell/scripting/setup/installing-powershell + 2. Install the .NET Core 2.1 SDK. You can download from https://www.microsoft.com/net/download + 3. Install the AWSLambdaPSCore module with command (with admin mode) + >Install-Module AWSLambdaPSCore + 4. If you don't have AWSPowerShell module also need to Install by PowerShell Gallery + >Install-Module -Name AWSPowerShell + +Slack setup using the same step as https://github.com/aws/aws-health-tools/tree/master/slack-notifier + +If not exist AWS Credentials need to setup this first + >Set-DefaultAWSRegion us-east-1 + >Set-AWSCredentials -AccessKey AXXXXXXXXXXXXX -SecretKey SXXXXXXXXXXXXX -StoreAs your-aws-credentails-name + >Set-AWSCredentials your-aws-credentails-name + +Using AWS Credentials + >Set-AWSCredentials your-aws-credentails-name + +Publish to AWS Lambda. + >Publish-AWSPowerShellLambda -ScriptPath .\PSAwsEventToSlack -Name PS6AwsEventToSlack -Region us-east-1 \ No newline at end of file