

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

# 使用 Terraform 部署 AWS WAF 解決方案的安全自動化
<a name="deploy-the-security-automations-for-aws-waf-solution-by-using-terraform"></a>

*Rahul Sharad Gaikwad 醫生和 Tamilselvan P，Amazon Web Services*

## 總結
<a name="deploy-the-security-automations-for-aws-waf-solution-by-using-terraform-summary"></a>

AWS WAF 是一種 Web 應用程式防火牆，可讓您使用自訂規則，在 *Web 存取控制清單* (ACLs) 中定義和部署，協助保護應用程式免受常見入侵。設定 AWS WAF 規則可能具有挑戰性，尤其是對於沒有專用安全團隊的組織。為了簡化此程序，Amazon Web Services (AWS) 提供適用於 解決方案[的安全自動化 AWS WAF](https://aws.amazon.com/solutions/implementations/security-automations-for-aws-waf/)，其會自動部署具有一組 AWS WAF 規則的單一 Web ACL，以篩選 Web 型攻擊。在 Terraform 部署期間，您可以指定要包含哪些保護功能。部署此解決方案之後， 會 AWS WAF 檢查現有 Amazon CloudFront 分佈或 Application Load Balancer 的 Web 請求，並封鎖不符合規則的任何請求。

解決方案的 安全自動化 AWS WAF 可以 AWS CloudFormation 根據 [安全自動化 AWS WAF 實作指南](https://docs.aws.amazon.com/solutions/latest/security-automations-for-aws-waf/overview.html)中的說明使用 部署。此模式為使用 HashiCorp Terraform 作為其偏好基礎設施作為程式碼 (IaC) 工具來佈建和管理其雲端基礎設施的組織提供了替代部署選項。當您部署此解決方案時，Terraform 會自動套用雲端中的變更，並部署和設定 AWS WAF 設定和保護功能。

## 先決條件和限制
<a name="deploy-the-security-automations-for-aws-waf-solution-by-using-terraform-prereqs"></a>

**先決條件**
+ 作用中 AWS 帳戶。
+ AWS Command Line Interface (AWS CLI) 2.4.25 版或更新版本，已安裝並設定必要的許可。如需詳細資訊，請參閱[入門 ](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html)(AWS CLI 文件）。
+ 已安裝並設定 Terraform 1.1.9 版或更新版本。如需詳細資訊，請參閱[安裝 Terraform](https://learn.hashicorp.com/tutorials/terraform/install-cli) (Terraform 文件）。

## Architecture
<a name="deploy-the-security-automations-for-aws-waf-solution-by-using-terraform-architecture"></a>

**目標架構**

此模式會部署 AWS WAF 解決方案的安全自動化。如需目標架構的詳細資訊，請參閱《 *安全自動化 AWS WAF 實作指南*》中的[架構概觀](https://docs.aws.amazon.com/solutions/latest/security-automations-for-aws-waf/overview.html)。如需此部署中 AWS Lambda 自動化、應用程式日誌剖析器、 AWS WAF 日誌剖析器、IP 清單剖析器和存取處理常式的詳細資訊，請參閱《 *安全自動化 AWS WAF 實作指南》*中的[元件詳細資訊](https://docs.aws.amazon.com/solutions/latest/security-automations-for-aws-waf/appendix-b.html)。

**Terraform 部署**

當您執行 時`terraform apply`，Terraform 會執行下列動作：

1. Terraform 會根據 **testing.tfvars** 檔案的輸入建立 AWS Identity and Access Management (IAM) 角色和 Lambda 函數。

1. Terraform 會根據 **testing.tfvars **檔案的輸入建立 AWS WAF ACL 規則和 IP 集。

1. Terraform 會根據 **testing.tfvars **檔案的輸入建立 Amazon Simple Storage Service (Amazon S3) 儲存貯體、Amazon EventBridge 規則、 AWS Glue 資料庫資料表和 Amazon Athena 工作群組。

1. Terraform 部署 AWS CloudFormation 堆疊以佈建自訂資源。

1. Terraform 會根據 **testing.tfvars **檔案的指定輸入建立 Amazon API Gateway 資源。

**自動化和擴展**

您可以使用此模式為多個 建立 AWS WAF 規則 AWS 區域 ， AWS 帳戶 並在整個 AWS 雲端 環境中部署適用於 AWS WAF 解決方案的安全自動化。

## 工具
<a name="deploy-the-security-automations-for-aws-waf-solution-by-using-terraform-tools"></a>

**AWS 服務**
+ [AWS Command Line Interface (AWS CLI)](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-welcome.html) 是一種開放原始碼工具，可協助您 AWS 服務 透過命令列 shell 中的命令與 互動。
+ [AWS WAF](https://docs.aws.amazon.com/waf/latest/developerguide/what-is-aws-waf.html) 是一種 Web 應用程式防火牆，可協助您監控轉送至受保護 Web 應用程式資源的 HTTP 和 HTTPS 請求。

**其他服務**
+ [Git](https://git-scm.com/docs) 是一種開放原始碼的分散式版本控制系統。
+ [HashiCorp Terraform](https://www.terraform.io/docs) 是一種命令列界面應用程式，可協助您使用程式碼來佈建和管理雲端基礎設施和資源。

**程式碼儲存庫**

此模式的程式碼可在 GitHub [AWS WAF Automation Using Terraform](https://github.com/aws-samples/aws-waf-automation-terraform-samples) 儲存庫中使用。

## 最佳實務
<a name="deploy-the-security-automations-for-aws-waf-solution-by-using-terraform-best-practices"></a>
+ 將靜態檔案放在不同的 Amazon S3 儲存貯體中。
+ 避免硬式編碼變數。
+ 限制自訂指令碼的使用。
+ 採用命名慣例。

## 史詩
<a name="deploy-the-security-automations-for-aws-waf-solution-by-using-terraform-epics"></a>

### 設定您的本機工作站
<a name="set-up-your-local-workstation"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 安裝 Git。 | 遵循[入門](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) (Git 網站） 中的指示，在本機工作站上安裝 Git。 | DevOps 工程師 | 
| 複製儲存庫。 | 在本機工作站上，輸入下列命令來複製程式碼儲存庫：<pre>git clone https://github.com/aws-samples/aws-waf-automation-terraform-samples.git</pre> | DevOps 工程師 | 
| 更新變數。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/deploy-the-security-automations-for-aws-waf-solution-by-using-terraform.html) | DevOps 工程師 | 

### 使用 Terraform 佈建目標架構
<a name="provision-the-target-architecture-using-terraform"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 初始化 Terraform 組態。 | 輸入下列命令來初始化包含 Terraform 組態檔案的工作目錄：<pre>terraform init</pre> | DevOps 工程師 | 
| 預覽 Terraform 計劃。 | 輸入以下命令。Terraform 會評估組態檔案，以判斷宣告資源的目標狀態。然後，它會比較目標狀態與目前狀態，並建立計劃：<pre>terraform plan -var-file="testing.tfvars"</pre> | DevOps 工程師 | 
| 驗證計劃。 | 檢閱計劃並確認其已在您的目標中設定所需的架構 AWS 帳戶。 | DevOps 工程師 | 
| 部署解決方案。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/deploy-the-security-automations-for-aws-waf-solution-by-using-terraform.html) | DevOps 工程師 | 

### 驗證和清除
<a name="validate-and-clean-up"></a>


| 任務 | Description | 所需的技能 | 
| --- | --- | --- | 
| 驗證變更。 | [\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/deploy-the-security-automations-for-aws-waf-solution-by-using-terraform.html) | DevOps 工程師 | 
| （選用） 清除基礎設施。 | 如果您想要移除此解決方案所做的所有資源和組態變更，請執行下列動作：[\[See the AWS documentation website for more details\]](http://docs.aws.amazon.com/zh_tw/prescriptive-guidance/latest/patterns/deploy-the-security-automations-for-aws-waf-solution-by-using-terraform.html) | DevOps 工程師 | 

## 疑難排解
<a name="deploy-the-security-automations-for-aws-waf-solution-by-using-terraform-troubleshooting"></a>


| 問題 | 解決方案 | 
| --- | --- | 
| `WAFV2 IPSet: WAFOptimisticLockException` 錯誤 | 如果您在執行 `terraform destroy`命令時** **收到此錯誤，則必須手動刪除 IP 集。如需說明，請參閱[刪除 IP 集 ](https://docs.aws.amazon.com/waf/latest/developerguide/waf-ip-set-deleting.html)(AWS WAF 文件）。 | 

## 相關資源
<a name="deploy-the-security-automations-for-aws-waf-solution-by-using-terraform-resources"></a>

**AWS 參考**
+ [適用於 AWS WAF 實作的安全自動化指南](https://docs.aws.amazon.com/solutions/latest/security-automations-for-aws-waf/welcome.html)
+ [的安全自動化 AWS WAF](https://aws.amazon.com/solutions/implementations/security-automations-for-aws-waf/)(AWS 解決方案程式庫）
+ [安全自動化 AWS WAF 常見問答集](https://aws.amazon.com/solutions/implementations/security-automations-for-aws-waf/resources/#FAQ)

**Terraform 參考**
+ [Terraform 後端組態](https://developer.hashicorp.com/terraform/language/backend)
+ [Terraform AWS 提供者 - 文件和使用](https://registry.terraform.io/providers/hashicorp/aws/latest/docs)
+ [Terraform AWS 提供者](https://github.com/hashicorp/terraform-provider-aws) (GitHub 儲存庫）