本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
测试 Step Functions 和Amazon SAMCLI 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。
-
注意 在安装 Amazon SAM CLI 之前,您需要先安装 Amazon CLI 和 Docker。请参阅先决条件用于安装Amazon SAMCLI。
-
通读 Amazon SAM 快速入门文档。请务必按照以下步骤执行以下操作:
这会创建一个
sam-app
目录,并将生成环境,其中包括基于 Python 语言的 Hello World Lambda 函数。
第 2 步:测试Amazon SAMCLI 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"}
EnterCTRL+C以结束 Lambda API。
第 3 步:启动Amazon SAMCLI Local
现在,您已测试该函数可正常工作,接下来启动 Amazon SAM CLI Local。在sam-app
目录中,输入以下内容命令:
sam local start-lambda
这开始Amazon SAMCLI 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 本地
JAR 文件
如果您使用的是.jar
文件版本的 Step Functions 本地,启动 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 的信息,请参阅设置LocaStep Functions cal(可下载版本)和 Docar Funcal.
您可以通过命令行指定终端节点;如果您从.jar
文件。对于 Docker 版本,您必须在文本文件中指定终端节点和凭证。请参阅为本地Step Functions 设置配置选项。
第 5 步:创建引用您的状态机Amazon SAMCLI Local 函数
在 Local Step Functions 运行后,创建一个状态机,该状态机引用HelloWorldFunction
你初始化的第 1 步:设置 Amazon SAM.
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 步:启动本地状态机的执行
在您创建状态机后,即可启动执行. 使用以下内容时,您需要引用终端节点和状态机 ARNaws stepfunctions
命令:
aws stepfunctions --endpoint http://localhost:8083 start-execution --state-machine arn:aws:states:us-east-1:123456789012:stateMachine:HelloWorld --name test
这开始执行被命名test
您的HelloWorld
状态机.
{
"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": "{}"
}