

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

# 使用 Terraform 在 Step Functions 中部署狀態機器
<a name="terraform-sfn"></a>

HashiCorp 的 [Terraform](https://www.terraform.io/intro/) 是使用基礎設施即程式碼 (IaC) 建置應用程式的架構。使用 Terraform，您可以建立狀態機器並使用功能，例如預覽基礎設施部署和建立可重複使用的範本。Terraform 範本會將程式碼分解為較小的區塊，協助您維護和重複使用程式碼。

如果您熟悉 Terraform，則可以遵循本主題所述的開發生命週期，做為在 Terraform 中建立和部署狀態機器的模型。如果您不熟悉 Terraform，建議您先完成 [上的 Terraform 研討會簡介 AWS](https://catalog.workshops.aws/terraform101/en-US)，以便熟悉 Terraform。

**提示**  
若要部署使用 Terraform 建置的狀態機器範例，請參閱 * AWS Step Functions 研討會*中的[使用 Terraform 部署](https://catalog.workshops.aws/stepfunctions/iac/deploy-with-terraform)。

**Topics**
+ [先決條件](#terraform-sfn-prerequisites)
+ [使用 Terraform 的開發生命週期](#terraform-sfn-dev-lifecycle)
+ [狀態機器的 IAM 角色和政策](#terraform-sfn-iam-policy)

## 先決條件
<a name="terraform-sfn-prerequisites"></a>

開始之前，請務必完成下列先決條件：
+ 在機器上安裝 Terraform。如需安裝 Terraform 的詳細資訊，請參閱[安裝 Terraform](https://developer.hashicorp.com/terraform/tutorials/aws-get-started/install-cli)。
+ 在機器上安裝 Step Functions Local。我們建議您安裝 Step Functions Local Docker 映像，以使用 Step Functions Local。如需詳細資訊，請參閱[使用 Step Functions Local 測試狀態機器 （不支援）](sfn-local.md)。
+ 安裝 AWS SAM CLI。如需安裝資訊，請參閱《 *AWS Serverless Application Model 開發人員指南*》中的[安裝 AWS SAM CLI](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/install-sam-cli.html)。
+ 安裝 AWS Toolkit for Visual Studio Code 以檢視狀態機器的工作流程圖表。如需安裝資訊，請參閱*AWS Toolkit for Visual Studio Code 《 使用者指南*[》中的安裝 AWS Toolkit for Visual Studio Code](https://docs.aws.amazon.com/toolkit-for-vscode/latest/userguide/setup-toolkit.html) 。

## 使用 Terraform 的狀態機器開發生命週期
<a name="terraform-sfn-dev-lifecycle"></a>

下列程序說明如何使用您在 Step Functions 主控台中使用 [Workflow Studio](workflow-studio.md) 建置的狀態機器原型，做為使用 Terraform 和 進行本機開發的起點[AWS Toolkit for Visual Studio Code](https://docs.aws.amazon.com/toolkit-for-vscode/latest/userguide/welcome.html)。

若要檢視與 Terraform 討論狀態機器開發並詳細說明最佳實務的完整範例，請參閱[撰寫 Step Functions Terraform 專案的最佳實務](https://aws.amazon.com/blogs/devops/best-practices-for-writing-step-functions-terraform-projects/)。

**使用 Terraform 啟動狀態機器的開發生命週期**

1. 使用以下命令引導新的 Terraform 專案。

   ```
   terraform init
   ```

1. 開啟 [Step Functions 主控台](https://console.aws.amazon.com/states/home?region=us-east-1#/)，為您的狀態機器建立原型。

1. 在 Workflow Studio 中，執行下列動作：

   1. 建立您的工作流程原型。

   1. 匯出工作流程的 [Amazon States Language (ASL)](concepts-amazon-states-language.md) 定義。若要執行此作業，請選擇**匯入/匯出**下拉式清單，然後選取**匯出 JSON 定義**。

1. 將匯出的 ASL 定義儲存在專案目錄中。

   您可以將匯出的 ASL 定義做為輸入參數傳遞給使用 [https://developer.hashicorp.com/terraform/language/functions/templatefile](https://developer.hashicorp.com/terraform/language/functions/templatefile)函數的 [https://registry.terraform.io/modules/terraform-aws-modules/step-functions/aws/latest](https://registry.terraform.io/modules/terraform-aws-modules/step-functions/aws/latest) Terraform 資源。此函數會在傳遞匯出 ASL 定義和任何變數替換的定義欄位中使用。
**提示**  
由於 ASL 定義檔案可以包含冗長的文字區塊，我們建議您避免內嵌 EOF 方法。這可讓您更輕鬆地將參數取代為您的狀態機器定義。

1. （選用） 更新 IDE 中的 ASL 定義，並使用 視覺化您的變更 AWS Toolkit for Visual Studio Code。  
![\[Visual Studio Code 中工作流程的 ASL 定義及其視覺化呈現的螢幕擷取畫面。\]](http://docs.aws.amazon.com/zh_tw/step-functions/latest/dg/images/visualize-sm-terraform-iac.png)

   為了避免持續匯出定義並將其重構到專案中，建議您在 IDE 中於本機進行更新，並使用 [Git](https://git-scm.com/) 追蹤這些更新。

1. 使用 [Step Functions Local](sfn-local.md) 測試您的工作流程。
**提示**  
您也可以使用 [AWS SAM CLI Local](sfn-local-lambda.md)，在本機測試服務與狀態機器中 Lambda 函數和 API Gateway APIs整合。

1. 部署狀態機器之前，請預覽您的狀態機器和其他 AWS 資源。若要進行這項動作，請執行以下命令。

   ```
   terraform plan
   ```

1. 使用下列命令，從本機環境或透過 [CI/CD 管道](https://aws.amazon.com/blogs/developer/build-infrastructure-ci-for-terraform-code-leveraging-aws-developer-tools-and-terratest/)部署您的狀態機器。

   ```
   terraform apply
   ```

1. （選用） 使用下列命令清除資源並刪除狀態機器。

   ```
   terraform destroy
   ```

## 狀態機器的 IAM 角色和政策
<a name="terraform-sfn-iam-policy"></a>

使用 [Terraform 服務整合政策](https://registry.terraform.io/modules/terraform-aws-modules/step-functions/aws/latest#service-integration-policies)將必要的 IAM 許可新增至您的狀態機器，例如調用 Lambda 函數的許可。您也可以定義明確的角色和政策，並將其與您的狀態機器建立關聯。

下列 IAM 政策範例授予您的狀態機器存取，以叫用名為 的 Lambda 函數`myFunction`。

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "lambda:InvokeFunction"
      ],
      "Resource": "arn:aws:lambda:us-east-1:123456789012:function:myFunction"
    }
  ]
}
```

在 Terraform 中為您的狀態機器定義 IAM 政策時，我們也建議使用 [https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) 資料來源。這可協助您檢查政策是否格式錯誤，並以變數取代任何資源。

下列 IAM 政策範例使用 `aws_iam_policy_document` 資料來源，並授予狀態機器呼叫名為 之 Lambda 函數的存取權`myFunction`。

```
data "aws_iam_policy_document" "state_machine_role_policy" {
  
  statement {
    effect = "Allow"

    actions = [
      "lambda:InvokeFunction"
    ]

    resources = ["${aws_lambda_function.function-1.arn}:*"]
  }
  
}
```

**提示**  
若要檢視使用 Terraform 部署的更進階 AWS 架構模式，請參閱 [Serverless Land Workflows Collection 中的 Terraform 範例](https://serverlessland.com/workflows?framework=Terraform)。