Notifications from AWS on MS Teams

Alerts from AWS to Teams

I’ve already written how to send notifications from Azure to MS Teams (link). Today I will show you how to send notifications from AWS to MS Teams. By the way, by changing one thing, you can also send notifications to Slack if someone does not like Teams;)

TEAMS

If we want to send notifications to the Teams application, we must have permissions there to create connectors (If we are missing, add them: Settings => Member permissions => Allow members to create, update, and remove connectors).

If we already have such permissions, we can add “incoming webhook“. Click on the 3 dots (…) next to the name of the channel in which you want to publish news and select Connectors.

powiadomienia teams notifications

Search for a webhook and click add “Incoming webhook“. Click add, then enter the name, we can change the image. After clicking create, a URL will be generated, which should be copied because we will need it later.

powiadomienia microsoft teams notifications webhook
powiadomienia teams webhook create

Amazon SNS

Now we can open the AWS website and go to ‘Simple Notification Service‘. This is the service that sends notifications. We create a new Topic. We choose the Standard type, give the name and click on ‘Create topic‘.

aws sns simple notifications service
powiadomienia aws notifications sns
aws sns (powiadomienia) fifo standard

Lambda

Now we can move on to the Lambda service. We will create a function that will translate our messages in a way that Teams can understand.

Click on ‘Create function‘, select ‘Author from scratch‘, enter the name of our function, and set the language to ‘Python 3.8‘.

lambda aws create function
lambda aws create function basic from scratch

After creating the function, we change its code to the one I put below with one exception. In place of the url, we paste the address that we created in Teams when adding the webhook.

lambda aws function code
#!/usr/bin/python3.8
import urllib3 
import json
http = urllib3.PoolManager() 
def lambda_handler(event, context): 
    url = "https://outlook.office.com/webhook/PODAJ_WŁAŚCIWY_URL"    
    msg = {
        "text": event['Records'][0]['Sns']['Message']
    }
    encoded_msg = json.dumps(msg).encode('utf-8')
    resp = http.request('POST',url, body=encoded_msg)
    print({
        "message": event['Records'][0]['Sns']['Message'], 
        "status_code": resp.status, 
        "response": resp.data
    })

When we have the right code, we can check it by clicking on test, enter a name and click create. If everything is configured correctly, we should get a 200 answer. Then we can click on deploy.

Now we can add a trigger that will trigger the sending of the message. For this purpose, we will use the topic that we created earlier in the Amazon SNS service. To do this, click on ‘Add trigger‘, select the SNS and the name of our topic.

lambda aws function add trigger
lambda aws function add trigger sns

It’s all. When we go to the topic in our Amazon SNS service, we can manually send a message that should appear on our Teams channel after a while

lambda aws function code publish message

Budget notifications

Finally, I will give you a practical example of how you can send over budget notifications.

First, we need to reopen our lambda function to add the necessary permissions to it. We copy the ARN name, which we will need in a moment. Click on ‘Edit’ and edit the ‘Access policy – optional’ section. After the square brackets, we insert the following code, in which we should replace the Resource parameter value with the ARN name that we copied earlier. Remember about the comma after the parenthesis when copying the code.

    {
      "Sid": "AWSBudgetsSNS_Permissions",
      "Effect": "Allow",
      "Principal": {
        "Service": "budgets.amazonaws.com"
      },
      "Action": "SNS:Publish",
      "Resource": "PODAJ_ARN"
    },
SNS topics aws budget notifications
AWS JSON editor powiadomienia budget notifications SNS

Now all you have to do is create a new budget, or edit an existing one. In AWS when creating/editing budget, go to the ‘Set up your notifications‘ section, if there, in addition to the email address, we provide the ARN name of our topic, it will send notifications to our Teams application.

powiadomienia aws budget notifications
powiadomienia aws budget set notifications

You can send other notifications in AWS to MS Teams in the same way.

SLACK

If you want to send messages to Slack instead of Teams, replace the code we entered in the Lambda function with the one shown below. The code only needs to be valid URL (as in Teamsach) and the channel name. Additionally, you can change the username that will appear next to the alerts.

#!/usr/bin/python3.8
import urllib3
import json
http = urllib3.PoolManager()
def lambda_handler(event, context):
    url = "https://hooks.slack.com/services/PODAJ_WŁAŚCIWY_URL"
    msg = {
        "channel": "#NAZWA_KANAŁU",
        "username": "POWIADOMIENIA_AWS",
        "text": event['Records'][0]['Sns']['Message'],
        "icon_emoji": ""
    }
    
    encoded_msg = json.dumps(msg).encode('utf-8')
    resp = http.request('POST',url, body=encoded_msg)
    print({
        "message": event['Records'][0]['Sns']['Message'], 
        "status_code": resp.status, 
        "response": resp.data
    })
aws slack notifications

More about Amazon Simple Notification Service can be found in the AWS documentation at https://docs.aws.amazon.com/sns/

If you are interested in Amazon Web Service, I encourage you to visit the blog and check the AWS category, where there will be more and more articles on this topic.

Newsletter blog Lepczynski IT

Leave a Reply

Your email address will not be published. Required fields are marked *