步骤执行完成后获取任务状态的函数

发布于 2025-01-11 10:14:51 字数 1500 浏览 0 评论 0原文

我尝试修改此 python 脚本以获取步骤函数执行的结果,但失败:

File "step_function.py", line 6, in <module>
    input = json.dumps({})
  File "/usr/local/lib/python2.7/dist-packages/botocore/client.py", line 386, in _api_call
    return self._make_api_call(operation_name, kwargs)
  File "/usr/local/lib/python2.7/dist-packages/botocore/client.py", line 705, in _make_api_call
    raise error_class(parsed_response, operation_name)
botocore.exceptions.ClientError: An error occurred (UnrecognizedClientException) when calling the StartExecution operation: The security token included in the request is invalid.

我在 python 脚本中使用的代码是:

 import time
  from time import sleep
  import boto3
    import json
    sf_client = boto3.client('stepfunctions')
    sf_output = sf_client.start_execution(
        stateMachineArn = arn:aws:states:us-west-2:208244xxxxx:stateMachine:samplePipelinedevs-xxxxx,
        input = json.dumps({})
    )
    
    while True:
        time.sleep(5) # don't need to check every nanosecond
    
        sf_response = sf_client.describe_execution(executionArn=sf_output['executionArn'])
        status = sf_response['status'] # BE SURE TO GET THE CURRENT STATE
    
        print("%s: %s" % ("> Status...", status))
    
        if status == 'RUNNING':
            continue
        elif status == 'FAILED':
            raise Exception("%s: %s" % ("! ERROR ! Execution FAILED: ", sf_response))
        else: # SUCCEEDED
            break

I have tried to modify this python script to get the result of the step function execution , but is failing with :

File "step_function.py", line 6, in <module>
    input = json.dumps({})
  File "/usr/local/lib/python2.7/dist-packages/botocore/client.py", line 386, in _api_call
    return self._make_api_call(operation_name, kwargs)
  File "/usr/local/lib/python2.7/dist-packages/botocore/client.py", line 705, in _make_api_call
    raise error_class(parsed_response, operation_name)
botocore.exceptions.ClientError: An error occurred (UnrecognizedClientException) when calling the StartExecution operation: The security token included in the request is invalid.

The code I used in the python script is :

 import time
  from time import sleep
  import boto3
    import json
    sf_client = boto3.client('stepfunctions')
    sf_output = sf_client.start_execution(
        stateMachineArn = arn:aws:states:us-west-2:208244xxxxx:stateMachine:samplePipelinedevs-xxxxx,
        input = json.dumps({})
    )
    
    while True:
        time.sleep(5) # don't need to check every nanosecond
    
        sf_response = sf_client.describe_execution(executionArn=sf_output['executionArn'])
        status = sf_response['status'] # BE SURE TO GET THE CURRENT STATE
    
        print("%s: %s" % ("> Status...", status))
    
        if status == 'RUNNING':
            continue
        elif status == 'FAILED':
            raise Exception("%s: %s" % ("! ERROR ! Execution FAILED: ", sf_response))
        else: # SUCCEEDED
            break

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷了相思 2025-01-18 10:14:51

根据 AWS 文档,如果您使用 AWS CLI 和配置文件名称创建了凭证,那么您需要使用该配置文件配置您的客户端。为此,您可以使用带有配置文件名称的会话。下面的例子(写你的代码)。

import boto3

session = boto3.Session(profile_name='devsample')
sf_client = session.client('stepfunctions')
.
.

Boto 必须知道要使用哪个配置文件和凭据,您可以使用 Session 指定它们。有关更多详细信息,请参阅文档

From the AWS Documentation, if you created a credential with AWS CLI and with a profile name, then you need to configure your client with that profile. For this you can use the session with profile name. Example below (w.r.t your code).

import boto3

session = boto3.Session(profile_name='devsample')
sf_client = session.client('stepfunctions')
.
.

Boto has to know, which profile and credential to use, which you can specify with Session. Refer to the documentation for more details.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文