Welcome to our guide on how to use the CDK Stack Demo for an AWS Aspect. This demo is designed to help you understand how you can use this aspect to simplify stack refactorings.
This Aspect is heavily inspired by sst and adds a similar functionality to native AWS CDK.
Deploy the application. Start by deploying the application to set up your environment.
Modify the table ID. Next, you can either change the ID of one of the
tables in lib/CdkTableStack.ts
or delete an alarm in lib/CdkMonitoringStack.ts
.
Deploy again. After making your changes, deploy the application once more.
Under normal circumstances, this second deployment would fail. This is because
CDK detects that an export of CdkTableStack is no longer in use and deletes it.
The usual solution would be to use a command like this.export(...)
and deploy twice.
So, what does the Aspect do in this scenario?
Interacting with the CloudFormation SDK client: The Aspect utilizes the CloudFormation SDK client to describe the stacks. This allows it to understand the current state and configuration of the stacks.
Listing and identifying exports: Next, the Aspect lists all the exports and identifies the one that is about to be removed. This step ensures that no necessary exports are accidentally deleted.
Re-adding necessary exports: If the Aspect finds an export that is still in use, it doesn't just leave it be. Instead, it re-adds a dummy export for the one still in use. This step prevents any disruptions that could occur from the removal of a still-used export.
While the Aspect is a valuable tool, it's crucial to understand its nuances to use it effectively. Here are some key points to consider:
- Dependency on external network requests: The final CloudFormation template generated by the Aspect relies on external network requests and not only your application code
- Persistence of automatically added exports: The Aspect has a feature where it automatically adds exports. However, these exports might persist even when they're no longer needed when there are no other changes. This is because the stack is detected as unchanged.
- Use of forced synthesis: The Aspect operates using
app.synth({force: true})
. This forces the synthesis of the CloudFormation templates, which might not always be desirable, especially in a production environment.