【发布时间】:2020-11-02 21:13:10
【问题描述】:
我在 AWS 中创建了一个 lambda 函数,显然它抛出了一个错误。这里是:
import json
from urllib.error import HTTPError, URLError
from urllib.request import Request, urlopen
import boto3
#Create a SSM Client to access parameter store
ssm = boto3.client('ssm')
def lambda_handler(event, context):
# TODO implement
#return {
# 'statusCode': 200,
# 'body': json.dumps('Hello from Lambda!')
#}
slack_message = {
'text' = f'Hello World'
}
#retrieve webhook url from parameter store
webhook_url = ssm.get_parameter(Name='slackwebhookurl', WithDecryption=True)
#make request to the API
req = Request(webhook_url['Parameter']['Value'],
json.dumps(slack_message).encode('utf-8'))
try:
response = urlopen(req)
response.read()
print("Messge posted to Slack")
except HTTPError as e:
print(f'Request failed: {e.code} {e.reason})
except URLError as e:
print(f'Server Connection failed: {e.reason})
它由 AWS SNS 通知触发。它应该为 Slack 通道获取 webhook url,然后将通知发送到 Slack。
谁能看出问题出在哪里?
如果不是很明显,有人可以指导我学习如何测试 AWS Lambda 函数的教程吗?
谢谢。
【问题讨论】:
-
您能否分享来自 Cloudwatch 的完整日志详细信息?它是与 S3、EC2、Lambda 等一起使用的 AWS 服务之一。您可能需要激活 Cloudwatch 并为其配置一些设置以输出日志,但这在故障排除方面值得做,假设您还没有这样做。
标签: python amazon-web-services testing aws-lambda