💾 Archived View for cfdocs.wetterberg.nu › refactor-stacks.gemini captured on 2023-11-04 at 11:54:59. Gemini links have been rewritten to link to archived content
View Raw
More Information
⬅️ Previous capture (2021-12-03)
-=-=-=-=-=-=-
Moving resources between stacks
Search
Using the `resource import` feature, you can move resources between, or *refactor*, stacks. You need to first add a `Retain` deletion policy to the resource you want to move to ensure that the resource is preserved when you remove it from the source stack and import it to the target stack.
Not all resources support import operations. See Resources that support import operations before you remove a resource from your stack. If you remove a resource that doesn't support import operations from your stack, you can't import the resource into another stack or bring it back into the source stack.
Resources that support import operations
Refactor a stack using the AWS Management Console
- In the source template, specify a Retain DeletionPolicy for the resource you want to move.In the following example source template, Games is the target of this refactor.Example JSON
- Open the AWS CloudFormation console to perform a stack update to apply the deletion policy.On the Stacks page, with the stack selected, choose Update.Under Prepare template, choose Replace current template.Under Specify template, provide the updated source template with the DeletionPolicy attribute on GamesTable, and then choose Next.Choose Amazon S3 URL, and then specify the URL to the updated source template in the text box.Choose Upload a template file, and then browse for the updated source template file.On the Specify stack details page, no changes are required. Choose Next.On the Configure stack options page, no changes are required. Choose Next.On the Review stack_name page, review your changes. If your template contains IAM resources, select I acknowledge that this template may create IAM resources to specify that you want to use IAM resources in the template. For more information about using IAM resources in templates, see Controlling access with AWS Identity and Access Management. Then, either update your source stack by creating a change set or update your source stack directly.
- Remove the resource, related parameters, and outputs from the source template, and then add them to the target template.The source template now looks like the following.Example JSONThe following example target template currently has the PlayersTable resource, and now also contains GamesTable.Example JSON
- Repeat steps 2-3 to update the source stack again, this time to delete the target resource from the stack.
- Perform an import operation to add GamesTable to the target stack.On the Stacks page, with the parent stack selected, choose Stack actions, and then choose Import resources into stack.[The Import resources into stack option in the console.]Read the Import overview page for a list of things you're required to provide during this operation. Then, choose Next.On the Specify template page, do one of the following, and then choose Next.Choose Amazon S3 URL, and then specify a URL in the text box.Choose Upload a template file, and then browse for a file to upload.On the Identify resources page, identify the resource you're moving (in this example, GamesTable).Under Identifier property, choose the type of resource identifier. For example, an AWS::DynamoDB::Table resource can be identified using the TableName property.Under Identifier value, type the actual property value. For example, GamesTables.[The Identify resources page in the console.]Choose Next.On the Specify stack details page, modify any parameters, and then choose Next. This automatically creates a change set.ImportantThe import operation fails if you modify existing parameters that trigger a create, update, or delete operation.On the Review stack-name page, confirm that the correct resource is being imported, and then choose Import resources. This automatically executes the change set created in the last step. Any stack-level tags are applied to imported resources at this time.The Events pane of the Stack details page for your parent stack displays.[The Events tab in the console.]NoteIt's not necessary to run drift detection on the parent stack after this import operation because the AWS::CloudFormation::Stack resource is already managed by AWS CloudFormation.
DeletionPolicy
Controlling access with AWS Identity and Access Management
stack-level tags
Refactor a stack using the AWS CLI
- In the source template, specify a Retain DeletionPolicy for the resource you want to move.In the following example source template, GamesTable is the target of this refactor.Example JSON
- Update the source stack to apply the deletion policy to the resource.
- Remove the resource, related parameters, and outputs from the source template, and then add them to the target template.The source template now looks like the following.Example JSONThe following example target template currently has the PlayersTable resource, and now also contains GamesTable.Example JSON
- Update the source stack to delete the GamesTable resource and its related parameters and outputs from the stack.
- Create a change set of type IMPORT with the following parameters. --resources-to-import does not support inline YAML.The AWS CLI also supports text files as input for the resources-to-import parameter, as shown in the following example.In this walkthrough, file://resourcesToImport.txt contains the following.
- Review the change set to make sure the correct resource is being imported into the target stack.
- Execute the change set to import the resource into the target stack. Any stack-level tags are applied to imported resources at this time. On successful completion of the operation (IMPORT_COMPLETE), the resource is successfully imported.
DeletionPolicy
stack-level tags
It's not necessary to run drift detection on the target stack after this import operation because the resource is already managed by AWS CloudFormation.