Skip to content

Importing existing resources into a cloudformation stack: dynamodb

Updated: at 08:07 AM

When managing AWS infrastructure, we often need to adjust our CloudFormation (CF) stacks while preserving critical resources like DynamoDB tables or Container Registry. In this blog, we’ll explore how we successfully imported three existing DynamoDB tables into a new CF stack after deleting their original stacks, while ensuring no data loss or service interruption.

Context

We had two CF stack consisting of following resources. We’ve used example names for the simplisity and purpose of this writing.

Our resources were IaC managed i.e. written by CDK. Our goal was to delete the existing CloudFormation stacks while keeping only the dynamoDB tables intact and eventually import them into a new stack. Important to note we have manually turned on Point-In-Time Recovery (PITR) and deletion protection to prevent anyaccidental delete and recovery. But initially when above two stacks were written removalPolicy for dynamodb was set to destroy meaning when the CF stack will be deleted this assoiciated resource will be deleted as well (removalPolicy: RemovalPolicy.DESTROY). Perhaps we didn’t may much attention this particular point which bit us badly few moments later 😄!

Migration Process

Step 1: Delete stack-a

Cloudformation -> Stacks -> stack-a -> Delete

But deletion didn’t go as planned due conflict of configuration:

Anyway, this conflict resulted in the stack entering a DELETE_FAILED state. This means we can’t actually update the stack via CDK anymore. Because when the deletion failed we tried updating removalPolicy: RemovalPolicy.RETAIN using CDK but it failed. We tried couple of other ways but none of them actually worked.

Rescue:

Step 2: Delete stacks-b

Lesson learned from the stack-a, so this time around we updated the CF template using CDK (removalPolicy: RemovalPolicy.RETAIN) before attempting Cloudformation -> Stacks -> stack-a -> Delete:

Above updated policies ensured table deletion will be skipped during stack’s deletion. Therefore, stacks-b stack proceeded without any issues, and both tables were safely retained as orphaned resources.

Step 3: Import orphaned resources

With the original stacks deleted, the three DynamoDB tables were now orphaned resources but still fully functional and fargate service consuming those tables were working as expected.

resource state simplified arch

Considerations

References