1 Answer
- Newest
- Most votes
- Most comments
5
Hello,
Handling conditions in CDK can indeed be tricky. By default, the removalPolicy on resources like S3 buckets is set to RemovalPolicy.RETAIN (see CDK bucket documentation), which keeps the bucket even when the stack is deleted. You might want to set it to RemovalPolicy.DESTROY if you're okay with the bucket being deleted when you remove the stack, so you can redeploy it without issues.
If you want to keep the bucket but still delete and recreate the stack, you could try:
- Adding a boolean context variable in your CDK stack to control whether the S3 bucket should be created. (context variables)
- Creating the S3 bucket outside of CDK using the AWS SDK, so it remains independent of your stack lifecycle.
- You can also use a CfnCondition and a Parameter Store like this guy (link)
Hope it helps :)
Relevant content
asked 4 years ago
