嵌套现有的堆栈 - AWS CloudFormation

嵌套现有的堆栈

可以使用 resource import 功能将现有堆栈嵌套在另一个现有堆栈中。嵌套堆栈是您从其他模板中声明和引用的通用组件。这样,您就可以避免将相同的配置复制并粘贴到您的模板中,并简化堆栈更新。如果您具有通用组件的模板,您可以使用 AWS::CloudFormation::Stack 资源从另一个模板中引用该模板。有关嵌套堆栈的更多信息,请参阅使用嵌套堆栈

在使用 resource import 时,AWS CloudFormation 仅支持一层嵌套。这意味着,您无法将堆栈导入到子堆栈中,也无法导入具有子堆栈的堆栈。

嵌套堆栈导入验证

在嵌套堆栈导入操作期间,AWS CloudFormation 执行以下验证。

  • 父堆栈模板中的嵌套 AWS::CloudFormation::Stack 定义与实际嵌套堆栈的模板匹配。

  • 父堆栈模板中的嵌套 AWS::CloudFormation::Stack 定义的标签与实际嵌套堆栈资源的标签匹配。

使用 AWS Management Console嵌套现有的堆栈

  1. 使用 Retain DeletionPolicyAWS::CloudFormation::Stack 资源添加到父堆栈模板中。在以下示例父模板中,NestedStack 是导入目标。

    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 } } }, "NestedStack" : { "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 NestedStack: 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 resources into stack (将资源导入到堆栈) 选项。
  4. 访问 Import overview (导入概述) 页面,以查看在该操作期间需要提供的内容列表。然后选择下一步

  5. Specify template (指定模板) 页面上,使用以下方法之一提供更新的父模板,然后选择 Next (下一步)

    • 选择 Amazon S3 URL,然后在文本框中指定您的模板的 URL。

    • 选择 Upload a template file (上传模板文件),然后浏览您的模板。

  6. Identify resources (标识资源) 页面上,标识 AWS::CloudFormation::Stack 资源。

    1. Identifier property (标识符属性) 下面,选择资源标识符的类型。例如,可以使用 StackId 属性标识 AWS::CloudFormation::Stack 资源。

    2. Identifier value (标识符值) 下面,键入实际属性值。例如,arn:aws:cloudformation:us-west-2:12345678910:stack/mystack/5b918d10-cd98-11ea-90d5-0a9cd3354c10

      
                        控制台中的 Identify resources (标识资源) 页面。
    3. 选择 Next(下一步)。

  7. Specify stack details (指定堆栈详细信息) 页面上,修改任何参数,然后选择 Next (下一步)。这会自动创建一个更改集。

    重要

    如果您修改的现有参数启动创建、更新或删除操作,导入操作将失败。

  8. Review stack-name (查看 <堆栈名称>) 页面上,确认正在导入正确的资源,然后选择 Import resources (导入资源)。这会自动执行在上一步中创建的更改集。此时,任何堆栈级标签将应用于导入的资源。

  9. 将显示父堆栈的 Stack details(堆栈详细信息)页面的 Events(事件)窗格。

    
                  控制台中的 Events (事件) 选项卡。
    注意

    在该导入操作完成后,无需对父堆栈运行偏差检测,因为 AWS::CloudFormation::Stack 资源已由 AWS CloudFormation 进行管理。

使用 AWS CLI嵌套现有的堆栈

  1. 使用 Retain DeletionPolicyAWS::CloudFormation::Stack 资源添加到父堆栈模板中。在以下示例父模板中,NestedStack 是导入目标。

    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 } } }, "NestedStack" : { "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 NestedStack: 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. 使用以下参数创建 IMPORT 类型的更改集。--resources-to-import 不支持内联 YAML。

    > aws cloudformation create-change-set --stack-name TargetParentStack --change-set-name ImportChangeSet --change-set-type IMPORT --resources-to-import "[{\"ResourceType\":\AWS::CloudFormation::Stack\",\"LogicalResourceId\":\"MyStack\",\"ResourceIdentifier\":{\"StackId\":\"arn:aws:cloudformation:us-east-2:123456789012:stack/mystack-mynestedstack-sggfrhxhum7w/f449b250-b969-11e0-a185-5081d0136786\"}}] --template-body file://templateToImport.json

    AWS CLI 还支持将文本文件作为 resources-to-import 参数的输入,如以下示例中所示。

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

    在本演练中,file://resourcesToImport.txt 包含以下内容。

    JSON

    [ { "ResourceType":"AWS::CloudFormation::Stack", "LogicalResourceId":"MyStack", "ResourceIdentifier": { "StackId":"arn:aws:cloudformation:us-east-2:123456789012:stack/mystack-mynestedstack-sggfrhxhum7w/f449b250-b969-11e0-a185-5081d0136786" } } ]

    YAML

    ResourceType: 'AWS::CloudFormation::Stack' LogicalResourceId: MyStack ResourceIdentifier: StackId: >- arn:aws:cloudformation:us-east-2:123456789012:stack/mystack-mynestedstack-sggfrhxhum7w/f449b250-b969-11e0-a185-5081d0136786
  3. 查看更改集,以确保导入正确的堆栈。

    > aws cloudformation describe-change-set --change-set-name ImportChangeSet
  4. 执行更改集以将堆栈导入到源父堆栈中。此时,任何堆栈级标签将应用于导入的资源。在成功完成导入操作后 (IMPORT_COMPLETE),将成功嵌套堆栈。

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

    在该导入操作完成后,无需对父堆栈运行偏差检测,因为 AWS::CloudFormation::Stack 资源已由 AWS CloudFormation 进行管理。