测试 Step Function 和 Amazon SAM CLI Local - Amazon Step Functions
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

测试 Step Function 和 Amazon SAM CLI Local

通过在本地计算机上将 Amazon Step Functions 与 Amazon Lambda 一起运行,可以测试您的状态机和 Lambda 函数,而无需将您的代码部署到 Amazon。

有关更多信息,请参阅以下主题:

第 1 步:设置 Amazon SAM

Amazon Serverless Application Model (Amazon SAM) CLI Local 要求安装 Amazon Command Line Interface、Amazon SAM 和 Docker。

  1. 安装 Amazon SAM CLI

    注意

    在安装 Amazon SAM CLI 之前,您需要先安装 Amazon CLI 和 Docker。参阅安装 Amazon SAM CLI 的先决条件

  2. 通读 Amazon SAM 快速入门文档。请务必按照以下步骤执行以下操作:

    这将创建 sam-app 目录,并将生成环境,其中包括基于 Python 语言的 Hello World Lambda 函数。

第 2 步:测试 Amazon SAM CLI Local

现在,您已安装了 Amazon SAM 并创建了 Hello World Lambda 函数,接下来进行测试。在 sam-app 目录中,输入下面的命令:

sam local start-api

这将启动 Lambda 函数的本地实例。您应该可以看到类似于如下所示的输出内容:

2019-01-31 16:40:27 Found credentials in shared credentials file: ~/.aws/credentials 2019-01-31 16:40:27 Mounting HelloWorldFunction at http://127.0.0.1:3000/hello [GET] 2019-01-31 16:40:27 You can now browse to the above endpoints to invoke your functions. You do not need to restart/reload SAM CLI while working on your functions changes will be reflected instantly/automatically. You only need to restart SAM CLI if you update your AWS SAM template 2019-01-31 16:40:27 * Running on http://127.0.0.1:3000/ (Press CTRL+C to quit)

打开浏览器并输入以下内容:

http://127.0.0.1:3000/hello

这将输出以下类似响应:

{"message": "hello world", "location": "72.21.198.66"}

CTRL+C 以终止 Lambda API。

第 3 步:启动 Amazon SAM CLI Local

现在,您已测试该函数可正常工作,接下来启动 Amazon SAM CLI Local。在 sam-app 目录中,输入下面的命令:

sam local start-lambda

这将启动 Amazon SAM CLI Local 并提供要使用的端点,类似于以下输出:

2019-01-29 15:33:32 Found credentials in shared credentials file: ~/.aws/credentials 2019-01-29 15:33:32 Starting the Local Lambda Service. You can now invoke your Lambda Functions defined in your template through the endpoint. 2019-01-29 15:33:32 * Running on http://127.0.0.1:3001/ (Press CTRL+C to quit)

第 4 步:启动 Step Functions Local

JAR 文件

如果使用 .jar 文件版本的 Step Functions Local,请启动 Step Functions 并指定 Lambda 端点。在提取 .jar 文件的目录中,输入以下命令:

java -jar StepFunctionsLocal.jar --lambda-endpoint http://localhost:3001

当 Step Functions Local 启动时,它将检查环境,然后检查 ~/.aws/credentials 文件中配置的凭证。默认情况下,它开始使用虚假用户 ID,并列出为 region us-east-1

2019-01-29 15:38:06.324: Failed to load credentials from environment because Unable to load AWS credentials from environment variables (AWS_ACCESS_KEY_ID (or AWS_ACCESS_KEY) and AWS_SECRET_KEY (or AWS_SECRET_ACCESS_KEY)) 2019-01-29 15:38:06.326: Loaded credentials from profile: default 2019-01-29 15:38:06.326: Starting server on port 8083 with account 123456789012, region us-east-1

Docker

如果您使用 Docker 版本的 Step Functions Local,请使用以下命令启动 Step Functions:

docker run -p 8083:8083 amazon/aws-stepfunctions-local

有关安装 Docker 版本的 Step Functions 的信息,请参阅设置 Step Functions Local(可下载版本)和 Docker

注意

您可以通过命令行指定端点;如果您从 .jar 文件启动 Step Functions,则可以通过设置环境变量来指定。对于 Docker 版本,您必须在文本文件中指定端点和凭证。请参阅为 Step Functions Local 设置配置选项

第 5 步:创建引用您的 Amazon SAM CLI Local 函数的状态机

在 Step Functions Local 运行后,创建一个状态机,该状态机引用您在 HelloWorldFunction 中初始化的 。

aws stepfunctions --endpoint http://localhost:8083 create-state-machine --definition "{\ \"Comment\": \"A Hello World example of the Amazon States Language using an AWS Lambda Local function\",\ \"StartAt\": \"HelloWorld\",\ \"States\": {\ \"HelloWorld\": {\ \"Type\": \"Task\",\ \"Resource\": \"arn:aws:lambda:us-east-1:123456789012:function:HelloWorldFunction\",\ \"End\": true\ }\ }\ }\ }}" --name "HelloWorld" --role-arn "arn:aws:iam::012345678901:role/DummyRole"

这将创建一个状态机,并提供一个您可以用来启动执行的 Amazon 资源名称 (ARN)。

{ "creationDate": 1548805711.403, "stateMachineArn": "arn:aws:states:us-east-1:123456789012:stateMachine:HelloWorld" }

第 6 步:启动本地状态机执行

创建状态机后,启动执行。使用以下 aws stepfunctions 命令时,您需要引用端点和状态机 ARN:

aws stepfunctions --endpoint http://localhost:8083 start-execution --state-machine arn:aws:states:us-east-1:123456789012:stateMachine:HelloWorld --name test

这将启动 HelloWorld 状态机的名为 test 的执行。

{ "startDate": 1548810641.52, "executionArn": "arn:aws:states:us-east-1:123456789012:execution:HelloWorld:test" }

现在 Step Functions 正在本地运行,您可以使用 Amazon CLI 与其进行交互。例如,要获取有关此执行的信息,请使用以下命令:

aws stepfunctions --endpoint http://localhost:8083 describe-execution --execution-arn arn:aws:states:us-east-1:123456789012:execution:HelloWorld:test

请为执行调用 describe-execution,这可以提供更完整的详细信息,内容与下面的输出类似:

{ "status": "SUCCEEDED", "startDate": 1549056334.073, "name": "test", "executionArn": "arn:aws:states:us-east-1:123456789012:execution:HelloWorld:test", "stateMachineArn": "arn:aws:states:us-east-1:123456789012:stateMachine:HelloWorld", "stopDate": 1549056351.276, "output": "{\"statusCode\": 200, \"body\": \"{\\\"message\\\": \\\"hello world\\\", \\\"location\\\": \\\"72.21.198.64\\\"}\"}", "input": "{}" }