Skip to content

Gungz/courier-lambda-notification

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Cloud Notification using Courier

Objective

As more and more infrastructures are deployed in the cloud, there's a need to receive notification in case our cloud infrastructures experience some changes e.g. CPU utilization almost full, Virtual Machine changes status to Stopped, etc. While this notification feature is provided by Cloud Provider out of the box, we may need to subscribe the notification to various channels that we use individually. Also, message coming from cloud provider by default is not user-friendly and we need to convert it.

Solution

Using Courier, we can solve this issue. We can create notification template and integrate providers such as Email, SMS, and Slack in Courier, which then can be used by Cloud Provider serverless such as AWS Lambda or GCP Cloud Function to call and send notification to multi-channel. See Architecture below for more details.

Architecture

courier cloud provider notification architecture

Prerequisite

  1. Python 3.9
  2. AWS Account
  3. Courier Account
  4. Twilio Account
  5. Slack Account
  6. Gmail Account

How to Setup

  1. Follow Readme in this link to familiarize yourself with Courier and also to create accounts such as Gmail, Twilio, and Courier itself.

  2. Follow this link especially the part that shows how to create Slack App. Once done, please add a channel (I call mine #cloud-notification) in slack workspace and invite the bot to the channel. Take note of the Channel ID in About tab as it will be used in the lambda setup. slack bot in channel

  3. In the Courier app, go to Designer and then Create Template, name it anything you like, I use Cloud Notification as name. Configure 3 channels as per screen-shot below. Courier Email Template Courier SMS Template Courier Slack Template Click the gear icon at the top (near the Cloud Notification in the screen shot) and take Notification ID which will be used during lambda setup.

  4. Create your AWS account.

  5. Create Standard SNS Topic in AWS account. Name it anything you like. My SNS topic in this tutorial is dlm-notification.

  6. Create event bridge rule that reacts to any AWS event you like. In this setup, I'll focus on EC2 Instance State Change. Below screen-shots are provided to help you setup and please follow exactly the JSON when configuring the transformer in Event Bridge as the exact attributes are required to make the lambda (python code) work. Event Bridge Rule Details Event Bridge Event Pattern 1 Event Bridge Event Pattern 2 Event Bridge Target Event Bridge Transformer Full JSON for Template is below as the screen-shot can't display full value.

    { "content": "EC2 Instance <instance> in account ID <account> and region <region> has changed state to <state>", "CloudProvider": "AWS", "Subject": "EC2 Instance State Change" }

    Feel free to configure tags if you like and create rule in Review and update.

  7. Prepare lambda package by following below instructions:

  • In terminal, go to folder that contains this README file, then run pip install --target ./package trycourier
  • Then, run cd package/
  • Then, run zip -r ../courier-lambda-package.zip .
  • Then, run cd ..
  • Then, run zip courier-lambda-package.zip index.py
  1. Create AWS lambda that will host the python code. Below screen-shots and instructions are provided to help you setup the lambda. Lambda Creation Screen Click Create Function at the bottom of the page, then upload the lambda package zip file created previously. Lambda Code Upload In the Configuration -> Permissions tab, add permission to invoke lambda from SNS in Resource-based policy statements. Pls change the SNS topic Arn with the one you configure (don't use what is in the screen-shot). Lambda Configuration Permission Lambda Config Permission Policy Detail Then, in Configuration -> Environment Variables, make sure you setup environment variables for below variables. Get the value from your Courier and Slack environment. For DEST_EMAIL and DEST_PHONE, put any email and phone you'd like to use to get notification from cloud event.

    COURIER_TEMPLATE is Courier Notification ID.

    COURIER_TOKEN is Courier API Key

    SLACK_CHANNEL is Slack Channel ID

    SLACK_TOKEN is Slack Bot OAuth Token Lambda Environment Variables

Testing

You can test the lambda by configuring test event or just creating EC2 instance and then changing the state of EC2. If it works, you'll see notification in your email, SMS, and slack channel.

Next Step

  1. Convert manual step mentioned in this README to using Terraform, AWS SAM, or Serverless framework (Infra as Code).
  2. Add another AWS event in Event Bridge but ensuring the Input Transformer will output JSON with content, CloudProvider, and Subject attributes.
  3. Deploy the same function to another cloud provider e.g. GCP.

How to Contribute

  1. As this is public repo, feel free to submit pull request in case you'd like to contribute to this project.
  2. The contribution could include: README update, converting this manual step into Infra as Code, adding another AWS event and configuring the proper Input Transformer, etc.

About

AWS Lambda Courier Notification

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages