巢狀現有堆疊 - AWS CloudFormation

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

巢狀現有堆疊

使用 resource import 功能來在另一個現有堆疊中巢狀現有堆疊。巢狀堆疊是您進行宣告,並從其他範本參考的常見元件。透過這種方式,您可以避免在您的範本中複製和貼上相同的組態,簡化堆疊更新。如果您有一個常見元件的範本,您可以使用 AWS::CloudFormation::Stack 資源來從另一個範本中參考此範本。如需巢狀堆疊的詳細資訊,請參閱 使用巢狀堆疊在其他堆疊中嵌入堆疊

AWS CloudFormation 僅支援使用一個巢狀層級resource import。這表示您無法將堆疊匯入子堆疊,或是匯入具有子系的堆疊。

如果您不熟悉匯入,建議您先檢閱使用 AWS 資源導入將資源導入到 CloudFormation 堆棧中主題中的介紹性資訊。

巢狀堆疊匯入驗證

在巢狀堆疊匯入作業期間, AWS CloudFormation 會執行下列驗證。

  • 父堆疊範本中的巢狀 AWS::CloudFormation::Stack 定義與實際的巢狀堆疊範本相符。

  • 父堆疊範本中巢狀 AWS::CloudFormation::Stack 定義的標籤與實際的巢狀堆疊資源相符。

使用 AWS Management Console巢狀現有堆疊

  1. 將資AWS::CloudFormation::Stack源新增至父系堆疊範本,並使用 Retain DeletionPolicy. 在下面的例子父堆棧模板中,MyNestedStack是導入的目標。

    JSON

    { "AWSTemplateFormatVersion" : "2010-09-09", "Resources" : { "ServiceTable":{ "Type":"AWS::DynamoDB::Table", "Properties":{ "TableName":"Service", "AttributeDefinitions":[ { "AttributeName":"key", "AttributeType":"S" } ], "KeySchema":[ { "AttributeName":"key", "KeyType":"HASH" } ], "ProvisionedThroughput":{ "ReadCapacityUnits":5, "WriteCapacityUnits":1 } } }, "MyNestedStack" : { "Type" : "AWS::CloudFormation::Stack", "DeletionPolicy": "Retain", "Properties" : { "TemplateURL" : "https://s3.amazonaws.com/cloudformation-templates-us-east-2/EC2ChooseAMI.template", "Parameters" : { "InstanceType" : "t1.micro", "KeyName" : "mykey" } } } } }

    YAML

    AWSTemplateFormatVersion: 2010-09-09 Resources: ServiceTable: Type: 'AWS::DynamoDB::Table' Properties: TableName: Service AttributeDefinitions: - AttributeName: key AttributeType: S KeySchema: - AttributeName: key KeyType: HASH ProvisionedThroughput: ReadCapacityUnits: 5 WriteCapacityUnits: 1 MyNestedStack: Type: 'AWS::CloudFormation::Stack' DeletionPolicy: Retain Properties: TemplateURL: >- https://s3.amazonaws.com/cloudformation-templates-us-east-2/EC2ChooseAMI.template Parameters: InstanceType: t1.micro KeyName: mykey
  2. 開啟主 AWS CloudFormation 控台。

  3. Stacks (堆疊) 頁面上,在選取父堆疊的情況下,選擇 Stack actions (堆疊動作),然後選擇 Import resources into stack (將資源匯入堆疊)

    主控台中 Import resource into stack (將資源匯入堆疊) 的選項。
  4. 請閱讀 Import overview (匯入概觀) 頁面,以取得您在此操作期間必須提供的項目清單。然後選擇下一步

  5. Specify template (指定範本) 頁面上,使用以下其中一種方法提供更新後的父範本,然後選擇 Next (下一步)

    • 選擇 Amazon S3 URL,然後在文字方塊中URL為您的範本指定。

    • 選擇 Upload a template file (上傳範本檔案),然後瀏覽您的範本。

  6. Identify resources (識別資源) 頁面上,識別 AWS::CloudFormation::Stack 資源。

    1. Identifer property (識別碼屬性) 下方,選擇資源識別碼類型。例如,AWS::CloudFormation::Stack 資源可以使用 StackId 屬性進行識別。

    2. 在 [識別碼值] 底下,輸入您要匯入ARN的堆疊。例如:arn:aws:cloudformation:us-west-2:12345678910:stack/mystack/5b918d10-cd98-11ea-90d5-0a9cd3354c10

      主控台中的 Identify resources (識別資源) 頁面。
    3. 選擇 Next (下一步)

  7. Specify stack details (識別堆疊詳細資訊) 頁面上,修改任何參數,然後選擇 Next (下一步)。這會自動建立變更集合。

    重要

    如果您修改了啟動建立、更新或刪除操作的現有參數,匯入操作便會失敗。

  8. 關於檢MyParentStack頁面上,確認正在匯入正確的資源,然後選擇 [匯入資源]。這會自動執行在最後一個步驟中建立的變更集合。此時會將任何堆疊層級標籤套用到匯入的資源。

  9. 隨即會顯示父堆疊 Stack details (堆疊詳細資訊) 頁面的 Events (事件) 窗格。

    主控台中的 Events (事件) 標籤。
    注意

    在此匯入操作後不需要在父堆疊上執行漂移偵測,因為 AWS::CloudFormation::Stack 資源已由 AWS CloudFormation進行管理。

