Sub topic 20
Bringing existing resources under management
This chapter assumes you can write a template with Resources and Outputs, run stack commands from the AWS CLI, and understand DeletionPolicy and drift detection from earlier chapters.
Why import resources
Many accounts hold resources that were created by hand or by another tool. Recreating them in CloudFormation would mean downtime or data loss. A bucket full of objects can't simply be deleted and rebuilt.
Resource import attaches an existing resource to a logical ID in a stack. CloudFormation does not create, modify, or replace the resource. After the import, later updates and deletes go through the stack like any other resource.
You can also use import to move a resource from one stack to another, or to recover a resource that was removed from a stack with Retain.
What you need before importing
- A template entry for each resource. The entry gives the logical ID, the resource type, and properties that describe the resource as it exists now.
- A DeletionPolicy attribute on every imported resource. The template must set one, and
Retainis the safe choice. - A resource identifier. Each type has a primary identifier, such as
BucketNameforAWS::S3::Bucket. You pass its value with the import request. - A supported resource type. Not every type can be imported. Check the "resources that support import" list in the CloudFormation documentation.
- Permissions. You need the CloudFormation permissions and also the permissions for the resource type itself.
The resource can belong to only one stack at a time. An import does not check that your template properties match the real resource. If they differ, the stack records the template as its desired state, and the difference appears as drift.
The import workflow
An import is a change set of type IMPORT. You review it and then execute it, as you would any change set.
Step 1: Describe the resource in the template
Suppose a bucket named amzn-s3-demo-existing already exists. Write it into import.yaml:
AWSTemplateFormatVersion: "2010-09-09"
Description: Adopt an existing bucket
Resources:
LegacyBucket:
Type: AWS::S3::Bucket
DeletionPolicy: Retain
Properties:
BucketName: amzn-s3-demo-existing
VersioningConfiguration:
Status: EnabledMatch the live configuration as closely as you can. Check the bucket's real settings first, for example its versioning status.
Step 2: List the resources to import
Create a file named resources.json. Each entry maps a logical ID in the template to the real resource:
[
{
"ResourceType": "AWS::S3::Bucket",
"LogicalResourceId": "LegacyBucket",
"ResourceIdentifier": {
"BucketName": "amzn-s3-demo-existing"
}
}
]Step 3: Create, review and execute the change set
aws cloudformation create-change-set \
--stack-name legacy \
--change-set-name adopt-bucket \
--change-set-type IMPORT \
--resources-to-import file://resources.json \
--template-body file://import.yaml
aws cloudformation describe-change-set \
--stack-name legacy \
--change-set-name adopt-bucket
aws cloudformation execute-change-set \
--stack-name legacy \
--change-set-name adopt-bucketIf the stack legacy does not exist, the IMPORT change set creates it. The stack passes through IMPORT_IN_PROGRESS and ends in IMPORT_COMPLETE. In the review step, the change shows the action Import for the resource.
Limits during an import
An import operation only imports. You can't add new resources, change other resources' properties, or delete resources in the same change set. Do those in a normal update before or after the import.
Parameters are an exception to this. You can add or change them when the new values are needed by the resources you import.
Verify with drift detection
After the stack reaches IMPORT_COMPLETE, run drift detection:
aws cloudformation detect-stack-drift --stack-name legacy
aws cloudformation describe-stack-resource-drifts \
--stack-name legacy \
--stack-resource-drift-status-filters MODIFIEDA MODIFIED resource means your template and the real configuration disagree. Edit the template to match reality, or run a normal update to push the template's values onto the resource. A stack update applies the template, so check the change set before you execute it.
Moving a resource between stacks
Import also lets you move a resource without recreating it:
- In the source stack, set
DeletionPolicy: Retainand run an update. - Remove the resource from the source template and update again. The resource stays in your account but leaves the stack.
- Add the resource to the destination template and run an
IMPORTchange set.
Skipping the first step means the removal deletes the resource.
Common errors
| Symptom | Likely cause |
|---|---|
| The change set fails validation | A resource has no `DeletionPolicy`, or the type does not support import |
| The identifier is rejected | The `ResourceIdentifier` key or value does not match the type's primary identifier |
| `IMPORT_ROLLBACK_COMPLETE` | The resource was not found, or another stack already manages it |
| Drift appears right after import | The template properties differ from the live configuration |
When an import fails, describe-stack-events shows the reason for each resource, the same way it does for any failed operation.
Exercise
You want to move a database from stack A to stack B without deleting it. List the steps in order.
Show answer
Set DeletionPolicy: Retain on the database in stack A and update stack A. Remove the database from stack A's template and update again, which leaves the database running but unmanaged. Add the database to stack B's template with a DeletionPolicy, then create and execute an IMPORT change set using the database's identifier. Run drift detection on stack B to confirm the template matches the real resource.
Exercise
An import change set for a bucket is rejected before it starts. The template contains the bucket with a BucketName and no other attributes. What is the most likely problem?
Show answer
The resource has no DeletionPolicy attribute. Every resource being imported must declare one, and Retain is the safe value.