【问题标题】:How to troubleshoot and solve lambda function issue?如何排查和解决 lambda 函数问题?
【发布时间】:2019-12-24 07:53:48
【问题描述】:

import sys
import botocore
import boto3
from botocore.exceptions import ClientError
def lambda_handler(event, context):
    # TODO implement
    rds = boto3.client('rds')
    lambdaFunc = boto3.client('lambda')
    print 'Trying to get Environment variable'
    try:
        funcResponse = lambdaFunc.get_function_configuration(
            FunctionName='RDSInstanceStart'
        )
        #print (funcResponse)
        DBinstance = funcResponse['Environment']['Variables']['DBInstanceName']
        print 'Starting RDS service for DBInstance : ' + DBinstance
    except ClientError as e:
        print(e)    
    try:
        response = rds.start_db_instance(
            DBInstanceIdentifier=DBinstance
        )
        print 'Success :: ' 
        return response
    except ClientError as e:
        print(e)    
    return
    {
        'message' : "Script execution completed. See Cloudwatch logs for complete output"
    } 

我有一个正在运行的 rds 实例。每天我都使用 lambda 表达式启动和停止我的 AWS 的 RDS 实例(db.t2.micro (MSSQL Server))。它以前工作正常,但出乎意料的是今天我遇到了这个问题。 我的 rds 实例不是由 lambda 表达式自动启动的。我看了一个错误日志,但没有问题,它通常看起来像每日日志。我无法排除故障并解决问题。谁能告诉我这个问题?

【问题讨论】:

  • 如果您的日志未显示问题,请更新您的日志,直到问题在日志中可见。或者尝试重现问题并定位问题。
  • 你能告诉我们你用来启动实例的代码吗?您是否在其周围添加了调试代码以显示可能出现的问题?您是否可以手动启动 RDS 实例,只是为了检查是否可行?
  • @JohnRotenstein 先生,我已经添加了代码。
  • 仅供参考,您可以通过import osdbName = os.environ['DBInstanceName']) 访问函数的环境变量
  • 你可以print response而不是return response吗?这样,您将在日志文件中看到结果。

标签: amazon-web-services


【解决方案1】:

仅供参考,缩短的版本是:

import boto3
import os

def lambda_handler(event, context):
    rds_client = boto3.client('rds')
    response = rds.start_db_instance(DBInstanceIdentifier=os.environ['DBInstanceName'])
    print response

【讨论】:

    【解决方案2】:

    您可以在cloudwatch或aws lambda->监控->在cloud watch中查看每个lambda调用的日志。这将打开一个页面,其中包含每个 lambda 调用的日志。 如果没有日志。这意味着 lambda 没有调用。 如果正确,您可以检查分配给 lambda 的角色和策略。

    【讨论】:

    • 谢谢您的评论,先生。正如我在问题中提到的那样,我检查了日志,它似乎是每日日志(从与您描述的相同路径观看)。但无法解决问题。
    【解决方案3】:

    您应该打印用于启动数据库的 api 的响应(ex-start-db-instance)。响应将打印到 CloudWatch Log。

    https://docs.aws.amazon.com/cli/latest/reference/rds/start-db-instance.html

    为了以后的自动化,您可能希望在 Lambda 的 CloudWatch Logs 上针对某个关键字创建一个指标过滤器,例如 -

    "\"DBInstanceStatus\":\"开始\""

    设置say threshold

    您可以稍微调整警报以将丢失的数据视为错误,然后当指标过滤器与传入日志不匹配时,警报将转换为警报状态。

    【讨论】:

      猜你喜欢
      • 2018-11-10
      • 1970-01-01
      • 2023-03-08
      • 2020-09-01
      • 1970-01-01
      • 2010-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多