使用 AWS CLI巢狀現有堆疊

  1. 將資AWS::CloudFormation::Stack源新增至父系堆疊範本,並使用 Retain DeletionPolicy. 在以下範例父範本中,MyNestedStack 是匯入目標。

    JSON

    { "AWSTemplateFormatVersion" : "2010-09-09", "Resources" : { "ServiceTable":{ "Type":"AWS::DynamoDB::Table", "Properties":{ "TableName":"Service", "AttributeDefinitions":[ { "AttributeName":"key", "AttributeType":"S" } ], "KeySchema":[ { "AttributeName":"key", "KeyType":"HASH" } ], "ProvisionedThroughput":{ "ReadCapacityUnits":5, "WriteCapacityUnits":1 } } }, "MyNestedStack" : { "Type" : "AWS::CloudFormation::Stack", "DeletionPolicy": "Retain", "Properties" : { "TemplateURL" : "https://s3.amazonaws.com/cloudformation-templates-us-east-2/EC2ChooseAMI.template", "Parameters" : { "InstanceType" : "t1.micro", "KeyName" : "mykey" } } } } }

    YAML

    AWSTemplateFormatVersion: 2010-09-09 Resources: ServiceTable: Type: 'AWS::DynamoDB::Table' Properties: TableName: Service AttributeDefinitions: - AttributeName: key AttributeType: S KeySchema: - AttributeName: key KeyType: HASH ProvisionedThroughput: ReadCapacityUnits: 5 WriteCapacityUnits: 1 MyNestedStack: Type: 'AWS::CloudFormation::Stack' DeletionPolicy: Retain Properties: TemplateURL: >- https://s3.amazonaws.com/cloudformation-templates-us-east-2/EC2ChooseAMI.template Parameters: InstanceType: t1.micro KeyName: mykey
  2. 透過這些修改,撰寫如下列範例所示的JSON字串:

    • Replace (取代) MyNestedStack 具有範本中指定之目標資源的邏輯 ID。

    • Replace (取代) arn:aws:cloudformation:us-west-2:12345678910:stack/mystack/5b918d10-cd98-11ea-90d5-0a9cd3354c10 與你想要導入的堆棧。ARN

    [{"ResourceType":"AWS::CloudFormation::Stack","LogicalResourceId":"MyNestedStack","ResourceIdentifier":{"StackId":"arn:aws:cloudformation:us-east-2:123456789012:stack/mystack/5b918d10-cd98-11ea-90d5-0a9cd3354c10"}}]

    或者,您也可以在組態檔案中指定參數。

    例如,若要匯入MyNestedStack,您可以建立 ResourcesToImport.txt 包含下列組態的檔案。

    JSON

    [ { "ResourceType":"AWS::CloudFormation::Stack", "LogicalResourceId":"MyNestedStack", "ResourceIdentifier": { "StackId":"arn:aws:cloudformation:us-west-2:12345678910:stack/mystack/5b918d10-cd98-11ea-90d5-0a9cd3354c10" } } ]

    YAML

    ResourceType: 'AWS::CloudFormation::Stack' LogicalResourceId: MyNestedStack ResourceIdentifier: StackId: >- arn:aws:cloudformation:us-west-2:12345678910:stack/mystack/5b918d10-cd98-11ea-90d5-0a9cd3354c10
  3. 若要建立變更集,請使用下列create-change-set指令並取代預留位置文字。對於 --change-set-type 選項,請指定值為 IMPORT。對於該--resources-to-import選項,請用剛剛創建的實際字JSON符串替換示例JSON字符串。

    $ aws cloudformation create-change-set \ --stack-name MyParentStack --change-set-name ImportChangeSet \ --change-set-type IMPORT \ --template-body file://TemplateToImport.json \ --resources-to-import '[{"ResourceType":"AWS::CloudFormation::Stack","LogicalResourceId":"MyNestedStack","ResourceIdentifier":{"StackId":"arn:aws:cloudformation:us-west-2:12345678910:stack/mystack/5b918d10-cd98-11ea-90d5-0a9cd3354c10"}}]'
    注意

    --resources-to-import不支持內聯YAML。轉義JSON字符串中引號的要求因您的終端而異。如需詳細資訊,請參閱《使用指南》中的〈在字串內使用引號。AWS Command Line Interface

    或者,您可以使用檔案URL做為--resources-to-import選項的輸入,如下列範例所示。

    --resources-to-import file://ResourcesToImport.txt

    如果成功,此命令會傳回下列範例輸出。

    { "Id": "arn:aws:cloudformation:us-west-2:12345678910:changeSet/ImportChangeSet/8ad75b3f-665f-46f6-a200-0b4727a9442e", "StackId": "arn:aws:cloudformation:us-west-2:12345678910:stack/MyParentStack/4e345b70-1281-11ef-b027-027366d8e82b" }
  4. 檢閱變更集合,確認您正在匯入正確的堆疊。

    $ aws cloudformation describe-change-set --change-set-name ImportChangeSet
  5. 若要啟動變更集並將堆疊匯入來源父系堆疊,請使用下列execute-change-set指令並取代預留位置文字。此時會將任何堆疊層級標籤套用到匯入的資源。成功完成匯入操作 (IMPORT_COMPLETE) 後,堆疊便已順利巢狀。

    $ aws cloudformation execute-change-set --change-set-name ImportChangeSet
    注意

    在此匯入操作後不需要在父堆疊上執行漂移偵測,因為 AWS::CloudFormation::Stack 資源已由 AWS CloudFormation進行管理。