【问题标题】:use last successful execution time in lambda function在 lambda 函数中使用最后一次成功的执行时间
【发布时间】:2021-10-11 13:30:57
【问题描述】:

我有一个 lambda 函数,用于从 API 获取数据。在我的 lambda 函数中,我使用当前时间 - 1 小时。而不是这个,我想使用当前时间 - [最后成功执行时间] 用于该功能。这可能吗?

    from_time = ""
    if response["executions"]:
        start_time = response["executions"][0]["startDate"]
        week_ago = datetime.now() - timedelta(days=7)
        week_ago = week_ago.replace(tzinfo=start_time.tzinfo)
        if start_time > week_ago:
            from_time = get_utc_time(start_time)
        else:
            from_time = get_utc_time(datetime.now() - timedelta(hours=1))
        logger.info(f"Last successful execution time in UTC: {from_time}")

我遇到了这个https://docs.aws.amazon.com/cli/latest/reference/stepfunctions/get-execution-history.html

但我不明白如何在我的情况下使用这个get-execution-history(阶梯函数内的地形化 lambda 函数)

【问题讨论】:

  • 也许您可以将值保存在文件中并在下次执行时读取它。
  • 如何获取、保存和阅读?这就是问题:P @furas
  • 使用 AWS SDK Step Function Class 及其方法,boto3.amazonaws.com/v1/documentation/api/latest/reference/… 但是是的,我会说你应该存储执行参考,或者某处的时间

标签: python amazon-web-services lambda terraform aws-step-functions


【解决方案1】:

使用您的 SDK,在 python 的情况下为 Boto3

 import boto3

 state_machine_client = boto3.client('stepfunctions')
 execution_data = state_machine_client.describe_execution(executionArn="execution arn")

 if execution_data.get('status') == "SUCCEEDED":
      end_time = execution_data.get('stopDate')

AWS 服务之间的几乎所有交互都使用您语言的 SDK(Boto3 for python)

您可以在这里阅读更多内容:https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/stepfunctions.html#SFN.Client.describe_execution

如果您不知道执行 arn,可以使用 https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/stepfunctions.html#SFN.Client.list_executions 找到它,列表中的第一项我相信是最后一项。我很确定这就是它自动订购的方式。 - 还有开始和结束时间。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-07
    相关资源
    最近更新 更多