Created
June 2, 2024 10:02
-
-
Save overnew/aafe2f4730b9642bc1fc39a6e418dd5d to your computer and use it in GitHub Desktop.
sns_lambda_slack.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import urllib3 | |
import os | |
def lambda_handler(event, context): | |
# SNS 메시지 추출 | |
message = event['Records'][0]['Sns']['Message'] | |
# Slack Webhook URL 가져오기 | |
slack_webhook_url = os.environ['SLACK_WEBHOOK_URL'] | |
# Slack 메시지 | |
slack_message = json.dumps({ | |
"text": message | |
}) | |
http = urllib3.PoolManager() | |
# Slack 메시지 전송 | |
try: | |
response = http.request('POST', slack_webhook_url ,headers={'Content-Type': 'application/json'},body=slack_message) | |
#requests.post(slack_webhook_url, json=slack_message) | |
#if response.status_code != 200: | |
# raise Exception('Slack message sending failed. Code: {}. Content: {}'.format(response.status_code, response.content)) | |
except Exception as e: | |
print(e) | |
raise e |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment