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/
Skip the settings for linking Slack and AWS Chatbot because they are on the original site.
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": {}
}
}
]
}
Make the following SNS settings to send information to Lambda. (Very simple, no special settings)
Try publishing the following message from SNS.
<@aws> help
`<XXX>`
is the notation for sending mentions in Slack.After a while, a message was posted to Slack, and the result of issuing the AWS command was returned.
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