Catalogue
Easy in 5 Minutes: Notify Slack of EC2 Events with AWS Lambda

Easy in 5 Minutes: Notify Slack of EC2 Events with AWS Lambda

🌐 日本語で読む

Previously I created a script to check for AWS EC2 maintenance notification events.
On top of that, I made it stop and start the target instances.

I set this up to send Slack notifications via AWS Lambda
so that I can find out every morning which events require maintenance.

Prerequisites

1
2
macOS%$ pip install lambda-uploader awscli
macOS%$ aws configure --profile <profile>

Cloning the Project

1
2
3
4
5
6
7
8
macOS%$ git clone https://github.com/kenzo0107/AWSEC2Events2Slack
macOS%$ tree AWSEC2Events2Slack
.
├── README.md
├── event.json
├── lambda.json
├── lambda_function.py
└── requirements.txt

Editing the Information to Match Each Environment

  • lambda.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
{
"name": "AWSEvent2Slack",
"description": "Notificate AWS events to Slack",
"region": "ap-northeast-1",
"handler": "lambda_function.lambda_handler",
"role": "arn:aws:iam::xxxxxxxxxxxx:role/lambda-check-events-to-slack",
"timeout": 60,
"memory": 128,
"variables":
{
"SLACK_INCOMING_WEBHOOK":"https://hooks.slack.com/services/XXXXXXXXX/XXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXX",
"SLACK_CHANNEL":"#channel",
"SLACK_USERNAME":"AWSEvent2Slack",
"SLACK_ICON_URL":"http://i.imgur.com/6RCTdfi.png"
}
}
Item Explain
role Attach a policy that holds permission to describe EC2 resources
variables Slack destination information for notifications

Uploading the Source to AWS Lambda

1
2
3
4
5
macOS%$ lambda-uploader --profile <profile>

λ Building Package
λ Uploading Package
λ Fin

Checking Lambda in the AWS Console

You can see that it has been registered.

Running a Test

I was able to make it fetch events and notify Slack.

Configuring the Trigger

I set up a cron with a CloudWatch schedule expression so that it is delivered every morning.

Overall Impressions

Uploading with lambda-uploader makes the flow of
develop locally → test → deploy
and version management nice and clear.

However, one thing that bothers me is that after uploading, the source is not visible in the console.

Specifically, the following message

The deployment package of the Lambda function “AWSEvent2Slack” is too large to enable inline code editing. However, you can still invoke your function right now.

is displayed in the console.

I used to write a shell script that bundled everything into a zip and uploaded it,
and back then I could see the source.

Since I verify the behavior locally, there is no problem for now even if it is not visible in the console.

That’s all.
I hope this is helpful.

kenzo0107

kenzo0107