本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
在堆疊之間移動資源
使用 resource import
功能,您可以在資源之間移動 (或是「重構」) 堆疊。您需要先將 Retain
刪除政策新增到您要移動的資源,以確保當您從來源堆疊移除該資源,並匯入到目標堆疊時,仍會保留該資源。
如果您是初次匯入,建議您先檢閱使用 AWS 資源匯入將資源匯入 CloudFormation 堆疊主題中的簡介資訊。
重要
並非所有資源都支援匯入操作。從堆疊移除資源之前,請參閱支援匯入操作的資源。如果您從堆疊移除不支援匯入操作的資源,則無法將資源匯入另一個堆疊或放回來源堆疊。
使用 重構堆疊 AWS Management Console
-
在來源範本中,
Retain
DeletionPolicy為您要移動的資源指定 。在以下範例來源範本中,
Games
是此重構的目標。範例 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" } ], "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 } } } } }
-
開啟 CloudFormation 主控台以執行堆疊更新,以套用刪除政策。
-
在堆疊頁面上,已選擇堆疊的情況下選擇更新。
-
在 Prepare template (準備範本) 下方,選擇 Replace current template (取代目前範本)。
-
在 Specify template (指定範本) 下方,提供在
GamesTable
上具備DeletionPolicy
屬性的更新來源範本,然後選擇 Next (下一步)。-
選擇 Amazon S3 URL,然後在文字方塊中將 指定URL為更新的來源範本。
-
選擇 Upload a template file (上傳範本檔案),然後瀏覽更新後的來源範本檔案。
-
-
在 Specify stack details (指定堆疊詳細資訊) 頁面上,不需要進行任何變更。選擇 Next (下一步)。
-
在 Configure stack options (設定堆疊選項) 頁面上,不需要進行任何變更。選擇 Next (下一步)。
-
在檢閱
SourceStackName
頁面上,檢閱您的變更。如果您的範本包含 IAM 資源,請選取 I acknowledge that this template may create IAM resources (我知道此範本可能會建立 IAM 資源),以指定您要使用此範本中的 IAM 資源。如需有關使用範本中 IAM 資源的詳細資訊,請參閱使用 控制 CloudFormation 存取 AWS Identity and Access Management。然後,透過建立變更集合來更新來源堆疊,或直接更新來源堆疊。
-
-
從來源範本移除資源、相關參數和輸出,然後將這些項目新增到目標範本。
來源範本現在看起來會如下。
範例 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" } ], "ProvisionedThroughput":{ "ReadCapacityUnits":5, "WriteCapacityUnits":1 } } } } }
以下範例目標範本目前具備
PlayersTable
資源,現在也包含了GamesTable
。範例 JSON
{ "AWSTemplateFormatVersion": "2010-09-09", "Description": "Import test", "Resources": { "PlayersTable": { "Type": "AWS::DynamoDB::Table", "Properties": { "TableName": "Players", "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 } } } } }
-
重複步驟 2 到 3 以再次更新來源堆疊,這一次請從堆疊刪除目標資源。
-
執行匯入操作,將
GamesTable
新增到目標堆疊。-
在 Stacks (堆疊) 頁面上,在選取父堆疊的情況下,選擇 Stack actions (堆疊動作),然後選擇 Import resources into stack (將資源匯入堆疊)。
-
請閱讀 Import overview (匯入概觀) 頁面,以取得您在此操作期間必須提供的項目清單。然後選擇下一步。
-
在 Specify template (指定範本) 頁面上,完成以下其中一項操作,然後選擇 Next (下一步)。
-
選擇 Amazon S3 URL,然後在文字方塊URL中指定 。
-
選擇 Upload a template file (上傳範本檔案),然後瀏覽要上傳的檔案。
-
-
在 Identify resources (識別資源) 頁面上,識別您正在移動的資源 (在此範例中為
GamesTable
)。如需詳細資訊,請參閱資源識別符。-
在 Identifer property (識別碼屬性) 下方,選擇資源識別碼類型。例如,
AWS::DynamoDB::Table
資源可以使用TableName
屬性進行識別。 -
在 Identifer value (識別碼值) 下方,輸入實際的屬性值。例如:
。GamesTables
-
選擇 Next (下一步)。
-
-
在 Specify stack details (識別堆疊詳細資訊) 頁面上,修改任何參數,然後選擇 Next (下一步)。這會自動建立變更集合。
重要
如果您修改了啟動建立、更新或刪除操作的現有參數,匯入操作便會失敗。
-
在檢閱
TargetStackName
頁面上,確認正在匯入正確的資源,然後選擇匯入資源。這會自動啟動在最後一個步驟中建立的變更集。此時會將任何堆疊層級標籤套用到匯入的資源。 -
隨即會顯示父堆疊 Stack details (堆疊詳細資訊) 頁面的 Events (事件) 窗格。
注意
在此匯入操作後不需要在父堆疊上執行漂移偵測,因為
AWS::CloudFormation::Stack
資源已由 CloudFormation 進行管理。
-
使用 重構堆疊 AWS CLI
-
在來源範本中,
Retain
DeletionPolicy為您要移動的資源指定 。在以下範例來源範本中,
GamesTable
是此重構的目標。範例 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" } ], "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 } } } } }
-
更新來源堆疊,以將刪除政策套用到資源。
aws cloudformation update-stack --stack-name
SourceStackName
-
從來源範本移除資源、相關參數和輸出,然後將這些項目新增到目標範本。
來源範本現在看起來會如下。
範例 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" } ], "ProvisionedThroughput":{ "ReadCapacityUnits":5, "WriteCapacityUnits":1 } } } } }
以下範例目標範本目前具備
PlayersTable
資源,現在也包含了GamesTable
。範例 JSON
{ "AWSTemplateFormatVersion": "2010-09-09", "Description": "Import test", "Resources": { "PlayersTable": { "Type": "AWS::DynamoDB::Table", "Properties": { "TableName": "Players", "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 } } } } }
-
更新來源堆疊,以從堆疊刪除
GamesTable
資源及其相關參數和輸出。aws cloudformation update-stack --stack-name
SourceStackName
-
以下列JSON字串格式撰寫要匯入的實際資源清單及其唯一識別符。如需詳細資訊,請參閱資源識別符。
[{"ResourceType":"AWS::DynamoDB::Table","LogicalResourceId":"GamesTable","ResourceIdentifier":{"TableName":"Games"}}]
或者,您可以在組態檔案中指定 JSON格式化參數。
例如,若要匯入
GamesTable
,您可以建立包含下列組態ResourcesToImport.txt
的檔案。[ { "ResourceType":"AWS::DynamoDB::Table", "LogicalResourceId":"GamesTable", "ResourceIdentifier": { "TableName":"Games" } } ]
-
若要建立變更集,請使用下列create-change-set命令並取代預留位置文字。對於
--change-set-type
選項,請指定值為IMPORT
。對於--resources-to-import
選項,將範例JSON字串取代為您剛建立的實際JSON字串。aws cloudformation create-change-set \ --stack-name
TargetStackName
--change-set-nameImportChangeSet
\ --change-set-typeIMPORT
\ --template-bodyfile://TemplateToImport.json
\ --resources-to-import "'[{"ResourceType":"AWS::DynamoDB::Table","LogicalResourceId":"GamesTable","ResourceIdentifier":{"TableName":"Games"}}]'
"注意
--resources-to-import
不支援內嵌 YAML。JSON 字串中逸出引號的需求會因終端機而異。如需詳細資訊,請參閱AWS Command Line Interface 《 使用者指南》中的在字串內使用引號。或者,您可以使用 檔案URL做為
--resources-to-import
選項的輸入,如下列範例所示。--resources-to-import
file://ResourcesToImport.txt
-
檢閱變更集合,確認您正在將正確的資源匯入目標堆疊。
aws cloudformation describe-change-set \ --change-set-name
ImportChangeSet
-
若要啟動變更集並匯入資源,請使用下列execute-change-set命令並取代預留位置文字。此時會將任何堆疊層級標籤套用到匯入的資源。成功完成操作
(IMPORT_COMPLETE)
後,資源便已順利匯入。aws cloudformation execute-change-set \ --change-set-name
ImportChangeSet
--stack-nameTargetStackName
注意
在此匯入操作後不需要在父堆疊上執行漂移偵測,因為資源已由 CloudFormation 進行管理。