

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

# 使用 CloudFormation 範本
<a name="template-guide"></a>

 AWS CloudFormation 範本會定義您要在堆疊中建立、更新或刪除 AWS 的資源。它包含多個區段，但唯一必要的區段是 [Resources](resources-section-structure.md) 區段，且該區段必須宣告至少一項資源。

您可以使用下列方法建立範本：
+ **AWS Infrastructure Composer** – 用於設計範本的視覺化介面。
+ **文字編輯器** – 直接以 JSON 或 YAML 語法撰寫範本。
+ **IaC 產生器** – 從您帳戶中已佈建但目前未由 CloudFormation 管理的資源產生範本。IaC 產生器可搭配您所在區域中 Cloud Control API 支援的多種資源類型使用。

本節提供完整指南，說明如何使用 CloudFormation 範本的各個區段，以及如何開始建立堆疊範本。它涵蓋下列主題：

**Topics**
+ [範本的儲存位置](#where-they-get-stored)
+ [驗證範本](#template-validation)
+ [開始使用範本](#getting-started)
+ [範例範本](#sample-templates)
+ [範本格式](template-formats.md)
+ [範本區段](template-anatomy.md)
+ [Infrastructure Composer](infrastructure-composer-for-cloudformation.md)
+ [AWS CloudFormation 語言伺服器](ide-extension.md)
+ [IaC 產生器](generate-IaC.md)
+ [取得儲存在其他服務中的值](dynamic-references.md)
+ [取得 AWS 值](pseudo-parameter-reference.md)
+ [取得堆疊輸出](using-cfn-stack-exports.md)
+ [在執行時期指定現有資源](cloudformation-supplied-parameter-types.md)
+ [逐步解說](walkthroughs.md)
+ [範本程式碼片段](template-snippets.md)
+ [Windows 型堆疊](cfn-windows-stacks.md)
+ [使用 CloudFormation 提供的資源類型](cloudformation-supplied-resource-types.md)
+ [透過模組建立可重複使用的資源組態](modules.md)

## 範本的儲存位置
<a name="where-they-get-stored"></a>

**Amazon S3 儲存貯體**  
您可以將 CloudFormation 範本儲存在 Amazon S3 儲存貯體中。建立或更新堆疊時，您可以指定範本的 S3 URL，而非直接上傳。

如果您直接透過 AWS 管理主控台 或 上傳範本 AWS CLI，系統會自動為您建立 S3 儲存貯體。如需詳細資訊，請參閱[從 CloudFormation 主控台建立堆疊](cfn-console-create-stack.md)。

**Git 儲存庫**  
使用 [Git 同步](git-sync.md)，您可以在 Git 儲存庫中儲存範本。建立或更新堆疊時，您可以指定包含範本的 Git 儲存庫位置和分支，而非直接上傳或參考 S3 URL。CloudFormation 會自動監控指定儲存庫和分支的範本變更。如需詳細資訊，請參閱[使用 Git 同步從儲存庫原始程式碼建立堆疊](git-sync-create-stack-from-repository-source-code.md)。

## 驗證範本
<a name="template-validation"></a>

**語法驗證**  
您可以使用 [validate-template](service_code_examples.md#validate-template-sdk) CLI 命令，或在主控台上指定範本，驗證範本的 JSON 或 YAML 語法。主控台會自動執行驗證。如需詳細資訊，請參閱[從 CloudFormation 主控台建立堆疊](cfn-console-create-stack.md)。

但這些方法僅驗證您範本的語法，而不會驗證您為資源指定的屬性數值。

**其他驗證工具**  
如需更複雜的驗證和最佳實務檢查，您可以使用下列其他工具：
+ [CloudFormation Linter (cfn-lint)](https://github.com/aws-cloudformation/cfn-lint) – 依據 [CloudFormation 資源提供者結構描述](https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/resource-type-schemas.html)驗證範本。包括檢查資源屬性的有效值，以及最佳實務。
+ [CloudFormation Rain (rain fmt)](https://github.com/aws-cloudformation/rain) – 將您的 CloudFormation 範本格式化為一致的標準，或重新格式化從 JSON 到 YAML (或 YAML 到 JSON) 的範本。使用 YAML 時會保留註解，並在可行的情況下將內建函數切換為簡短語法。

## 開始使用範本
<a name="getting-started"></a>

若要開始建立 CloudFormation 範本，請遵循下列步驟：

1. **選擇資源** – 識別您要包含在堆疊中的 AWS 資源，例如 EC2 執行個體、VPCs、安全群組等。

1. **撰寫範本** – 以 JSON 或 YAML 格式撰寫範本，定義資源及其屬性。

1. **儲存範本** – 在本機使用副檔名儲存範本，例如：`.json`、`.yaml` 或 `.txt`。

1. **驗證範本** – 使用 [驗證範本](#template-validation) 章節所述的方法驗證範本。

1. **建立堆疊** – 使用經驗證的範本建立堆疊。

### 規劃使用 CloudFormation 範本參考
<a name="additional-resources"></a>

撰寫範本時，您可以在《[AWS 資源與屬性類型參考](https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/aws-template-resource-type-ref.html)》中，找到不同資源類型的詳細語法文件。

您的堆疊範本通常需要使用內建函數，指派執行時期才會可用的屬性數值，並使用特殊屬性控制資源的行為。撰寫範本時，可參考下列資源取得指引：
+ [內建函數參考](https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/intrinsic-function-reference.html) – 常用的內建函數包括：
  + `Ref` - 擷取資源的參數值或實體 ID。
  + `Sub` – 將字串中的預留位置替換為實際數值。
  + `GetAtt` – 傳回範本中某項資源的屬性數值。
  + `Join` – 將一組數值串聯為單一字串。
+ [資源屬性參考](https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/aws-product-attribute-reference.html) – 部分常用的特殊屬性包括：
  + `DependsOn` - 使用此屬性以指定必須在另一項資源後建立的資源。
  + `DeletionPolicy` - 使用此屬性指定 CloudFormation 應如何處理刪除資源。

## 範例範本
<a name="sample-templates"></a>

CloudFormation 提供開放原始碼堆疊範本，供您入門使用。如需詳細資訊，請參閱 GitHub 網站上的 [CloudFormation 範例範本](https://github.com/aws-cloudformation/aws-cloudformation-templates)。

請注意，這些範本並非可用於正式環境的版本。您應花時間了解其運作方式、根據自身需求調整，並確保其符合公司的合規標準。

此儲存庫中的每個範本都會通過 [CloudFormation Linter](https://github.com/aws-cloudformation/cfn-lint) (cfn-lint) 檢查，以及以Center for Internet Security (CIS)前 20 為基礎的一組基本 AWS CloudFormation Guard 規則，但某些規則的例外是，在合理的情況下，讓範例專注於單一使用案例。