通过导入操作消除偏差 - AWS CloudFormation

通过导入操作消除偏差

在某些情况下,资源的配置可能已偏离其预期配置,而您希望接受新配置作为预期配置。在大多数情况下,您可以通过使用新配置更新堆栈模板中的资源定义来消除偏差结果,然后执行堆栈更新。但是,如果新配置更新了需要替换的资源属性,则在堆栈更新期间将重新创建该资源。如果要保留现有资源,则可以使用资源导入功能来更新资源并消除偏差结果,而不会导致替换资源。

通过导入操作消除资源偏差的过程包括以下基本步骤:

有关资源导入的更多信息,请参阅使用 CloudFormation 对现有资源进行管理。有关支持导入的资源列表,请参阅支持导入操作的资源

在此示例中,我们使用以下名为 templateToImport.json 的模板。

Example JSON
{ "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" } ], "BillingMode": "PROVISIONED", "ProvisionedThroughput":{ "ReadCapacityUnits":5, "WriteCapacityUnits":1 } } }, "GamesTable": { "Type": "AWS::DynamoDB::Table", "Properties": { "TableName": "Games", "AttributeDefinitions": [ { "AttributeName": "key", "AttributeType": "S" } ], "KeySchema": [ { "AttributeName": "key", "KeyType": "HASH" } ], "BillingMode": "PROVISIONED", "ProvisionedThroughput": { "ReadCapacityUnits": 5, "WriteCapacityUnits": 1 } } } } }
Example YAML
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 BillingMode: PROVISIONED ProvisionedThroughput: ReadCapacityUnits: 5 WriteCapacityUnits: 1 GamesTable: Type: 'AWS::DynamoDB::Table' Properties: TableName: Games AttributeDefinitions: - AttributeName: key AttributeType: S KeySchema: - AttributeName: key KeyType: HASH BillingMode: PROVISIONED ProvisionedThroughput: ReadCapacityUnits: 5 WriteCapacityUnits: 1

在此示例中,我们假设用户更改了 CloudFormation 之外的资源。运行偏差检测后,我们发现 GamesTableBillingMode 已修改为 PAY_PER_REQUEST。有关偏差检测的更多信息,请参阅检测堆栈和资源的非托管配置更改


                偏差结果在控制台中显示预期结果和实际结果。

我们的堆栈现在已经过时,我们的资源处于活动状态,但我们希望保留预期的资源配置。为此,我们可以通过导入操作来消除偏差,而不会中断服务。

使用 CloudFormation 控制台通过导入操作消除偏差

第 1 步 使用 Retain 删除策略更新堆栈

使用带有 Retain 选项的 DeletionPolicy 属性更新堆栈
  1. 登录到 AWS Management Console 并打开 AWS CloudFormation 控制台 https://console.aws.amazon.com/cloudformation

  2. 堆栈页面上,选择已偏差的堆栈。

  3. 选择更新,然后从堆栈详细信息窗格中选择替换当前模板

  4. 指定模板页面上,使用下列方法之一提供更新的模板,其中包含带有 Retain 选项的 DeletionPolicy 属性:

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

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

    然后选择下一步

  5. 查看指定堆栈详细信息页面,然后选择下一步

  6. 查看配置堆栈选项页面,然后选择下一步

  7. 查看 stack-name 页面上,选择更新堆栈

结果:在堆栈的事件页面上,状态为 UPDATE_COMPLETE

要在不中断服务的情况下通过导入操作消除偏差,请为要从堆栈中移除的资源指定 Retain DeletionPolicy。在以下示例中,我们向 GamesTable 资源添加了一个设置为 RetainDeletionPolicy 属性。

Example JSON
"GamesTable": { "Type": "AWS::DynamoDB::Table", "DeletionPolicy": "Retain", "Properties": { "TableName": "Games",
Example YAML
GamesTable: Type: 'AWS::DynamoDB::Table' DeletionPolicy: Retain Properties: TableName: Games

第 2 步 移除偏差的资源、相关参数和输出

移除偏差的资源、相关参数和输出
  1. 选择更新,然后从堆栈详细信息窗格中选择替换当前模板

  2. 指定模板页面上,使用下列方法之一提供更新的模板,该模板的资源、相关参数和输出已从堆栈模板中移除:

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

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

    然后选择下一步

  3. 查看指定堆栈详细信息页面,然后选择下一步

  4. 查看配置堆栈选项页面,然后选择下一步

  5. 查看 stack-name 页面上,选择更新堆栈

结果逻辑 ID GamesTable 在堆栈的事件页面上具有状态 DELETE_SKIPPED

等待 CloudFormation 完成堆栈更新操作。堆栈更新操作完成后,从堆栈模板中移除资源、相关参数和输出。然后,导入更新后的模板。完成这些操作后,示例模板现在如下所示。

Example JSON
{ "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" } ], "BillingMode": "PROVISIONED", "ProvisionedThroughput":{ "ReadCapacityUnits":5, "WriteCapacityUnits":1 } } } } }
Example YAML
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 BillingMode: PROVISIONED ProvisionedThroughput: ReadCapacityUnits: 5 WriteCapacityUnits: 1

第 3 步 更新模板以匹配资源的实时状态

更新模板以匹配资源的实时状态
  1. 要导入更新后的模板,请选择堆栈操作,然后选择将资源导入到堆栈

    
                                控制台中的 Import resources into stack (将资源导入到堆栈) 选项。
  2. 导入概述页面上,查看在该操作期间需要提供的内容列表,然后选择下一步

  3. 指定模板页面上,使用下列方法之一提供更新的模板:

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

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

    然后选择下一步

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

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

    2. 标识符值下,输入实际属性值。在示例模板中,GamesTable 资源的 TableNameGames

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

  5. 查看指定堆栈详细信息页面,然后选择下一步

  6. 导入概述页面上,查看正在导入的资源,然后选择导入资源。这会将 AWS::DynamoDB::Table 资源类型导入回您的堆栈中。

结果:在此示例中,我们通过导入操作消除了资源偏差,而不会中断服务。您可以在 CloudFormation 控制台的“Events (事件)”选项卡中检查导入操作的进度。导入的资源将具有 IMPORT_COMPLETE 状态,后跟 CREATE_COMPLETE 状态,并且资源导入完成作为状态原因。

等待 CloudFormation 完成堆栈更新操作。堆栈更新操作完成后,更新模板以匹配资源的实际偏差状态。例如,BillingMode 将设置为 PAY_PER_REQUESTReadCapacityUnitsWriteCapacityUnits 将设置为 0

Example JSON
{ "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" } ], "BillingMode": "PROVISIONED", "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" } ], "BillingMode": "PAY_PER_REQUEST", "ProvisionedThroughput": { "ReadCapacityUnits": 0, "WriteCapacityUnits": 0 } } } } }
Example YAML
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 BillingMode: PROVISIONED 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 BillingMode: PAY_PER_REQUEST ProvisionedThroughput: ReadCapacityUnits: 0 WriteCapacityUnits: 0