从现有的资源中创建堆栈
在该导入操作期间,您需要提供以下内容。
-
描述将位于新堆栈中的资源和资源配置的模板。模板中的每个资源必须具有 DeletionPolicy 属性。
-
每个目标资源的唯一标识符。请访问相应的服务控制台以获取唯一标识符。
在本演练中,我们提供了以下名为 TemplateToImport.json
的示例模板。ServiceTable
和 GamesTable
是导入目标。
{ "AWSTemplateFormatVersion": "2010-09-09", "Description": "Import test", "Resources": { "ServiceTable": { "Type": "AWS::DynamoDB::Table", "DeletionPolicy": "Retain", "Properties": { "TableName": "Service", "AttributeDefinitions": [ { "AttributeName": "key", "AttributeType": "S" } ], "KeySchema": [ { "AttributeName": "key", "KeyType": "HASH" } ], "ProvisionedThroughput": { "ReadCapacityUnits": 5, "WriteCapacityUnits": 1 } } }, "GamesTable": { "Type": "AWS::DynamoDB::Table", "DeletionPolicy": "Retain", "Properties": { "TableName": "Games", "AttributeDefinitions": [ { "AttributeName": "key", "AttributeType": "S" } ], "KeySchema": [ { "AttributeName": "key", "KeyType": "HASH" } ], "ProvisionedThroughput": { "ReadCapacityUnits": 5, "WriteCapacityUnits": 1 } } } } }
使用 Amazon Web Services Management Console 从现有的资源中创建堆栈
-
登录到 Amazon Web Services Management Console 并打开 Amazon CloudFormation 控制台 https://console.aws.amazon.com/cloudformation
。 -
在 Stacks (堆栈) 页面上,选择 Create stack (创建堆栈),然后选择 With existing resources (import resources) (使用现有的资源 (导入资源))。
-
访问 Import overview (导入概述) 页面,以查看在该操作期间需要提供的内容列表。然后选择下一步。
-
在 Specify template (指定模板) 页面上,使用以下方法之一提供模板,然后选择 Next (下一步)。
-
选择 Amazon S3 URL,然后在文本框中指定您的模板的 URL。
-
选择 Upload a template file (上传模板文件),然后浏览您的模板。
-
-
在 Identify resources(标识资源)页面上,标识每个目标资源。
-
在 Identifier property (标识符属性) 下面,选择资源标识符的类型。例如,可以使用
TableName
属性标识AWS::DynamoDB::Table
资源。 -
在 Identifier value (标识符值) 下面,键入实际属性值。例如,示例模板中的
GamesTable
资源的TableName
为
。Games
-
选择 Next(下一步)。
-
-
在 Specify stack details (指定堆栈详细信息) 页面上,修改任何参数,然后选择 Next (下一步)。这会自动创建一个更改集。
重要
如果您修改的现有参数启动创建、更新或删除操作,导入操作将失败。
-
在 Review
stack-name
(查看 <堆栈名称>) 页面上,确认正在导入正确的资源,然后选择 Import resources (导入资源)。这会自动执行在上一步中创建的更改集。将显示新堆栈的 Stack details(堆栈详细信息)页面的 Events(事件)窗格。
-
(可选)对堆栈运行偏差检测,以确保导入的资源的模板和实际配置匹配。有关检测偏差的更多信息,请参阅在整个 CloudFormation 堆栈上检测偏差。
-
(可选)如果导入的资源与预期的模板配置不匹配,请更正模板配置或直接更新资源。在本演练中,我们更正模板配置以与其实际配置匹配。
-
为受影响的资源恢复导入操作。
-
再次将导入目标添加到模板中,从而确保模板配置与实际配置匹配。
-
使用修改的模板重复步骤 2 - 8,以再次导入资源。
-
使用 Amazon CLI 从现有的资源中创建堆栈
-
打开 Amazon CLI。
-
(可选)运行
GetTemplateSummary
以了解哪些属性标识模板中的每种资源类型。例如,可以使用TableName
属性标识AWS::DynamoDB::Table
资源。对于示例模板中的GamesTable
资源,TableName
的值为Games
。>
aws cloudformation get-template-summary --template-url https://
DOC-EXAMPLE-BUCKET
.s3.us-west-2
.amazonaws.com/TemplateToImport.json
-
使用以下格式编写模板中的目标资源及其唯一标识符的列表。
[{\"ResourceType\":\"
AWS::DynamoDB::Table
\",\"LogicalResourceId\":\"GamesTable
\",\"ResourceIdentifier\":{\"TableName
\":\"Games
\"}}] -
使用以下参数创建
IMPORT
类型的更改集。--resources-to-import
不支持内联 YAML。>
aws cloudformation create-change-set --stack-name
TargetStack
--change-set-nameImportChangeSet
--change-set-typeIMPORT
--resources-to-import "[{\"ResourceType\":\"AWS::DynamoDB::Table
\",\"LogicalResourceId\":\"GamesTable
\",\"ResourceIdentifier\":{\"TableName
\":\"Games
\"}},{\"ResourceType\":\"AWS::DynamoDB::Table
\",\"LogicalResourceId\":\"ServiceTable
\",\"ResourceIdentifier\":{\"TableName
\":\"Service
\"}}]" --template-url https://DOC-EXAMPLE-BUCKET
.s3.us-west-2
.amazonaws.com/TemplateToImport.json
Amazon CLI 还支持将文本文件作为
--resources-to-import
参数的输入,如以下示例中所示。--resources-to-import
file://ResourcesToImport.txt
在本演练中,
file://ResourcesToImport.txt
包含以下内容。[ { "ResourceType":"AWS::DynamoDB::Table", "LogicalResourceId":"GamesTable", "ResourceIdentifier":{ "TableName":"Games" } }, { "ResourceType":"AWS::DynamoDB::Table", "LogicalResourceId":"ServiceTable", "ResourceIdentifier":{ "TableName":"Service" } } ]
-
查看更改集,以确保将导入正确的资源。
>
aws cloudformation describe-change-set --change-set-name
ImportChangeSet
--stack-nameTargetStack
-
执行更改集以导入资源。在成功完成该操作后
(IMPORT_COMPLETE)
,将成功导入资源。>
aws cloudformation execute-change-set --change-set-name
ImportChangeSet
--stack-nameTargetStack
-
(可选)对
IMPORT_COMPLETE
堆栈运行偏差检测,以确保导入的资源的模板和实际配置匹配。有关检测偏差的更多信息,请参阅在整个 CloudFormation 堆栈上检测偏差。>
aws cloudformation detect-stack-drift --stack-name
TargetStack
{ "StackDriftDetectionId" : "624af370-311a-11e8-b6b7-500cexample" }>
aws cloudformation describe-stack-drift-detection-status --stack-drift-detection-id
624af370-311a-11e8-b6b7-500cexample
>
aws cloudformation describe-stack-resource-drifts --stack-name
TargetStack
-
(可选)如果导入的资源与预期的模板配置不匹配,请更正模板配置或直接更新资源。在本演练中,我们更正模板配置以与其实际配置匹配。
-
为受影响的资源恢复导入操作。
-
再次将导入目标添加到模板中,从而确保模板配置与实际配置匹配。
-
使用修改的模板重复步骤 4 - 7,以再次导入资源。
-