【问题标题】:How can I troubleshoot my AWS Lambda function?如何对我的 AWS Lambda 函数进行故障排除?
【发布时间】: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


【解决方案1】:

AWS Lambda 具有内置的测试功能。您可以单击 Test 按钮并配置输入,以防函数使用来自 event 的值。

日志将显示在 Lambda 控制台中。

此外,确保与 AWS Lambda 函数关联的 IAM 角色具有 AWSLambdaBasicExecutionRole 权限策略,以便它可以写入 CloudWatch Logs。然后,您可以转到该功能的监控选项卡并单击在 CloudWatch 中查看日志以查看过去的日志。

您可以在代码中添加print() 语句,该语句将出现在日志中。

【讨论】:

  • 谢谢约翰,我会试试这些东西。
猜你喜欢
  • 1970-01-01
  • 2011-02-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-03
  • 1970-01-01
  • 2023-04-07
相关资源
最近更新 更多