本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
嵌套现有的堆栈
可以使用 resource import
功能将现有堆栈嵌套在另一个现有堆栈中。嵌套堆栈是您从其他模板中声明和引用的通用组件。这样,您就可以避免将相同的配置复制并粘贴到您的模板中,并简化堆栈更新。如果您具有通用组件的模板,您可以使用 AWS::CloudFormation::Stack
资源从另一个模板中引用该模板。有关嵌套堆栈的更多信息,请参阅使用嵌套堆栈 查看嵌套堆栈的根堆栈 查看属于根堆栈的嵌套堆栈。
在使用 resource import
时,Amazon CloudFormation 仅支持一层嵌套。这意味着,您无法将堆栈导入到子堆栈中,也无法导入具有子堆栈的堆栈。
嵌套堆栈导入验证
在嵌套堆栈导入操作期间,Amazon CloudFormation 执行以下验证。
-
父堆栈模板中的嵌套
AWS::CloudFormation::Stack
定义与实际嵌套堆栈的模板匹配。 -
父堆栈模板中的嵌套
AWS::CloudFormation::Stack
定义的标签与实际嵌套堆栈资源的标签匹配。
使用 Amazon Web Services Management Console嵌套现有的堆栈
-
使用将
AWS::CloudFormation::Stack
资源添加到父堆栈模板中Retain
DeletionPolicy。在以下示例父模板中,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
-
打开 Amazon CloudFormation 控制台。
-
在 Stacks (堆栈) 页面上,在选择了父堆栈的情况下,选择 Stack actions (堆栈操作),然后选择 Import resources into stack (将资源导入到堆栈)。
-
访问 Import overview (导入概述) 页面,以查看在该操作期间需要提供的内容列表。然后选择下一步。
-
在 Specify template (指定模板) 页面上,使用以下方法之一提供更新的父模板,然后选择 Next (下一步)。
-
选择 Amazon S3 URL,然后在文本框中指定您的模板的 URL。
-
选择 Upload a template file (上传模板文件),然后浏览您的模板。
-
-
在 Identify resources (标识资源) 页面上,标识
AWS::CloudFormation::Stack
资源。-
在 Identifier property (标识符属性) 下面,选择资源标识符的类型。例如,可以使用
StackId
属性标识AWS::CloudFormation::Stack
资源。 -
在 Identifier value (标识符值) 下面,键入实际属性值。例如,
。arn:aws:cloudformation:us-west-2:12345678910:stack/mystack/5b918d10-cd98-11ea-90d5-0a9cd3354c10
-
选择下一步。
-
-
在 Specify stack details (指定堆栈详细信息) 页面上,修改任何参数,然后选择 Next (下一步)。这会自动创建一个更改集。
重要
如果您修改的现有参数启动创建、更新或删除操作,导入操作将失败。
-
在 Review
stack-name
(查看 <堆栈名称>) 页面上,确认正在导入正确的资源,然后选择 Import resources (导入资源)。这会自动执行在上一步中创建的更改集。此时,任何堆栈级标签将应用于导入的资源。 -
将显示父堆栈的 Stack details(堆栈详细信息)页面的 Events(事件)窗格。
注意
在该导入操作完成后,无需对父堆栈运行偏差检测,因为
AWS::CloudFormation::Stack
资源已由 Amazon CloudFormation 进行管理。
使用 Amazon CLI嵌套现有的堆栈
-
使用将
AWS::CloudFormation::Stack
资源添加到父堆栈模板中Retain
DeletionPolicy。在以下示例父模板中,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
-
使用以下参数创建
IMPORT
类型的更改集。--resources-to-import
不支持内联 YAML。>
aws cloudformation create-change-set --stack-name
TargetParentStack
--change-set-nameImportChangeSet
--change-set-typeIMPORT
--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-bodyfile://templateToImport.json
Amazon CLI 还支持将文本文件作为
resources-to-import
参数的输入,如以下示例中所示。--resources-to-import:
file://resourcesToImport.txt
在本演练中,f
ile://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
-
查看更改集,以确保导入正确的堆栈。
>
aws cloudformation describe-change-set --change-set-name
ImportChangeSet
-
执行更改集以将堆栈导入到源父堆栈中。此时,任何堆栈级标签将应用于导入的资源。在成功完成导入操作后
(IMPORT_COMPLETE)
,将成功嵌套堆栈。>
aws cloudformation execute-change-set --change-set-name
ImportChangeSet
注意
在该导入操作完成后,无需对父堆栈运行偏差检测,因为
AWS::CloudFormation::Stack
资源已由 Amazon CloudFormation 进行管理。