[Persönliches Memorandum] Einfache Einstellung zum Senden einer Nachricht von Lambda an Slack
In Bezug auf das Senden von Nachrichten an die Slack-API
Verwenden Sie nicht slackweb library (es ist ärgerlich, die Bibliothek in Ihrer lokalen Umgebung zu installieren).
Verwenden Sie die Python-Standardbibliothek urllib.
Legen Sie die an die Slack-API übergebenen Informationen in der Lambda-Umgebungsvariablen fest.
In Bezug auf die Lambda-Erstellung mit Terraform
Die Version von aws provider ist 2.48.0
Der Lambda-Funktionscode (Python-Quellcode) wird unter Verwendung der Datenquelle ** Archivdatei ** des ** Terraform-Archivanbieters ** komprimiert und dann bereitgestellt.
Bild senden
├── .terraform │ ├── plugins │ │ └── darwin_amd64 │ │ └── terraform-provider-aws_v2.48.0_x4 ├── post-slack.tf └── source_code └── post-slack └── main.py
post-slack.tf
data "archive_file" "post-slack" {
type = "zip"
source_dir = "./source_code/post-slack"
output_path = "./source_code/post-slack.zip"
}
resource "aws_lambda_function" "post-slack" {
filename = "${data.archive_file.post-slack.output_path}"
function_name = "post-slack"
role = "arn:aws:iam::※※※※※※※※※※※※:role/service-role/lambda-basic-execution"
handler = "main.lambda_handler"
source_code_hash = "${data.archive_file.post-slack.output_base64sha256}"
runtime = "python3.8"
memory_size = 128
timeout = 300
environment {
variables = {
SLACK_CHANNEL = "#hogehoge"
SLACK_TEXT = "Slack-Benachrichtigungstest"
SLACK_USER_NAME = "Anonym"
SLACK_ICON_EMOJI = ":fearful:"
SLACK_COLOR = "warning"
SLACK_WEBHOOK_URL = "https://hooks.slack.com/services/※※※※※※※※※/※※※※※※※※※/※※※※※※※※※※※※※※※※※※※※※※※※"
}
}
}
lambda-basic-execution
data "aws_iam_policy_document" "lambda-assume-role-policy" {
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["lambda.amazonaws.com"]
}
}
}
resource "aws_iam_role" "role_lambda-basic-execution" {
name = "lambda-basic-execution"
assume_role_policy = data.aws_iam_policy_document.lambda-assume-role-policy.json
path = "/service-role/"
}
resource "aws_iam_role_policy_attachment" "AWSLambdaBasicExecutionRole" {
role = aws_iam_role.role_lambda-basic-execution.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
}
source_code/post-slack/main.py
import os
import json
from urllib.request import Request, urlopen
slackChannel = os.environ['SLACK_CHANNEL']
slackUserName = os.environ['SLACK_USER_NAME']
slackText = os.environ['SLACK_TEXT']
slackWebhookURL = os.environ['SLACK_WEBHOOK_URL']
slackIconEmoji = os.environ['SLACK_ICON_EMOJI']
slackColor = os.environ['SLACK_COLOR']
def lambda_handler(event, context):
post_slack()
def post_slack():
message = {
'channel': slackChannel,
'username': slackUserName,
'text': slackText,
'icon_emoji': slackIconEmoji,
'attachments': [
{
"color": slackColor,
"text": "was für ein Tag··"
}
]
}
data = json.dumps(message).encode('utf-8')
request = Request(slackWebhookURL, data)
urlopen(request).read()
terraform plan
** Wenn beim ersten Ausführen des Terraform-Plans die folgende Fehlermeldung angezeigt wird, **
$ terraform plan
Error: Could not satisfy plugin requirements
Plugin reinitialization required. Please run "terraform init".
Plugins are external binaries that Terraform uses to access and manipulate resources. The configuration provided requires plugins which can't be located, don't satisfy the version constraints, or are otherwise incompatible.
Terraform automatically discovers provider requirements from your configuration, including providers used in child modules. To see the requirements and constraints from each module, run "terraform providers".
Error: provider.archive: no suitable version installed version requirements: "(any version)" versions installed: none
** Führen Sie terraform init aus **
$ terraform init
Initializing the backend... Initializing provider plugins... - Checking for available provider plugins... - Downloading plugin for provider "archive" (hashicorp/archive) 2.0.0... The following providers do not have any version constraints in configuration, so the latest version was installed. To prevent automatic upgrades to new major versions that may contain breaking changes, it is recommended to add version = "..." constraints to the corresponding provider blocks in configuration, with the constraint strings suggested below. * provider.archive: version = "~> 2.0" Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure. All Terraform commands should now work. If you ever set or change modules or backend configuration for Terraform, rerun this command to reinitialize your working directory. If you forget, other commands will detect it and remind you to do so if necessary.
** terraform-provider-aws_v2.48.0_x4 wurde in das Plugins-Verzeichnis heruntergeladen **
├── .terraform │ ├── plugins │ │ └── darwin_amd64 │ │ ├── terraform-provider-archive_v2.0.0_x5 │ │ └── terraform-provider-aws_v2.48.0_x4
terraform apply
$ terraform apply
** Post-slack.zip wird nach der Ausführung im Verzeichnis source_code erstellt **
└── source_code ├── post-slack │ └── main.py └── post-slack.zip
** Lambda-Funktion "Post-Slack" -Einstellungen **
** Testlauf **
Testereignis erstellen
Testlauf
Cloudwatch protokolliert Protokollgruppe
Slack-Benachrichtigung