[PYTHON] Amazon SNS → AWS Lambda → Slack → AWS Chatbot to execute AWS commands

Since AWS announced that AWS commands can be executed on Slack, I checked whether it supports writing from other services instead of direct input.

https://aws.amazon.com/jp/blogs/devops/running-aws-commands-from-slack-using-aws-chatbot/

Product-Page-Diagram_Chatbot-How-it-Works_Console_FINAL@2x_cropped-1024x433.png

Skip preparations

Skip the settings for linking Slack and AWS Chatbot because they are on the original site.

Lambda implementation

A simple thing like this

import json
import os
import logging
import urllib.request

logger = logging.getLogger(__name__)

SLACK_URL = os.environ['SLACK_URL']

def lambda_handler(event, context):
    logger.info('Start Slack message sending.')
    message = event['Records'][0]['Sns']['Message']

    params = {
        "text": message,
        "icon_emoji": ":heavy_exclamation_mark:"
    }
    text = "payload=" + json.dumps(params)
    request = urllib.request.Request(
        SLACK_URL, 
        data = text.encode("utf-8"), 
        method="POST"
    )
    with urllib.request.urlopen(request) as response:
        response = response.read().decode("utf-8")
        return response

Since the message variable contains the message sent from SNS, it is extracted. The data from SNS stored in event is as follows.

{
  "Records": [
    {
      "EventSource": "aws:sns",
      "EventVersion": "1.0",
      "EventSubscriptionArn": "ARN contents",
      "Sns": {
        "Type": "Notification",
        "MessageId": "Message ID",
        "TopicArn": "SNS topic ARN",
        "Subject": "title",
        "Message": "Message content",
        "Timestamp": "Delivery time",
        "SignatureVersion": "1",
        "Signature": "XXX",
        "SigningCertUrl": "https://XXX",
        "MessageAttributes": {}
      }
    }
  ]
}

Settings on the SNS side

Make the following SNS settings to send information to Lambda. (Very simple, no special settings)

スクリーンショット 2019-12-03 8.54.54_deco.png

Try sending a message from SNS

Try publishing the following message from SNS.

<@aws> help

After a while, a message was posted to Slack, and the result of issuing the AWS command was returned.

スクリーンショット 2019-12-03 8.54.54_deco.png

Summary

It's easy, but I've confirmed that issuing AWS commands through AWS Chatbot can also be done by writing to Slack from other services. The possibilities of issuing Slack-> AWS Chatbot-> AWS commands have expanded again.

Recommended Posts

Amazon SNS → AWS Lambda → Slack → AWS Chatbot to execute AWS commands
Terraform configured to launch AWS Lambda from Amazon SQS
Email daily AWS bills on Lambda x Amazon SNS
Posted as an attachment to Slack on AWS Lambda (Python)
Send a request from AWS Lambda to Amazon Elasticsearch Service
Tweet in Chama Slack Bot ~ How to make a Slack Bot using AWS Lambda ~
Regularly post to Twitter using AWS lambda!
How to execute commands in jupyter notebook
Connect to s3 with AWS Lambda Python
Summary of how to write AWS Lambda
Create a setting in terraform to send a message from AWS Lambda Python3.8 to Slack
[Introduction to AWS] The first Lambda is Transcribe ♪
Sample to send slack notification with python lambda
[AWS / Lambda] How to load Python external library
Summary of studying Python to use AWS Lambda
Amazon API Gateway and AWS Lambda Python version