

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

# AWS CLI 和 PowerShell 的 CloudFormation 堆疊操作命令範例
<a name="service_code_examples"></a>

下列命令列範例示範如何使用 AWS CLI 和 PowerShell 執行個別 CloudFormation 動作。這些範例僅包含最常使用的動作。如需完整清單，請參閱 *AWS CLI 命令參考*中的 [https://docs.aws.amazon.com/cli/latest/reference/cloudformation/](https://docs.aws.amazon.com/cli/latest/reference/cloudformation/)。

本指南中的範例採用反斜線 (\$1) 的慣例，用於表示較長的命令列會延續至下一行。

**Topics**
+ [取消堆疊更新](#cancel-update-stack-sdk)
+ [繼續復原更新](#continue-update-rollback-sdk)
+ [建立堆疊](#create-stack-sdk)
+ [建立包含轉換的堆疊](#deploy-sdk)
+ [刪除堆疊](#delete-stack-sdk)
+ [描述堆疊事件](#describe-stack-events-sdk)
+ [描述堆疊資源](#describe-stack-resource-sdk)
+ [描述堆疊資源](#describe-stack-resources-sdk)
+ [描述堆疊](#describe-stacks-sdk)
+ [取得範本](#get-template-sdk)
+ [列出堆疊資源](#list-stack-resources-sdk)
+ [列出堆疊](#list-stacks-sdk)
+ [更新堆疊](#update-stack-sdk)
+ [驗證您的範本](#validate-template-sdk)
+ [使用 將本機成品上傳至 S3 儲存貯體 AWS CLI](using-cfn-cli-package.md)

## 取消堆疊更新
<a name="cancel-update-stack-sdk"></a>

使用 [https://docs.aws.amazon.com/cli/latest/reference/cloudformation/cancel-update-stack.html](https://docs.aws.amazon.com/cli/latest/reference/cloudformation/cancel-update-stack.html) 命令以取消堆疊更新。如需詳細資訊，請參閱[取消堆疊更新](using-cfn-stack-update-cancel.md)。

------
#### [ CLI ]

**AWS CLI**  
**取消進行中的堆疊更新**  
下列 `cancel-update-stack` 命令會取消 `myteststack` 堆疊上的堆疊更新：  

```
aws cloudformation cancel-update-stack --stack-name myteststack
```
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [CancelUpdateStack](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/cloudformation/cancel-update-stack.html)。

------
#### [ PowerShell ]

**Tools for PowerShell V4**  
**範例 1：取消指定堆疊的更新。**  

```
Stop-CFNUpdateStack -StackName "myStack"
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [CancelUpdateStack](https://docs.aws.amazon.com/powershell/v4/reference)。

**Tools for PowerShell V5**  
**範例 1：取消指定堆疊的更新。**  

```
Stop-CFNUpdateStack -StackName "myStack"
```
+  如需 API 詳細資訊，請參閱《*AWS Tools for PowerShell Cmdlet 參考 (V5)*》中的 [CancelUpdateStack](https://docs.aws.amazon.com/powershell/v5/reference)。

------

## 繼續復原更新
<a name="continue-update-rollback-sdk"></a>

使用 [https://docs.aws.amazon.com/cli/latest/reference/cloudformation/continue-update-rollback.html](https://docs.aws.amazon.com/cli/latest/reference/cloudformation/continue-update-rollback.html) 命令繼續復原更新。如需詳細資訊，請參閱[繼續復原更新](using-cfn-updating-stacks-continueupdaterollback.md)。

------
#### [ CLI ]

**AWS CLI**  
**重試更新復原**  
下列 `continue-update-rollback` 範例從先前失敗的堆疊更新處繼續還原操作。  

```
aws cloudformation continue-update-rollback \
    --stack-name my-stack
```
此命令不會產生輸出。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [ContinueUpdateRollback](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/cloudformation/continue-update-rollback.html)。

------
#### [ PowerShell ]

**Tools for PowerShell V4**  
**範例 1：繼續還原具名堆疊，其應處於 'UPDATE\$1ROLLBACK\$1FAILED' 狀態。如果繼續還原成功，堆疊將進入狀態 'UPDATE\$1ROLLBACK\$1COMPLETE'。**  

```
Resume-CFNUpdateRollback -StackName "myStack"
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [ContinueUpdateRollback](https://docs.aws.amazon.com/powershell/v4/reference)。

**Tools for PowerShell V5**  
**範例 1：繼續還原具名堆疊，其應處於 'UPDATE\$1ROLLBACK\$1FAILED' 狀態。如果繼續還原成功，堆疊將進入狀態 'UPDATE\$1ROLLBACK\$1COMPLETE'。**  

```
Resume-CFNUpdateRollback -StackName "myStack"
```
+  如需 API 詳細資訊，請參閱《*AWS Tools for PowerShell Cmdlet 參考 (V5)*》中的 [ContinueUpdateRollback](https://docs.aws.amazon.com/powershell/v5/reference)。

------

## 建立堆疊
<a name="create-stack-sdk"></a>

使用 [https://docs.aws.amazon.com/cli/latest/reference/cloudformation/create-stack.html](https://docs.aws.amazon.com/cli/latest/reference/cloudformation/create-stack.html) 命令來建立堆疊。您必須提供堆疊名稱、有效範本的位置和任何輸入參數。參數索引鍵名稱區分大小寫。若您輸入錯誤的參數金鑰名稱，CloudFormation 就不會建立堆疊，並會報告範本未包含該參數。

以下範例展示如何使用指定的名稱、範本和輸入參數建立新的堆疊。

------
#### [ CLI ]

**AWS CLI**  
**建立 an AWS CloudFormation 堆疊**  
以下 `create-stacks` 命令使用 `sampletemplate.json` 範本建立名為 `myteststack` 的堆疊：  

```
aws cloudformation create-stack --stack-name myteststack --template-body file://sampletemplate.json --parameters ParameterKey=KeyPairName,ParameterValue=TestKey ParameterKey=SubnetIDs,ParameterValue=SubnetID1\\,SubnetID2
```
輸出：  

```
{
    "StackId": "arn:aws:cloudformation:us-east-1:123456789012:stack/myteststack/466df9e0-0dff-08e3-8e2f-5088487c4896"
}
```
如需詳細資訊，請參閱《*AWS CloudFormation 使用者指南*》中的堆疊。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [CreateStack](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/cloudformation/create-stack.html)。

------
#### [ PowerShell ]

**Tools for PowerShell V4**  
**範例 1：使用指定的名稱建立新堆疊。範本是根據提供的內容和自訂參數 ('PK1' 和 'PK2' 代表範本內容中宣告的參數名稱，'PV1' 和 'PV2' 代表這些參數的值) 進行剖析。自訂參數也可以使用 'Key' 和 'Value' (而不是 'ParameterKey' 和 'ParameterValue') 來指定。如果堆疊建立失敗，則不會還原。**  

```
New-CFNStack -StackName "myStack" `
             -TemplateBody "{TEMPLATE CONTENT HERE}" `
             -Parameter @( @{ ParameterKey="PK1"; ParameterValue="PV1" }, @{ ParameterKey="PK2"; ParameterValue="PV2" }) `
             -DisableRollback $true
```
**範例 2：使用指定的名稱建立新堆疊。範本是根據提供的內容和自訂參數 ('PK1' 和 'PK2' 代表範本內容中宣告的參數名稱，'PV1' 和 'PV2' 代表這些參數的值) 進行剖析。自訂參數也可以使用 'Key' 和 'Value' (而不是 'ParameterKey' 和 'ParameterValue') 來指定。如果堆疊建立失敗，則會還原。**  

```
$p1 = New-Object -Type Amazon.CloudFormation.Model.Parameter
$p1.ParameterKey = "PK1"
$p1.ParameterValue = "PV1"

$p2 = New-Object -Type Amazon.CloudFormation.Model.Parameter
$p2.ParameterKey = "PK2"
$p2.ParameterValue = "PV2"

New-CFNStack -StackName "myStack" `
             -TemplateBody "{TEMPLATE CONTENT HERE}" `
             -Parameter @( $p1, $p2 ) `
             -OnFailure "ROLLBACK"
```
**範例 3：使用指定的名稱建立新堆疊。範本是從具有自訂參數 ('PK1' 代表範本內容中宣告的參數名稱，'PV1' 代表參數的值) 的 Amazon S3 URL 取得。自訂參數也可以使用 'Key' 和 'Value' (而不是 'ParameterKey' 和 'ParameterValue') 來指定。如果建立堆疊失敗，則會還原 (與指定 -DisableRollback \$1false 相同)。**  

```
New-CFNStack -StackName "myStack" `
             -TemplateURL https://s3.amazonaws.com/amzn-s3-demo-bucket/templatefile.template `
             -Parameter @{ ParameterKey="PK1"; ParameterValue="PV1" }
```
**範例 4：使用指定的名稱建立新堆疊。範本是從具有自訂參數 ('PK1' 代表範本內容中宣告的參數名稱，'PV1' 代表參數的值) 的 Amazon S3 URL 取得。自訂參數也可以使用 'Key' 和 'Value' (而不是 'ParameterKey' 和 'ParameterValue') 來指定。如果建立堆疊失敗，則會還原 (與指定 -DisableRollback \$1false 相同)。指定的通知 AEN 會收到已發佈的堆疊相關事件。**  

```
New-CFNStack -StackName "myStack" `
             -TemplateURL https://s3.amazonaws.com/amzn-s3-demo-bucket/templatefile.template `
             -Parameter @{ ParameterKey="PK1"; ParameterValue="PV1" } `
             -NotificationARN @( "arn1", "arn2" )
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [CreateStack](https://docs.aws.amazon.com/powershell/v4/reference)。

**Tools for PowerShell V5**  
**範例 1：使用指定的名稱建立新堆疊。範本是根據提供的內容和自訂參數 ('PK1' 和 'PK2' 代表範本內容中宣告的參數名稱，'PV1' 和 'PV2' 代表這些參數的值) 進行剖析。自訂參數也可以使用 'Key' 和 'Value' (而不是 'ParameterKey' 和 'ParameterValue') 來指定。如果堆疊建立失敗，則不會還原。**  

```
New-CFNStack -StackName "myStack" `
             -TemplateBody "{TEMPLATE CONTENT HERE}" `
             -Parameter @( @{ ParameterKey="PK1"; ParameterValue="PV1" }, @{ ParameterKey="PK2"; ParameterValue="PV2" }) `
             -DisableRollback $true
```
**範例 2：使用指定的名稱建立新堆疊。範本是根據提供的內容和自訂參數 ('PK1' 和 'PK2' 代表範本內容中宣告的參數名稱，'PV1' 和 'PV2' 代表這些參數的值) 進行剖析。自訂參數也可以使用 'Key' 和 'Value' (而不是 'ParameterKey' 和 'ParameterValue') 來指定。如果堆疊建立失敗，則會還原。**  

```
$p1 = New-Object -Type Amazon.CloudFormation.Model.Parameter
$p1.ParameterKey = "PK1"
$p1.ParameterValue = "PV1"

$p2 = New-Object -Type Amazon.CloudFormation.Model.Parameter
$p2.ParameterKey = "PK2"
$p2.ParameterValue = "PV2"

New-CFNStack -StackName "myStack" `
             -TemplateBody "{TEMPLATE CONTENT HERE}" `
             -Parameter @( $p1, $p2 ) `
             -OnFailure "ROLLBACK"
```
**範例 3：使用指定的名稱建立新堆疊。範本是從具有自訂參數 ('PK1' 代表範本內容中宣告的參數名稱，'PV1' 代表參數的值) 的 Amazon S3 URL 取得。自訂參數也可以使用 'Key' 和 'Value' (而不是 'ParameterKey' 和 'ParameterValue') 來指定。如果建立堆疊失敗，則會還原 (與指定 -DisableRollback \$1false 相同)。**  

```
New-CFNStack -StackName "myStack" `
             -TemplateURL https://s3.amazonaws.com/amzn-s3-demo-bucket/templatefile.template `
             -Parameter @{ ParameterKey="PK1"; ParameterValue="PV1" }
```
**範例 4：使用指定的名稱建立新堆疊。範本是從具有自訂參數 ('PK1' 代表範本內容中宣告的參數名稱，'PV1' 代表參數的值) 的 Amazon S3 URL 取得。自訂參數也可以使用 'Key' 和 'Value' (而不是 'ParameterKey' 和 'ParameterValue') 來指定。如果建立堆疊失敗，則會還原 (與指定 -DisableRollback \$1false 相同)。指定的通知 AEN 會收到已發佈的堆疊相關事件。**  

```
New-CFNStack -StackName "myStack" `
             -TemplateURL https://s3.amazonaws.com/amzn-s3-demo-bucket/templatefile.template `
             -Parameter @{ ParameterKey="PK1"; ParameterValue="PV1" } `
             -NotificationARN @( "arn1", "arn2" )
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V5)》**中的 [CreateStack](https://docs.aws.amazon.com/powershell/v5/reference)。

------

**注意**  
您可以使用 AWS CLI `--template-url`選項，在 Amazon S3 或 中指定範本檔案位置 AWS Systems Manager。  
對於 Amazon S3，URL 必須以 `https://` 開頭。不支援 S3 靜態網站 URL。  

```
--template-url https://s3.region-code.amazonaws.com/bucket-name/template-name
```
對於 AWS Systems Manager，請使用下列格式：  

```
--template-url "ssm-doc://arn:aws:ssm:region-code:account-id:document/document-name"
```

## 建立包含轉換的堆疊
<a name="deploy-sdk"></a>

使用 [https://docs.aws.amazon.com/cli/latest/reference/cloudformation/deploy/](https://docs.aws.amazon.com/cli/latest/reference/cloudformation/deploy/) 命令來建立包含轉換的堆疊。當您從包含轉換的範本建立堆疊時，必須使用變更集。`deploy` 命令會將兩個步驟 (建立變更集並執行) 合併為單一命令。

------
#### [ AWS CLI ]

下列 `deploy` 命令會建立具有指定名稱、範本和輸入參數的堆疊。

```
aws cloudformation deploy --stack-name myteststack \
  --template /path_to_template/my-template.json \
  --parameter-overrides Key1=Value1 Key2=Value2
```

------

## 刪除堆疊
<a name="delete-stack-sdk"></a>

使用 [https://docs.aws.amazon.com/cli/latest/reference/cloudformation/delete-stack.html](https://docs.aws.amazon.com/cli/latest/reference/cloudformation/delete-stack.html) 命令來刪除堆疊。如需詳細資訊，請參閱[刪除堆疊](cfn-console-delete-stack.md)。

------
#### [ CLI ]

**AWS CLI**  
**刪除堆疊**  
以下 `delete-stack` 範例會刪除指定的堆疊。  

```
aws cloudformation delete-stack \
    --stack-name my-stack
```
此命令不會產生輸出。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [DeleteStack](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/cloudformation/delete-stack.html)。

------
#### [ PowerShell ]

**Tools for PowerShell V4**  
**範例 1：刪除指定的堆疊。**  

```
Remove-CFNStack -StackName "myStack"
```
+  如需 API 詳細資訊，請參閱《*AWS Tools for PowerShell Cmdlet 參考 (V4)*》中的 [DeleteStack](https://docs.aws.amazon.com/powershell/v4/reference)。

**Tools for PowerShell V5**  
**範例 1：刪除指定的堆疊。**  

```
Remove-CFNStack -StackName "myStack"
```
+  如需 API 詳細資訊，請參閱《*AWS Tools for PowerShell Cmdlet 參考 (V5)*》中的 [DeleteStack](https://docs.aws.amazon.com/powershell/v5/reference)。

------

若刪除失敗並傳回 `DELETE_FAILED` 狀態，您可選擇使用 `--deletion-mode` 選項強制刪除堆疊。下列是可與 `deletion-mode` 搭配使用的值：
+ `STANDARD`：正常刪除堆疊。這是預設刪除模式。
+ `FORCE_DELETE_STACK`：刪除堆疊並略過所有無法刪除的資源。

------
#### [ AWS CLI ]

下列 `delete-stack` 命令強制使用 `FORCE_DELETE_STACK` 值搭配 `deletion-mode` 參數刪除 `myteststack` 堆疊：

```
aws cloudformation delete-stack --stack-name myteststack \
    --deletion-mode FORCE_DELETE_STACK
```

此命令不會產生輸出。

------

使用 `FORCE_DELETE_STACK` 之後，您可使用 `list-stack-resources` 命令列出在堆疊刪除程序期間略過的資源。保留的資源會顯示 DELETE\$1SKIPPED 狀態。如需詳細資訊，請參閱[列出堆疊資源](#list-stack-resources-sdk)。

## 描述堆疊事件
<a name="describe-stack-events-sdk"></a>

使用 [https://docs.aws.amazon.com/cli/latest/reference/cloudformation/describe-stack-events.html](https://docs.aws.amazon.com/cli/latest/reference/cloudformation/describe-stack-events.html) 命令來描述堆疊事件。如需詳細資訊，請參閱[監控堆疊進度](monitor-stack-progress.md)。

------
#### [ CLI ]

**AWS CLI**  
**描述堆疊事件**  
下列 `describe-stack-events` 範例顯示指定堆疊的 2 個最近事件。  

```
aws cloudformation describe-stack-events \
    --stack-name my-stack \
    --max-items 2

{
    "StackEvents": [
        {
            "StackId": "arn:aws:cloudformation:us-west-2:123456789012:stack/my-stack/d0a825a0-e4cd-xmpl-b9fb-061c69e99204",
            "EventId": "4e1516d0-e4d6-xmpl-b94f-0a51958a168c",
            "StackName": "my-stack",
            "LogicalResourceId": "my-stack",
            "PhysicalResourceId": "arn:aws:cloudformation:us-west-2:123456789012:stack/my-stack/d0a825a0-e4cd-xmpl-b9fb-061c69e99204",
            "ResourceType": "AWS::CloudFormation::Stack",
            "Timestamp": "2019-10-02T05:34:29.556Z",
            "ResourceStatus": "UPDATE_COMPLETE"
        },
        {
            "StackId": "arn:aws:cloudformation:us-west-2:123456789012:stack/my-stack/d0a825a0-e4cd-xmpl-b9fb-061c69e99204",
            "EventId": "4dd3c810-e4d6-xmpl-bade-0aaf8b31ab7a",
            "StackName": "my-stack",
            "LogicalResourceId": "my-stack",
            "PhysicalResourceId": "arn:aws:cloudformation:us-west-2:123456789012:stack/my-stack/d0a825a0-e4cd-xmpl-b9fb-061c69e99204",
            "ResourceType": "AWS::CloudFormation::Stack",
            "Timestamp": "2019-10-02T05:34:29.127Z",
            "ResourceStatus": "UPDATE_COMPLETE_CLEANUP_IN_PROGRESS"
        }
    ],
    "NextToken": "eyJOZXh0VG9XMPLiOiBudWxsLCAiYm90b190cnVuY2F0ZV9hbW91bnQiOiAyfQ=="
}
```
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [DescribeStackEvents](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/cloudformation/describe-stack-events.html)。

------
#### [ PowerShell ]

**Tools for PowerShell V4**  
**範例 1：傳回特定堆疊的所有堆疊相關事件。**  

```
Get-CFNStackEvent -StackName "myStack"
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [DescribeStackEvents](https://docs.aws.amazon.com/powershell/v4/reference)。

**Tools for PowerShell V5**  
**範例 1：傳回特定堆疊的所有堆疊相關事件。**  

```
Get-CFNStackEvent -StackName "myStack"
```
+  如需 API 詳細資訊，請參閱《*AWS Tools for PowerShell Cmdlet 參考 (V5)*》中的 [DescribeStackEvents](https://docs.aws.amazon.com/powershell/v5/reference)。

------

## 描述堆疊資源
<a name="describe-stack-resource-sdk"></a>

------
#### [ CLI ]

**AWS CLI**  
**取得堆疊資源的相關資訊**  
下列 `describe-stack-resource` 範例顯示指定的堆疊中名為 `MyFunction` 的資源詳細資訊。  

```
aws cloudformation describe-stack-resource \
    --stack-name MyStack \
    --logical-resource-id MyFunction
```
輸出：  

```
{
    "StackResourceDetail": {
        "StackName": "MyStack",
        "StackId": "arn:aws:cloudformation:us-east-2:123456789012:stack/MyStack/d0a825a0-e4cd-xmpl-b9fb-061c69e99204",
        "LogicalResourceId": "MyFunction",
        "PhysicalResourceId": "my-function-SEZV4XMPL4S5",
        "ResourceType": "AWS::Lambda::Function",
        "LastUpdatedTimestamp": "2019-10-02T05:34:27.989Z",
        "ResourceStatus": "UPDATE_COMPLETE",
        "Metadata": "{}",
        "DriftInformation": {
            "StackResourceDriftStatus": "IN_SYNC"
        }
    }
}
```
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [DescribeStackResource](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/cloudformation/describe-stack-resource.html)。

------
#### [ PowerShell ]

**Tools for PowerShell V4**  
**範例 1：傳回透過邏輯 ID "MyDBInstance" 在與指定堆疊相關聯的範本中識別的資源描述。**  

```
Get-CFNStackResource -StackName "myStack" -LogicalResourceId "MyDBInstance"
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [DescribeStackResource](https://docs.aws.amazon.com/powershell/v4/reference)。

**Tools for PowerShell V5**  
**範例 1：傳回透過邏輯 ID "MyDBInstance" 在與指定堆疊相關聯的範本中識別的資源描述。**  

```
Get-CFNStackResource -StackName "myStack" -LogicalResourceId "MyDBInstance"
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V5)》**中的 [DescribeStackResource](https://docs.aws.amazon.com/powershell/v5/reference)。

------

## 描述堆疊資源
<a name="describe-stack-resources-sdk"></a>

------
#### [ CLI ]

**AWS CLI**  
**取得堆疊資源的相關資訊**  
下列 `describe-stack-resources` 範例顯示指定之堆疊中的資源詳細資訊。  

```
aws cloudformation describe-stack-resources \
    --stack-name my-stack
```
輸出：  

```
{
    "StackResources": [
        {
            "StackName": "my-stack",
            "StackId": "arn:aws:cloudformation:us-west-2:123456789012:stack/my-stack/d0a825a0-e4cd-xmpl-b9fb-061c69e99204",
            "LogicalResourceId": "bucket",
            "PhysicalResourceId": "my-stack-bucket-1vc62xmplgguf",
            "ResourceType": "AWS::S3::Bucket",
            "Timestamp": "2019-10-02T04:34:11.345Z",
            "ResourceStatus": "CREATE_COMPLETE",
            "DriftInformation": {
                "StackResourceDriftStatus": "IN_SYNC"
            }
        },
        {
            "StackName": "my-stack",
            "StackId": "arn:aws:cloudformation:us-west-2:123456789012:stack/my-stack/d0a825a0-e4cd-xmpl-b9fb-061c69e99204",
            "LogicalResourceId": "function",
            "PhysicalResourceId": "my-function-SEZV4XMPL4S5",
            "ResourceType": "AWS::Lambda::Function",
            "Timestamp": "2019-10-02T05:34:27.989Z",
            "ResourceStatus": "UPDATE_COMPLETE",
            "DriftInformation": {
                "StackResourceDriftStatus": "IN_SYNC"
            }
        },
        {
            "StackName": "my-stack",
            "StackId": "arn:aws:cloudformation:us-west-2:123456789012:stack/my-stack/d0a825a0-e4cd-xmpl-b9fb-061c69e99204",
            "LogicalResourceId": "functionRole",
            "PhysicalResourceId": "my-functionRole-HIZXMPLEOM9E",
            "ResourceType": "AWS::IAM::Role",
            "Timestamp": "2019-10-02T04:34:06.350Z",
            "ResourceStatus": "CREATE_COMPLETE",
            "DriftInformation": {
                "StackResourceDriftStatus": "IN_SYNC"
            }
        }
    ]
}
```
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [DescribeStackResources](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/cloudformation/describe-stack-resources.html)。

------
#### [ PowerShell ]

**Tools for PowerShell V4**  
**範例 1：傳回最多 100 個與指定堆疊相關聯的資源 AWS 描述。若要取得與堆疊相關聯的所有資源詳細資訊，請使用 Get-CFNStackResourceSummary，這也支援手動分頁結果。**  

```
Get-CFNStackResourceList -StackName "myStack"
```
**範例 2：傳回邏輯 ID "Ec2Instance" 在與指定堆疊相關聯的範本中識別的 Amazon EC2 執行個體描述。**  

```
Get-CFNStackResourceList -StackName "myStack" -LogicalResourceId "Ec2Instance"
```
**範例 3：傳回最多 100 個與堆疊相關聯的資源描述，其中包含由執行個體 ID "i-123456" 識別的 Amazon EC2 執行個體。若要取得與堆疊相關聯的所有資源詳細資訊，請使用 Get-CFNStackResourceSummary，這也支援手動分頁結果。**  

```
Get-CFNStackResourceList -PhysicalResourceId "i-123456"
```
**範例 4：傳回堆疊範本中邏輯 ID "Ec2Instance" 所識別的 Amazon EC2 執行個體描述。堆疊是使用其包含的資源的實體資源 ID 來識別，在此情況下，也會使用執行個體 ID 為 "i-123456" 的 Amazon EC2 執行個體。根據範本內容，也可以使用不同的實體資源來識別堆疊，例如 Amazon S3 儲存貯體。**  

```
Get-CFNStackResourceList -PhysicalResourceId "i-123456" -LogicalResourceId "Ec2Instance"
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [DescribeStackResources](https://docs.aws.amazon.com/powershell/v4/reference)。

**Tools for PowerShell V5**  
**範例 1：傳回最多 100 個與指定堆疊相關聯的資源 AWS 描述。若要取得與堆疊相關聯的所有資源詳細資訊，請使用 Get-CFNStackResourceSummary，這也支援手動分頁結果。**  

```
Get-CFNStackResourceList -StackName "myStack"
```
**範例 2：傳回邏輯 ID "Ec2Instance" 在與指定堆疊相關聯的範本中識別的 Amazon EC2 執行個體描述。**  

```
Get-CFNStackResourceList -StackName "myStack" -LogicalResourceId "Ec2Instance"
```
**範例 3：傳回最多 100 個與堆疊相關聯的資源描述，其中包含由執行個體 ID "i-123456" 識別的 Amazon EC2 執行個體。若要取得與堆疊相關聯的所有資源詳細資訊，請使用 Get-CFNStackResourceSummary，這也支援手動分頁結果。**  

```
Get-CFNStackResourceList -PhysicalResourceId "i-123456"
```
**範例 4：傳回堆疊範本中邏輯 ID "Ec2Instance" 所識別的 Amazon EC2 執行個體描述。堆疊是使用其包含的資源的實體資源 ID 來識別，在此情況下，也會使用執行個體 ID 為 "i-123456" 的 Amazon EC2 執行個體。根據範本內容，也可以使用不同的實體資源來識別堆疊，例如 Amazon S3 儲存貯體。**  

```
Get-CFNStackResourceList -PhysicalResourceId "i-123456" -LogicalResourceId "Ec2Instance"
```
+  如需 API 詳細資訊，請參閱《*AWS Tools for PowerShell Cmdlet 參考 (V5)*》中的 [DescribeStackResources](https://docs.aws.amazon.com/powershell/v5/reference)。

------

## 描述堆疊
<a name="describe-stacks-sdk"></a>

------
#### [ CLI ]

**AWS CLI**  
**描述 AWS CloudFormation 堆疊**  
以下 `describe-stacks` 範例顯示 `myteststack` 堆疊的摘要資訊：  

```
aws cloudformation describe-stacks --stack-name myteststack
```
輸出：  

```
{
    "Stacks":  [
        {
            "StackId": "arn:aws:cloudformation:us-east-1:123456789012:stack/myteststack/466df9e0-0dff-08e3-8e2f-5088487c4896",
            "Description": "AWS CloudFormation Sample Template S3_Bucket: Sample template showing how to create a publicly accessible S3 bucket. **WARNING** This template creates an S3 bucket. You will be billed for the AWS resources used if you create a stack from this template.",
            "Tags": [],
            "Outputs": [
                {
                    "Description": "Name of S3 bucket to hold website content",
                    "OutputKey": "BucketName",
                    "OutputValue": "myteststack-s3bucket-jssofi1zie2w"
                }
            ],
            "StackStatusReason": null,
            "CreationTime": "2013-08-23T01:02:15.422Z",
            "Capabilities": [],
            "StackName": "myteststack",
            "StackStatus": "CREATE_COMPLETE",
            "DisableRollback": false
        }
    ]
}
```
如需詳細資訊，請參閱《*AWS CloudFormation 使用者指南*》中的堆疊。  
+  如需 API 詳細資訊，請參閱*《AWS CLI 命令參考》*中的 [DescribeStacks](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/cloudformation/describe-stacks.html)。

------
#### [ Go ]

**SDK for Go V2**  
 GitHub 上提供更多範例。尋找完整範例，並了解如何在 [AWS 程式碼範例儲存庫](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/gov2/workflows/user_pools_and_lambda_triggers#code-examples)中設定和執行。

```
import (
	"context"
	"log"

	"github.com/aws/aws-sdk-go-v2/aws"
	"github.com/aws/aws-sdk-go-v2/service/cloudformation"
)

// StackOutputs defines a map of outputs from a specific stack.
type StackOutputs map[string]string

type CloudFormationActions struct {
	CfnClient *cloudformation.Client
}

// GetOutputs gets the outputs from a CloudFormation stack and puts them into a structured format.
func (actor CloudFormationActions) GetOutputs(ctx context.Context, stackName string) StackOutputs {
	output, err := actor.CfnClient.DescribeStacks(ctx, &cloudformation.DescribeStacksInput{
		StackName: aws.String(stackName),
	})
	if err != nil || len(output.Stacks) == 0 {
		log.Panicf("Couldn't find a CloudFormation stack named %v. Here's why: %v\n", stackName, err)
	}
	stackOutputs := StackOutputs{}
	for _, out := range output.Stacks[0].Outputs {
		stackOutputs[*out.OutputKey] = *out.OutputValue
	}
	return stackOutputs
}
```
+  如需 API 詳細資訊，請參閱《適用於 Go 的 AWS SDK  API 參考》**中的 [DescribeStacks](https://pkg.go.dev/github.com/aws/aws-sdk-go-v2/service/cloudformation#Client.DescribeStacks)。

------
#### [ PowerShell ]

**Tools for PowerShell V4**  
**範例 1：傳回描述所有使用者堆疊的堆疊執行個體集合。**  

```
Get-CFNStack
```
**範例 2：傳回描述指定堆疊的堆疊執行個體**  

```
Get-CFNStack -StackName "myStack"
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [DescribeStacks](https://docs.aws.amazon.com/powershell/v4/reference)。

**Tools for PowerShell V5**  
**範例 1：傳回描述所有使用者堆疊的堆疊執行個體集合。**  

```
Get-CFNStack
```
**範例 2：傳回描述指定堆疊的堆疊執行個體**  

```
Get-CFNStack -StackName "myStack"
```
+  如需 API 詳細資訊，請參閱《*AWS Tools for PowerShell Cmdlet 參考 (V5)*》中的 [DescribeStacks](https://docs.aws.amazon.com/powershell/v5/reference)。

------

根據預設，`describe-stacks` 命令會傳回參數值。若要避免傳回敏感參數值，如密碼，請在 CloudFormation 範本中包括 `NoEcho` 屬性，並將其設定為 `TRUE`。

**重要**  
使用 `NoEcho` 屬性不會遮罩任何儲存在下列資訊中的資訊：  
`Metadata` 範本區段。CloudFormation 不會轉換、修改或標記您在 `Metadata` 區段中包含的任何資訊。若要取得更多資訊，請參閱[中繼資料](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/metadata-section-structure.html)。
`Outputs` 範本區段。如需詳細資訊，請參閱[輸出](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/outputs-section-structure.html)。
資源定義的 `Metadata` 屬性。如需詳細資訊，請參閱 [`Metadata` 屬性](https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/aws-attribute-metadata.html)。
我們強烈建議您不要使用這些機制來包含敏感資訊，例如密碼或秘密。

**重要**  
我們建議您不要直接在 CloudFormation 範本中嵌入敏感資訊，而是在堆疊範本中使用動態參數來參考在 CloudFormation 外部存放和管理的敏感資訊，例如在 AWS Systems Manager 參數存放區或 中 AWS Secrets Manager。  
如需詳細資訊，請參閱[請勿在範本中內嵌認證](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/security-best-practices.html#creds)的最佳實務。

## 取得範本
<a name="get-template-sdk"></a>

------
#### [ CLI ]

**AWS CLI**  
**檢視 an AWS CloudFormation 堆疊的範本內文**  
下列 `get-template` 命令示範 `myteststack` 堆疊的範本：  

```
aws cloudformation get-template --stack-name myteststack
```
輸出：  

```
{
    "TemplateBody": {
        "AWSTemplateFormatVersion": "2010-09-09",
        "Outputs": {
            "BucketName": {
                "Description": "Name of S3 bucket to hold website content",
                "Value": {
                    "Ref": "S3Bucket"
                }
            }
        },
        "Description": "AWS CloudFormation Sample Template S3_Bucket: Sample template showing how to create a publicly accessible S3 bucket. **WARNING** This template creates an S3 bucket. You will be billed for the AWS resources used if you create a stack from this template.",
        "Resources": {
            "S3Bucket": {
                "Type": "AWS::S3::Bucket",
                "Properties": {
                    "AccessControl": "PublicRead"
                }
            }
        }
    }
}
```
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [GetTemplate](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/cloudformation/get-template.html)。

------
#### [ PowerShell ]

**Tools for PowerShell V4**  
**範例 1：傳回與指定堆疊相關聯的範本。**  

```
Get-CFNTemplate -StackName "myStack"
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [GetTemplate](https://docs.aws.amazon.com/powershell/v4/reference)。

**Tools for PowerShell V5**  
**範例 1：傳回與指定堆疊相關聯的範本。**  

```
Get-CFNTemplate -StackName "myStack"
```
+  如需 API 詳細資訊，請參閱《*AWS Tools for PowerShell Cmdlet 參考 (V5)*》中的 [GetTemplate](https://docs.aws.amazon.com/powershell/v5/reference)。

------

## 列出堆疊資源
<a name="list-stack-resources-sdk"></a>

------
#### [ CLI ]

**AWS CLI**  
**列出堆疊中的資源**  
下列命令顯示指定堆疊中的資源清單。  

```
aws cloudformation list-stack-resources \
    --stack-name my-stack
```
輸出：  

```
{
    "StackResourceSummaries": [
        {
            "LogicalResourceId": "bucket",
            "PhysicalResourceId": "my-stack-bucket-1vc62xmplgguf",
            "ResourceType": "AWS::S3::Bucket",
            "LastUpdatedTimestamp": "2019-10-02T04:34:11.345Z",
            "ResourceStatus": "CREATE_COMPLETE",
            "DriftInformation": {
                "StackResourceDriftStatus": "IN_SYNC"
            }
        },
        {
            "LogicalResourceId": "function",
            "PhysicalResourceId": "my-function-SEZV4XMPL4S5",
            "ResourceType": "AWS::Lambda::Function",
            "LastUpdatedTimestamp": "2019-10-02T05:34:27.989Z",
            "ResourceStatus": "UPDATE_COMPLETE",
            "DriftInformation": {
                "StackResourceDriftStatus": "IN_SYNC"
            }
        },
        {
            "LogicalResourceId": "functionRole",
            "PhysicalResourceId": "my-functionRole-HIZXMPLEOM9E",
            "ResourceType": "AWS::IAM::Role",
            "LastUpdatedTimestamp": "2019-10-02T04:34:06.350Z",
            "ResourceStatus": "CREATE_COMPLETE",
            "DriftInformation": {
                "StackResourceDriftStatus": "IN_SYNC"
            }
        }
    ]
}
```
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [ListStackResources](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/cloudformation/list-stack-resources.html)。

------
#### [ PowerShell ]

**Tools for PowerShell V4**  
**範例 1：傳回與指定堆疊相關聯的所有資源的描述。**  

```
Get-CFNStackResourceSummary -StackName "myStack"
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [ListStackResources](https://docs.aws.amazon.com/powershell/v4/reference)。

**Tools for PowerShell V5**  
**範例 1：傳回與指定堆疊相關聯的所有資源的描述。**  

```
Get-CFNStackResourceSummary -StackName "myStack"
```
+  如需 API 詳細資訊，請參閱《*AWS Tools for PowerShell Cmdlet 參考 (V5)*》中的 [ListStackResources](https://docs.aws.amazon.com/powershell/v5/reference)。

------

## 列出堆疊
<a name="list-stacks-sdk"></a>

使用 [https://docs.aws.amazon.com/cli/latest/reference/cloudformation/list-stacks.html](https://docs.aws.amazon.com/cli/latest/reference/cloudformation/list-stacks.html) 命令列出堆疊。若要僅列出具有指定狀態碼的堆疊，請包含 `--stack-status-filter` 選項。您可以為 `--stack-status-filter` 選項指定一或多個堆疊狀態碼。如需詳細資訊，請參閱[堆疊狀態碼](view-stack-events.md#cfn-console-view-stack-data-resources-status-codes)。

------
#### [ CLI ]

**AWS CLI**  
**列出 AWS CloudFormation 堆疊**  
以下 `list-stacks` 命令示範狀態為 `CREATE_COMPLETE` 之所有堆疊的摘要：  

```
aws cloudformation list-stacks --stack-status-filter CREATE_COMPLETE
```
輸出：  

```
[
    {
        "StackId": "arn:aws:cloudformation:us-east-1:123456789012:stack/myteststack/466df9e0-0dff-08e3-8e2f-5088487c4896",
        "TemplateDescription": "AWS CloudFormation Sample Template S3_Bucket: Sample template showing how to create a publicly accessible S3 bucket. **WARNING** This template creates an S3 bucket. You will be billed for the AWS resources used if you create a stack from this template.",
        "StackStatusReason": null,
        "CreationTime": "2013-08-26T03:27:10.190Z",
        "StackName": "myteststack",
        "StackStatus": "CREATE_COMPLETE"
    }
]
```
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [ListStacks](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/cloudformation/list-stacks.html)。

------
#### [ PowerShell ]

**Tools for PowerShell V4**  
**範例 1：傳回所有堆疊的摘要資訊。**  

```
Get-CFNStackSummary
```
**範例 2：傳回目前正在建立之所有堆疊的摘要資訊。**  

```
Get-CFNStackSummary -StackStatusFilter "CREATE_IN_PROGRESS"
```
**範例 3：傳回目前正在建立或更新之所有堆疊的摘要資訊。**  

```
Get-CFNStackSummary -StackStatusFilter @("CREATE_IN_PROGRESS", "UPDATE_IN_PROGRESS")
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [ListStacks](https://docs.aws.amazon.com/powershell/v4/reference)。

**Tools for PowerShell V5**  
**範例 1：傳回所有堆疊的摘要資訊。**  

```
Get-CFNStackSummary
```
**範例 2：傳回目前正在建立之所有堆疊的摘要資訊。**  

```
Get-CFNStackSummary -StackStatusFilter "CREATE_IN_PROGRESS"
```
**範例 3：傳回目前正在建立或更新之所有堆疊的摘要資訊。**  

```
Get-CFNStackSummary -StackStatusFilter @("CREATE_IN_PROGRESS", "UPDATE_IN_PROGRESS")
```
+  如需 API 詳細資訊，請參閱《*AWS Tools for PowerShell Cmdlet 參考 (V5)*》中的 [ListStacks](https://docs.aws.amazon.com/powershell/v5/reference)。

------

## 更新堆疊
<a name="update-stack-sdk"></a>

使用 [https://docs.aws.amazon.com/cli/latest/reference/cloudformation/update-stack.html](https://docs.aws.amazon.com/cli/latest/reference/cloudformation/update-stack.html) 命令直接更新堆疊。您可以指定要更新的堆疊、參數值和功能；如果您希望使用已更新的範本，也可以指定範本的名稱。如需詳細資訊，請參閱[直接更新堆疊](using-cfn-updating-stacks-direct.md)。

------
#### [ CLI ]

**AWS CLI**  
**更新 AWS CloudFormation 堆疊**  
以下 `update-stack` 命令更新 `mystack` 堆疊的範本和輸入參數：  

```
aws cloudformation update-stack --stack-name mystack --template-url https://s3.amazonaws.com/sample/updated.template --parameters ParameterKey=KeyPairName,ParameterValue=SampleKeyPair ParameterKey=SubnetIDs,ParameterValue=SampleSubnetID1\\,SampleSubnetID2
```
以下 `update-stack` 命令僅更新 `mystack` 堆疊的 `SubnetIDs` 參數值。如果您未指定參數值，則會使用範本中所指定的預設值：  

```
aws cloudformation update-stack --stack-name mystack --template-url https://s3.amazonaws.com/sample/updated.template --parameters ParameterKey=KeyPairName,UsePreviousValue=true ParameterKey=SubnetIDs,ParameterValue=SampleSubnetID1\\,UpdatedSampleSubnetID2
```
以下 `update-stack` 命令會將兩個堆疊通知主題新增至 `mystack` 堆疊：  

```
aws cloudformation update-stack --stack-name mystack --use-previous-template --notification-arns "arn:aws:sns:use-east-1:123456789012:mytopic1" "arn:aws:sns:us-east-1:123456789012:mytopic2"
```
如需詳細資訊，請參閱《*AWS CloudFormation 使用者指南*》中的 [AWS CloudFormation 堆疊更新](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/using-cfn-updating-stacks.html)。  
+  如需 API 詳細資訊，請參閱《AWS CLI 命令參考》**中的 [UpdateStack](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/cloudformation/update-stack.html)。

------
#### [ PowerShell ]

**Tools for PowerShell V4**  
**範例 1：使用指定的範本和自訂參數更新堆疊 'myStack'。'PK1' 代表範本中宣告的參數名稱，而 'PV1' 代表其值。自訂參數也可以使用 'Key' 和 'Value' (而不是 'ParameterKey' 和 'ParameterValue') 來指定。**  

```
Update-CFNStack -StackName "myStack" `
                -TemplateBody "{Template Content Here}" `
                -Parameter @{ ParameterKey="PK1"; ParameterValue="PV1" }
```
**範例 2：使用指定的範本和自訂參數更新堆疊 'myStack'。'PK1' 和 'PK2' 代表範本中宣告的參數名稱，'PV1' 和 'PV2' 代表其請求的值。自訂參數也可以使用 'Key' 和 'Value' (而不是 'ParameterKey' 和 'ParameterValue') 來指定。**  

```
Update-CFNStack -StackName "myStack" `
                -TemplateBody "{Template Content Here}" `
                -Parameter @( @{ ParameterKey="PK1"; ParameterValue="PV1" }, @{ ParameterKey="PK2"; ParameterValue="PV2" } )
```
**範例 3：使用指定的範本和自訂參數更新堆疊 'myStack'。'PK1' 代表範本中宣告的參數名稱，而 'PV2' 代表其值。自訂參數也可以使用 'Key' 和 'Value' (而不是 'ParameterKey' 和 'ParameterValue') 來指定。**  

```
Update-CFNStack -StackName "myStack" -TemplateBody "{Template Content Here}" -Parameters @{ ParameterKey="PK1"; ParameterValue="PV1" }
```
**範例 4：使用從 Amazon S3 取得的指定範本和自訂參數來更新堆疊 'myStack'。'PK1' 和 'PK2' 代表範本中宣告的參數名稱，'PV1' 和 'PV2' 代表其請求的值。自訂參數也可以使用 'Key' 和 'Value' (而不是 'ParameterKey' 和 'ParameterValue') 來指定。**  

```
Update-CFNStack -StackName "myStack" `
                -TemplateURL https://s3.amazonaws.com/amzn-s3-demo-bucket/templatefile.template `
                -Parameter @( @{ ParameterKey="PK1"; ParameterValue="PV1" }, @{ ParameterKey="PK2"; ParameterValue="PV2" } )
```
**範例 5：更新堆疊 'myStack'，在本範例中假設包含 IAM 資源，具有從 Amazon S3 取得的指定範本和自訂參數。'PK1' 和 'PK2' 代表範本中宣告的參數名稱，'PV1' 和 'PV2' 代表其請求的值。自訂參數也可以使用 'Key' 和 'Value' (而不是 'ParameterKey' 和 'ParameterValue') 來指定。包含 IAM 資源的堆疊需要指定 -Capabilities "CAPABILITY\$1IAM" 參數，否則更新會失敗，並顯示 'InsufficientCapabilities' 錯誤。**  

```
Update-CFNStack -StackName "myStack" `
                -TemplateURL https://s3.amazonaws.com/amzn-s3-demo-bucket/templatefile.template `
                -Parameter @( @{ ParameterKey="PK1"; ParameterValue="PV1" }, @{ ParameterKey="PK2"; ParameterValue="PV2" } ) `
                -Capabilities "CAPABILITY_IAM"
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V4)》**中的 [UpdateStack](https://docs.aws.amazon.com/powershell/v4/reference)。

**Tools for PowerShell V5**  
**範例 1：使用指定的範本和自訂參數更新堆疊 'myStack'。'PK1' 代表範本中宣告的參數名稱，而 'PV1' 代表其值。自訂參數也可以使用 'Key' 和 'Value' (而不是 'ParameterKey' 和 'ParameterValue') 來指定。**  

```
Update-CFNStack -StackName "myStack" `
                -TemplateBody "{Template Content Here}" `
                -Parameter @{ ParameterKey="PK1"; ParameterValue="PV1" }
```
**範例 2：使用指定的範本和自訂參數更新堆疊 'myStack'。'PK1' 和 'PK2' 代表範本中宣告的參數名稱，'PV1' 和 'PV2' 代表其請求的值。自訂參數也可以使用 'Key' 和 'Value' (而不是 'ParameterKey' 和 'ParameterValue') 來指定。**  

```
Update-CFNStack -StackName "myStack" `
                -TemplateBody "{Template Content Here}" `
                -Parameter @( @{ ParameterKey="PK1"; ParameterValue="PV1" }, @{ ParameterKey="PK2"; ParameterValue="PV2" } )
```
**範例 3：使用指定的範本和自訂參數更新堆疊 'myStack'。'PK1' 代表範本中宣告的參數名稱，而 'PV2' 代表其值。自訂參數也可以使用 'Key' 和 'Value' (而不是 'ParameterKey' 和 'ParameterValue') 來指定。**  

```
Update-CFNStack -StackName "myStack" -TemplateBody "{Template Content Here}" -Parameters @{ ParameterKey="PK1"; ParameterValue="PV1" }
```
**範例 4：使用從 Amazon S3 取得的指定範本和自訂參數來更新堆疊 'myStack'。'PK1' 和 'PK2' 代表範本中宣告的參數名稱，'PV1' 和 'PV2' 代表其請求的值。自訂參數也可以使用 'Key' 和 'Value' (而不是 'ParameterKey' 和 'ParameterValue') 來指定。**  

```
Update-CFNStack -StackName "myStack" `
                -TemplateURL https://s3.amazonaws.com/amzn-s3-demo-bucket/templatefile.template `
                -Parameter @( @{ ParameterKey="PK1"; ParameterValue="PV1" }, @{ ParameterKey="PK2"; ParameterValue="PV2" } )
```
**範例 5：更新堆疊 'myStack'，在本範例中假設包含 IAM 資源，具有從 Amazon S3 取得的指定範本和自訂參數。'PK1' 和 'PK2' 代表範本中宣告的參數名稱，'PV1' 和 'PV2' 代表其請求的值。自訂參數也可以使用 'Key' 和 'Value' (而不是 'ParameterKey' 和 'ParameterValue') 來指定。包含 IAM 資源的堆疊需要指定 -Capabilities "CAPABILITY\$1IAM" 參數，否則更新會失敗，並顯示 'InsufficientCapabilities' 錯誤。**  

```
Update-CFNStack -StackName "myStack" `
                -TemplateURL https://s3.amazonaws.com/amzn-s3-demo-bucket/templatefile.template `
                -Parameter @( @{ ParameterKey="PK1"; ParameterValue="PV1" }, @{ ParameterKey="PK2"; ParameterValue="PV2" } ) `
                -Capabilities "CAPABILITY_IAM"
```
+  如需 API 詳細資訊，請參閱《*AWS Tools for PowerShell Cmdlet 參考 (V5)*》中的 [UpdateStack](https://docs.aws.amazon.com/powershell/v5/reference)。

------

**注意**  
要移除所有通知，為 `--notification-arns` 選項指定 `[]`。

## 驗證您的範本
<a name="validate-template-sdk"></a>

使用 [https://docs.aws.amazon.com/cli/latest/reference/cloudformation/validate-template.html](https://docs.aws.amazon.com/cli/latest/reference/cloudformation/validate-template.html) 命令檢查您的範本檔案中的語法錯誤。

在驗證期間，CloudFormation 會先檢查範本是否為有效的 JSON。如果不是，CloudFormation 會檢查範本是否為有效的 YAML。如果兩種檢查都失敗，則 CloudFormation 會傳回範本驗證錯誤。

------
#### [ CLI ]

**AWS CLI**  
**驗證 an AWS CloudFormation 範本**  
下列 `validate-template` 命令可驗證 `sampletemplate.json` 範本：  

```
aws cloudformation validate-template --template-body file://sampletemplate.json
```
輸出：  

```
{
    "Description": "AWS CloudFormation Sample Template S3_Bucket: Sample template showing how to create a publicly accessible S3 bucket. **WARNING** This template creates an S3 bucket. You will be billed for the AWS resources used if you create a stack from this template.",
    "Parameters": [],
    "Capabilities": []
}
```
如需詳細資訊，請參閱《 AWS CloudFormation *AWS 使用者指南》中的使用 CloudFormation *範本。  
+  如需 API 詳細資訊，請參閱《*AWS CLI 命令參考*》中的 [ValidateTemplate](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/cloudformation/validate-template.html)。

------
#### [ PowerShell ]

**Tools for PowerShell V4**  
**範例 1：驗證指定的範本內容。輸出中詳細說明範本的功能、描述和參數。**  

```
Test-CFNTemplate -TemplateBody "{TEMPLATE CONTENT HERE}"
```
**範例 2：驗證透過 Amazon S3 URL 存取的指定範本。輸出中詳細說明範本的功能、描述和參數。**  

```
Test-CFNTemplate -TemplateURL https://s3.amazonaws.com/amzn-s3-demo-bucket/templatefile.template
```
+  如需 API 詳細資訊，請參閱《*AWS Tools for PowerShell Cmdlet 參考 (V4)*》中的 [ValidateTemplate](https://docs.aws.amazon.com/powershell/v4/reference)。

**Tools for PowerShell V5**  
**範例 1：驗證指定的範本內容。輸出中詳細說明範本的功能、描述和參數。**  

```
Test-CFNTemplate -TemplateBody "{TEMPLATE CONTENT HERE}"
```
**範例 2：驗證透過 Amazon S3 URL 存取的指定範本。輸出中詳細說明範本的功能、描述和參數。**  

```
Test-CFNTemplate -TemplateURL https://s3.amazonaws.com/amzn-s3-demo-bucket/templatefile.template
```
+  如需 API 詳細資訊，請參閱《AWS Tools for PowerShell Cmdlet 參考 (V5)》**中的 [ValidateTemplate](https://docs.aws.amazon.com/powershell/v5/reference)。

------

下列為產生驗證錯誤的回應範例。

```
{
    "ResponseMetadata": {
        "RequestId": "4ae33ec0-1988-11e3-818b-e15a6df955cd"
    },
    "Errors": [
        {
            "Message": "Template format error: JSON not well-formed. (line 11, column 8)",
            "Code": "ValidationError",
            "Type": "Sender"
        }
    ],
    "Capabilities": [],
    "Parameters": []
}
A client error (ValidationError) occurred: Template format error: JSON not well-formed. (line 11, column 8)
```

**注意**  
此 `validate-template` 命令旨在僅檢查範本的語法。它不能確保您為資源指定的屬性值對該資源有效。它也不能判斷建立堆疊時將存在的資源數量。  
您需要嘗試建立堆疊才能檢查操作有效性。 CloudFormation 堆疊沒有沙盒或測試區域，因此您需要為測試期間建立的資源付費。

**可用性範例**  
找不到所需的內容嗎？ 請使用本頁面底部的**提供意見回饋**連結申請新版範例。

# 使用 將本機成品上傳至 S3 儲存貯體 AWS CLI
<a name="using-cfn-cli-package"></a>

您可以使用 AWS CLI 將 CloudFormation 範本參考的本機成品上傳至 Amazon S3 儲存貯體。本機成品指的是您在範本中參考的檔案。您不需要手動上傳檔案到 S3 儲存貯體，然後將其位置新增到您的範本，您可以在您的範本中指定本機成品，然後使用 [https://docs.aws.amazon.com/cli/latest/reference/cloudformation/package.html](https://docs.aws.amazon.com/cli/latest/reference/cloudformation/package.html) 命令來快速上傳。

本機成品是 **package** 命令上傳至 Amazon S3 的檔案或資料夾的路徑。例如，成品可以是 AWS Lambda 函數原始碼的本機路徑或 Amazon API Gateway REST API OpenAPI 檔案。

使用 **package** 命令時：
+ 如果您指定檔案，命令會將它直接上傳至 S3 儲存貯體。
+ 如果您指定資料夾，命令將建立該資料夾的 `.zip` 檔案，然後上傳 `.zip` 檔案。
+ 如果您不指定路徑，命令將建立工作目錄的 `.zip` 檔案，然後上傳。

您可以指定絕對或相對路徑，相對路徑相對於範本的位置。

上傳成品之後，命令會傳回範本的副本，以命令上傳成品的 S3 位置取代本機成品的參考。然後，您可以使用傳回的範本來建立或更新堆疊。

**注意**  
您只能在 **package** 命令支援的資源屬性中使用本機成品。如需有關此命令和受支援資源屬性清單的詳細資訊，請參閱 [AWS CLI 命令參考](https://docs.aws.amazon.com/cli/latest/reference/cloudformation/index.html)中的 [https://docs.aws.amazon.com/cli/latest/reference/cloudformation/package.html](https://docs.aws.amazon.com/cli/latest/reference/cloudformation/package.html) 文件。



## 先決條件
<a name="using-cfn-cli-package-prerequisites"></a>

開始之前，您必須具備現有的 Amazon S3 儲存貯體。

## 封裝並部署包含本機成品的範本
<a name="package-and-deploy-a-template-with-local-artifacts"></a>

以下範本指定用於 Lambda 函數原始程式碼的本機成品。原始程式碼存放在 `/home/user/code/lambdafunction` 資料夾中。

**原始範本**

```
{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Transform": "AWS::Serverless-2016-10-31",
  "Resources": {
    "MyFunction": {
      "Type": "AWS::Serverless::Function",
      "Properties": {
        "Handler": "index.handler",
        "Runtime": "nodejs18.x",
        "CodeUri": "/home/user/code/lambdafunction"
      }
    }
  }
}
```

下列 [https://docs.aws.amazon.com/cli/latest/reference/cloudformation/package.html](https://docs.aws.amazon.com/cli/latest/reference/cloudformation/package.html) 命令會建立函數原始碼資料夾的 `.zip` 檔案，並將其上傳至指定儲存貯體的根目錄。

```
aws cloudformation package \
  --s3-bucket amzn-s3-demo-bucket \
  --template /path_to_template/template.json \
  --output-template-file packaged-template.json \
  --output json
```

命令會在 `--output-template-file` 指定的路徑上產生新的範本。如下所示，它會將成品參考取代為 Amazon S3 位置。`.zip` 檔案會使用資料夾內容的 MD5 檢查總和來命名，而非資料夾本身的名稱。

**產生範本**

```
{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Transform": "AWS::Serverless-2016-10-31",
  "Resources": {
    "MyFunction": {
      "Type": "AWS::Serverless::Function",
      "Properties": {
        "Handler": "index.handler",
        "Runtime": "nodejs18.x",
        "CodeUri": "s3://amzn-s3-demo-bucket/md5 checksum"
      }
    }
  }
}
```

封裝範本的成品後，請使用 [https://docs.aws.amazon.com/cli/latest/reference/cloudformation/deploy/](https://docs.aws.amazon.com/cli/latest/reference/cloudformation/deploy/) 命令部署已處理的範本。

```
aws cloudformation deploy \
  --template-file packaged-template.json \
  --stack-name stack-name
```

部署超過 51,200 位元組的範本時，請搭配 `--s3-bucket` 選項使用 **deploy** 命令將範本上傳至 S3，如下列範例所示。

```
aws cloudformation deploy \
  --template-file packaged-template.json \
  --stack-name stack-name \
  --s3-bucket amzn-s3-demo-bucket
```

**注意**  
如需使用 **package** 命令上傳本機成品的另一個範例，請參閱 [運用巢狀堆疊，將範本分割成可重複使用的部分](using-cfn-nested-stacks.md)。