Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

using powershell Lambda in slack notifier #54

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions ps-slack-notifier/PSAwsEventToSlack.ps1
Original file line number Diff line number Diff line change
@@ -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 += "<https://phd.aws.amazon.com/phd/home?region=us-east-1#/event-log?eventID=" + $($LambdaInput.detail.eventArn) + "|Click here> 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
23 changes: 23 additions & 0 deletions ps-slack-notifier/readme.txt
Original file line number Diff line number Diff line change
@@ -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