

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 教學課程：使用 Step Functions 和 CLI Local AWS SAM 測試工作流程
<a name="sfn-local-lambda"></a>

**不支援 Step Functions Local**  
Step Functions Local **不提供**功能同位，且**不受支援**。  
您可以考慮將模擬 Step Functions 的第三方解決方案用於測試目的。  
作為 Step Functions Local 的替代方案，您可以使用 TestState API 在部署到 AWS 您的帳戶之前，先單元測試您的狀態機器邏輯。如需詳細資訊，請參閱[使用 TestState API 測試狀態機器](https://docs.aws.amazon.com/step-functions/latest/dg/test-state-isolation.html)。

在本機電腦上同時 AWS Lambda 執行 AWS Step Functions 和 時，您可以測試狀態機器和 Lambda 函數，而無需將程式碼部署到其中 AWS。

如需詳細資訊，請參閱下列主題：
+ [使用 Step Functions Local 測試狀態機器 （不支援）](sfn-local.md)
+ [設定 AWS SAM](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-quick-start.html)

## 步驟 1：設定 AWS SAM
<a name="install-sam"></a>

AWS Serverless Application Model (AWS SAM) CLI Local 需要安裝 AWS Command Line Interface AWS SAM、 和 Docker。

1. [安裝 AWS SAM CLI](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html)。
**注意**  
安裝 AWS SAM CLI 之前，您需要安裝 AWS CLI 和 Docker。請參閱安裝 CLI AWS SAM 的[先決條件](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html)。

1. 請參閱 [AWS SAM Quick Start](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-quick-start.html) 文件。請務必遵循步驟來執行以下作業：

   1. [初始化應用程式](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-quick-start.html#gs-ex1-setup-local-app)

   1.  [在本機測試應用程式](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-quick-start.html#gs-ex1-test-locally) 

   

   這會建立`sam-app`目錄，並建置包含 Python Hello World Lambda 函數的環境。

   

## 步驟 2：測試 AWS SAM CLI Local
<a name="test-local-lambda"></a>

現在您已安裝 AWS 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\$1C** 以結束 Lambda API。

## 步驟 3：啟動本機 AWS SAM CLI
<a name="start-local-lambda"></a>

現在您已測試 函數是否正常運作，請啟動 AWS SAM CLI Local。在 `sam-app` 目錄中，輸入下列命令：

```
sam local start-lambda
```

這會啟動 AWS 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
<a name="start-stepfunctions-local"></a>

### JAR 檔案
<a name="start-local-jar"></a>

如果您使用的是 Step Functions Local `.jar` 的檔案版本，請啟動 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 account-id, region us-east-1
```

### Docker
<a name="start-local-docker"></a>

如果您使用的是 Step Functions Local 的 Docker 版本，請使用下列命令啟動 Step Functions：

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

如需安裝 Step Functions Docker 版本的相關資訊，請參閱 [在 Docker 中設定 Step Functions Local （可下載版本）](sfn-local.md#sfn-local-docker)。

**注意**  
如果您從 `.jar` 檔案啟動 Step Functions，您可以透過命令列或設定環境變數來指定端點。針對 Docker 版本，您必須在文字檔案中指定端點和登入資料。請參閱 [設定 Step Functions Local 的組態選項](sfn-local.md#sfn-local-config-options)。

## 步驟 5：建立參考 CLI Local Function AWS SAM 的狀態機器
<a name="create-local-statemachine"></a>

在 Step Functions Local 執行後，建立 狀態機器，參考您在 中初始化`HelloWorldFunction`的 [步驟 1：設定 AWS SAM](#install-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:region:account-id:function:HelloWorldFunction\",\
      \"End\": true\
    }\
  }\
}\" --name "HelloWorld" --role-arn "arn:aws:iam::012345678901:role/DummyRole"
```

這會建立狀態機器，並提供可用來啟動執行的 Amazon Resource Name (ARN)。

```
{
    "creationDate": 1548805711.403, 
    "stateMachineArn": "arn:aws:states:region:account-id:stateMachine:HelloWorld"
}
```

## 步驟 6：開始您本機狀態機器的執行
<a name="run-local-statemachine"></a>

建立狀態機器後，請開始執行。使用下列**aws stepfunctions**命令時，您將需要參考端點和狀態機器 ARN：

```
aws stepfunctions --endpoint http://localhost:8083 start-execution --state-machine arn:aws:states:region:account-id:stateMachine:HelloWorld --name test
```

這會啟動名為 `HelloWorld` 狀態機器`test`的執行。

```
{
    "startDate": 1548810641.52, 
    "executionArn": "arn:aws:states:region:account-id:execution:HelloWorld:test"
}
```

現在 Step Functions 已在本機執行，您可以使用 與其互動 AWS CLI。例如，若要取得此執行的相關資訊，請使用下列命令：

```
aws stepfunctions --endpoint http://localhost:8083 describe-execution --execution-arn arn:aws:states:region:account-id:execution:HelloWorld:test
```

呼叫 `describe-execution` 執行可提供更完整的詳細資訊，類似於下列輸出：

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