

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

# 巢狀堆疊變更集
<a name="change-sets-for-nested-stacks"></a>

透過*巢狀堆疊變更集*，您可以預覽整個巢狀堆疊階層中對應用程式和基礎設施資源所做的變更，並在確認所有變更都如預期時繼續進行更新。

如需有關巢狀堆疊變更集的詳細資訊，請參閱下列章節：

**Topics**
+ [變更集和巢狀堆疊的概觀](#overview-of-change-sets-and-nested-stacks)
+ [使用巢狀堆疊的變更集 (主控台)](#change-sets-for-nested-stacks-console)
+ [使用巢狀堆疊的變更集 (AWS CLI)](#change-sets-for-nested-stacks-cli)

## 變更集和巢狀堆疊的概觀
<a name="overview-of-change-sets-and-nested-stacks"></a>

巢狀堆疊的變更集合將下列功能結合在一起，以將預覽變更的範圍擴展至整個堆疊階層：
+ *變更集*是一種 CloudFormation 功能，可預覽對堆疊的提議變更將如何影響現有或新建立的資源。建立變更集時，透過將堆疊與您提交之資源的變更相比較，CloudFormation 會提供提議變更的清單。如需變更集的詳細資訊，請參閱[透過變更集更新 CloudFormation 堆疊](using-cfn-updating-stacks-changesets.md)。
+ *巢狀堆疊*是建立為另一個堆疊一部分的堆疊。例如，您可能在一個巢狀堆疊中擁有聯網和安全性相關資源，而在另一個巢狀堆疊中擁有應用程式資源。透過這種方式來分割應用程式模型，有助於提升程式碼的可維護性和重複使用性。如需巢狀堆疊的詳細資訊，請參閱[運用巢狀堆疊，將範本分割成可重複使用的部分](using-cfn-nested-stacks.md)。

## 使用巢狀堆疊的變更集 (主控台)
<a name="change-sets-for-nested-stacks-console"></a>
+ **建立變更集** – 透過從堆疊階層的任何層級提交變更來建立變更集。您可以提交修改後的堆疊範本或修改後的輸入參數值，而 CloudFormation 會將巢狀堆疊與提交的變更進行比較，以產生變更集。在 CloudFormation 主控台中預設會啟用巢狀堆疊的變更集。如需詳細資訊，請參閱[建立 CloudFormation 堆疊的變更集](using-cfn-updating-stacks-changesets-create.md)。  
![根據預設，Enabled (已啟用) 建立巢狀堆疊變更集。](http://docs.aws.amazon.com/zh_tw/AWSCloudFormation/latest/UserGuide/images/change-sets-for-nested-sets-enabled-default.png)
**注意**  
根變更集是與建立整個變更集階層的堆疊相關聯的變更集。您必須從根變更集中執行或刪除巢狀堆疊的變更集。如需詳細資訊，請參閱[對巢狀堆疊執行堆疊操作](using-cfn-nested-stacks.md#perform-stack-operations-on-nested-stacks)。
+ **檢視變更集** – 在執行巢狀堆疊之前，以視覺方式呈現對巢狀堆疊內資源的變更。您可以在變更集的 **Changes (變更)** 區段中檢視提議的變更，方法是瀏覽目前的堆疊及其巢狀變更集。如需詳細資訊，請參閱[檢視 CloudFormation 堆疊的變更集](using-cfn-updating-stacks-changesets-view.md)。
+ **執行變更集** – 執行變更集中所述的變更，這些變更都與目前的堆疊及其子代相關。必須透過根變更集進行執行操作。如需詳細資訊，請參閱[執行 CloudFormation 堆疊的變更集堆疊政策與執行變更集](using-cfn-updating-stacks-changesets-execute.md)。
+ **刪除變更集** – 將變更集從目前堆疊移除。刪除變更集可防止自己或其他使用者意外起始不應套用的變更集。必須透過根變更集進行刪除操作。如需詳細資訊，請參閱[刪除 CloudFormation 堆疊的變更集](using-cfn-updating-stacks-changesets-delete.md)。

## 使用巢狀堆疊的變更集 (AWS CLI)
<a name="change-sets-for-nested-stacks-cli"></a>

### create-change-set
<a name="working-with-change-sets-for-nested-stacks-cli"></a>
+ [https://docs.aws.amazon.com/cli/latest/reference/cloudformation/create-change-set.html](https://docs.aws.amazon.com/cli/latest/reference/cloudformation/create-change-set.html) – 根據預設，不會為 AWS CLI啟用巢狀堆疊的變更集。若要為整個堆疊階層建立變更集，請指定 `--include-nested-stacks` 選項。如需詳細資訊，請參閱[建立 CloudFormation 堆疊的變更集](using-cfn-updating-stacks-changesets-create.md)。

下列 AWS CLI 範例會為指定的根堆疊建立變更集。

```
aws cloudformation create-change-set \
    --stack-name {{my-root-stack}} \
    --change-set-name {{my-root-stack-change-set}} \
    --template-body {{file://template.yaml}} \
    --capabilities CAPABILITY_IAM \
    --include-nested-stacks
```

以下為範例輸出。

```
{
    "Id":"arn:aws:cloudformation:us-west-2:123456789012:changeSet/{{my-root-stack-change-set}}/4eca1a01-e285-xmpl-8026-9a1967bfb4b0",
    "StackId": "arn:aws:cloudformation:us-west-2:123456789012:Stack/{{my-root-stack}}/d0a825a0-e4cd-xmpl-b9fb-061c69e99204"
}
```

### 描述變更集
<a name="working-with-change-set-describe-cli"></a>
+ [https://docs.aws.amazon.com/cli/latest/reference/cloudformation/describe-change-set.html](https://docs.aws.amazon.com/cli/latest/reference/cloudformation/describe-change-set.html) – 傳回如果您執行變更集，CloudFormation 將進行的變更清單。如果指定的變更集包含屬於巢狀堆疊的子變更集，則 `ChangeSetId` 會傳回與該變更集相關的資訊。如需詳細資訊，請參閱[檢視 CloudFormation 堆疊的變更集](using-cfn-updating-stacks-changesets-view.md)。

下列 AWS CLI 範例說明指定根堆疊的變更集。

```
aws cloudformation describe-change-set \
    --change-set-name {{my-root-stack-change-set}} \
    --stack-name {{my-root-stack}}
```

以下為範例輸出。

```
{
    "Changes": [
        {
            "Type": "Resource",
            "ResourceChange": {
                "Action": "Modify",
                "LogicalResourceId": "ChildStack",
                "PhysicalResourceId": "arn:aws:cloudformation:us-west-2:123456789012:stack/{{my-nested-stack}}/d0a825a0-e4cd-xmpl-b9fb-061c69e99205",
                "ResourceType": "AWS::CloudFormation::Stack",
                "Replacement": "False",
                "ChangeSetId": "arn:aws:cloudformation:us-west-2:123456789012:changeSet/{{my-nested-stack-change-set}}/4eca1a01-e285-xmpl-8026-9a1967bfb4b0",
                "Scope": [
                    "Properties"
                ],
                "Details": [
                    {
                        "Target": {
                            "Attribute": "Properties",
                            "RequiresRecreation": "Never"
                        },
                        "Evaluation": "Dynamic",
                        "ChangeSource": "Automatic"
                    }
                ]
            }
        }
    ],
    "ChangeSetName": "{{my-root-stack-change-set}}",
    "ChangeSetId": "arn:aws:cloudformation:us-west-2:123456789012:changeSet/{{my-root-stack-change-set}}/4eca1a01-e285-xmpl-8026-9a1967bfb4b0",
    "StackId": "arn:aws:cloudformation:us-west-2:123456789012:stack/{{my-root-stack}}/d0a825a0-e4cd-xmpl-b9fb-061c69e99204",
    "StackName": "{{my-root-stack}}",
    "IncludeNestedStacks": true,
    "ParentChangeSetId": null,
    "RootChangeSetId": null,
    "Description": null,
    "Parameters": null,
    "CreationTime": "2020-11-18T05:20:56.651Z",
    "ExecutionStatus": "AVAILABLE",
    "Status": "CREATE_COMPLETE",
    "StatusReason": null,
    "NotificationARNs": [
        
    ],
    "RollbackConfiguration": {
        
    },
    "Capabilities": [
        "CAPABILITY_IAM"
    ],
    "Tags": null
}
```

下列 AWS CLI 範例說明指定巢狀堆疊的變更集。

```
aws cloudformation describe-change-set \
    --change-set-name {{my-nested-stack-change-set}} \
    --stack-name {{my-nested-stack}}
```

以下為範例輸出。

```
{
    "Changes": [
        {
            "Type": "Resource",
            "ResourceChange": {
                "Action": "Modify",
                "LogicalResourceId": "function",
                "PhysicalResourceId": "my-function",
                "ResourceType": "AWS::Lambda::Function",
                "Replacement": "False",
                "ChangeSetId": null,
                "Scope": [
                    "Properties"
                ],
                "Details": [
                    {
                        "Target": {
                            "Attribute": "Properties",
                            "Name": "Timeout",
                            "RequiresRecreation": "Never"
                        },
                        "Evaluation": "Static",
                        "ChangeSource": "DirectModification"
                    }
                ]
            }
        }
    ],
    "ChangeSetName": "{{my-nested-stack-change-set}}",
    "ChangeSetId": "arn:aws:cloudformation:us-west-2:123456789012:changeSet/{{my-nested-stack-change-set}}/4eca1a01-e285-xmpl-8026-9a1967bfb4b0",
    "StackId": "arn:aws:cloudformation:us-west-2:123456789012:stack/{{my-nested-stack}}/d0a825a0-e4cd-xmpl-b9fb-061c69e99205",
    "ParentChangeSetId": "arn:aws:cloudformation:us-west-2:123456789012:changeSet/{{my-root-stack-change-set}}/4eca1a01-e285-xmpl-8026-9a1967bfb4b0",
    "RootChangeSetId": "arn:aws:cloudformation:us-west-2:123456789012:changeSet/{{my-root-stack-change-set}}/4eca1a01-e285-xmpl-8026-9a1967bfb4b0",
    "IncludeNestedStacks": true,
    "StackName": "{{my-nested-stack}}",
    "Description": null,
    "Parameters": null,
    "CreationTime": "2020-11-18T05:20:56.651Z",
    "ExecutionStatus": "UNAVAILABLE",
    "Status": "CREATE_COMPLETE",
    "StatusReason": "Executable from root change set",
    "NotificationARNs": [
        
    ],
    "RollbackConfiguration": {
        
    },
    "Capabilities": [
        "CAPABILITY_IAM"
    ],
    "Tags": null
}
```

### 執行變更集
<a name="working-with-change-set-execute-cli"></a>
+ [https://docs.aws.amazon.com/cli/latest/reference/cloudformation/execute-change-set.html](https://docs.aws.amazon.com/cli/latest/reference/cloudformation/execute-change-set.html) – 使用在建立特定變更集時所提供的輸入資訊，建立或更新堆疊。若要為整個堆疊階層建立變更集，您必須在 **create-change-set** 操作期間指定 `–-include-nested-stacks` 選項。如需詳細資訊，請參閱[執行 CloudFormation 堆疊的變更集堆疊政策與執行變更集](using-cfn-updating-stacks-changesets-execute.md)。
**注意**  
**execute-change-set** 的執行必須透過根變更集進行，並將變更集套用至整個堆疊階層。

下列 AWS CLI 範例會針對指定的根堆疊執行變更集。

```
aws cloudformation execute-change-set \
    --stack-name {{my-root-stack}} \
    --change-set-name {{my-root-stack-change-set}}
```

### 刪除變更集
<a name="working-with-change-set-delete-cli"></a>
+ [https://docs.aws.amazon.com/cli/latest/reference/cloudformation/delete-change-set.html](https://docs.aws.amazon.com/cli/latest/reference/cloudformation/delete-change-set.html) – 刪除指定的變更集。刪除變更集可確保不會使用到錯誤的變更集。對於使用 `–-include-nested-stacks` 選項建立的變更集，變更集的刪除不是同時發生的。如需詳細資訊，請參閱[刪除 CloudFormation 堆疊的變更集](using-cfn-updating-stacks-changesets-delete.md)。
**注意**  
**delete-change-set** 的執行必須透過根變更集進行，並將會刪除整個變更集的階層。如果在 **create-change-set** 操作期間建立巢狀堆疊，也會刪除狀態為 `REVIEW_IN_PROGRESS` 的巢狀堆疊。

下列 AWS CLI 範例會刪除指定根堆疊的變更集。

```
aws cloudformation delete-change-set \
    --stack-name {{my-root-stack}} \ 
    --change-set-name {{my-root-stack-change-set}}
```