将现有的资源导入到堆栈 - Amazon CloudFormation
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

将现有的资源导入到堆栈

在该导入操作期间,您需要提供以下内容。

  • 描述整个堆栈的模板,包括堆栈已包含的资源和要导入的资源。每个要导入的资源必须在模板中具有 DeletionPolicy 属性

  • 每个目标资源的唯一标识符。请访问相应的服务控制台以获取唯一标识符。

在本演练中,我们提供了以下名为 TemplateToImport.json 的示例模板。ServiceTable 当前是堆栈的一部分,而 GamesTable 是导入目标。

{ "AWSTemplateFormatVersion": "2010-09-09", "Description": "Import test", "Resources": { "ServiceTable": { "Type": "AWS::DynamoDB::Table", "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 CloudFormation 控制台不支持使用内置函数 Fn:: Transform。您可以使用 Amazon Command Line Interface 导入使用内置函数 Fn:: Transform 的资源。

  1. 登录到 Amazon Web Services Management Console 并打开 Amazon CloudFormation 控制台 https://console.aws.amazon.com/cloudformation

  2. Stacks (堆栈) 页面上,选择要将资源导入到的堆栈。

  3. 选择 Stack actions (堆栈操作),然后选择 Import resources into stack (将资源导入到堆栈)

    
       控制台中的 Import resources into stack (将资源导入到堆栈) 选项。
  4. 查看 Import overview (导入概述) 页面,然后选择 Next (下一步)

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

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

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

  6. Identify resources(标识资源)页面上,标识每个目标资源。

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

    2. Identifier value (标识符值) 下面,键入实际属性值。例如,示例模板中的 GamesTable 资源的 TableNameGames

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

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

    注意

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

  8. Review stack-name (查看 <堆栈名称>) 页面上,查看要导入的资源,然后选择 Import resources (导入资源)。这会自动执行在上一步中创建的更改集。此时,任何堆栈级标签将应用于导入的资源。有关更多信息,请参阅设置 Amazon CloudFormation 堆栈选项

    将显示堆栈的 Events (事件) 页面。

    
       控制台中的 Events (事件) 选项卡。
  9. (可选)对堆栈运行偏差检测,以确保导入的资源的模板和实际配置匹配。有关检测偏差的更多信息,请参阅 在整个 CloudFormation 堆栈上检测偏差

  10. (可选)如果导入的资源与预期的模板配置不匹配,请更正模板配置或直接更新资源。有关导入偏差的资源的更多信息,请参阅 通过导入操作消除偏差

使用 Amazon CLI 将现有资源导入到堆栈

  1. (可选)运行 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
  2. 使用以下格式编写要导入的资源及其唯一标识符的列表。

    [{\"ResourceType\":\"AWS::DynamoDB::Table\",\"LogicalResourceId\":\"GamesTable\",\"ResourceIdentifier\":{\"TableName\":\"Games\"}}]
  3. 使用以下参数创建 IMPORT 类型的更改集。--resources-to-import 不支持内联 YAML。

    > aws cloudformation create-change-set --stack-name TargetStack --change-set-name ImportChangeSet --change-set-type IMPORT --resources-to-import "[{\"ResourceType\":\"AWS::DynamoDB::Table\",\"LogicalResourceId\":\"GamesTable\",\"ResourceIdentifier\":{\"TableName\":\"Games\"}}]" --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" } } ]
  4. 查看更改集,以确保将导入正确的资源。

    > aws cloudformation describe-change-set --change-set-name ImportChangeSet --stack-name TargetStack
  5. 执行更改集以导入资源。此时,任何堆栈级标签将应用于导入的资源。有关更多信息,请参阅设置 Amazon CloudFormation 堆栈选项。在成功完成该操作后 (IMPORT_COMPLETE),将成功导入资源。

    > aws cloudformation execute-change-set --change-set-name ImportChangeSet --stack-name TargetStack
  6. (可选)对 IMPORT_COMPLETE 堆栈运行偏差检测,以确保导入的资源的模板和实际配置匹配。有关检测偏差的更多信息,请参阅 在整个 CloudFormation 堆栈上检测偏差

    > aws cloudformation detect-stack-drift --stack-name TargetStack { "Stack-Drift-Detection-Id" : "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
  7. (可选)如果导入的资源与预期的模板配置不匹配,请更正模板配置或直接更新资源。有关导入偏差的资源的更多信息,请参阅 通过导入操作消除偏差