From a8d31977f299e9390c1cbb15287b75e596f7da46 Mon Sep 17 00:00:00 2001 From: Ramsay Leung Date: Sat, 2 Dec 2023 10:09:45 -0800 Subject: [PATCH] Update the post "How to share resource between CDK stack" by adding a loose couping solution. --- ...ow_to_share_resource_between_cdk_stacks.md | 50 ++++++++++++++++++- themes/PaperMod | 2 +- 2 files changed, 49 insertions(+), 3 deletions(-) diff --git a/content/en/post/2023/how_to_share_resource_between_cdk_stacks.md b/content/en/post/2023/how_to_share_resource_between_cdk_stacks.md index 2d5fe502..6c3423bf 100644 --- a/content/en/post/2023/how_to_share_resource_between_cdk_stacks.md +++ b/content/en/post/2023/how_to_share_resource_between_cdk_stacks.md @@ -1,7 +1,7 @@ +++ title = "How to share resource between CDK stacks" date = 2023-06-28T09:41:00-07:00 -lastmod = 2023-06-28T15:52:51-07:00 +lastmod = 2023-12-02T10:09:35-08:00 tags = ["aws"] categories = ["aws"] draft = false @@ -104,8 +104,54 @@ The synthesized CFN template for `GlueStack`: This is the way about how to share value between two stacks. +--- -## 4 Reference {#reference} + +## 4 Loose couping solution {#loose-couping-solution} + +Updated on 2023-12-02 + +People learn from mistake. + +After applying this practice in my project, I recently learn that it's not good practice to share resource across stack. + +With using `export/import`, I tightly couple my stacks with a commitment that I can never update that unless I remove that couping later on. + +It means it will become a disaster[^fn:1] whenever I need to update/delete the `S3Bucket`, `CloudFormation` will raise an error, complaining something like: "ServiceStack cannot be deleted as it's in use by GlueStack". + +A better practice I learnt is adding a loose couping between `ServiceStack` and `GlueStack` by sharing a constant variable: + +1. Define a constant variable somewhere: + ```javascript + export const Constants = { + MyBucketName: 'TestBucket' + } + ``` + +2. Refine the definition of `s3Bucket` + ```javascript + import { Bucket } from 'aws-cdk-lib/aws-s3'; + + const s3Bucket = new Bucket(this, 'MyBucketId', { + bucketName: Constants.MyBucketName, + }); + ``` + +3. Refer the `s3Bucket` in `GlueStack` by `MyBucketName` instead of CDK exported reference + ```javascript + const requiredS3BucketName = Constants.MyBucketName; + ``` + +Therefore, these two stacks are not directly coupled, but they are referencing the same constant variable. + +Then, CloudFormation won't prevent you from updating the `S3Bucket` as there is not direct relation between these two stacks anymore. + +This is the benefit of loose couping. + + +## 5 Reference {#reference} - [API Document: class CfnOutput (construct)](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.CfnOutput.html) - [AWS Cloud Development Kit (AWS CDK) v2](https://docs.aws.amazon.com/cdk/v2/guide/stacks.html) + +[^fn:1]: \ No newline at end of file diff --git a/themes/PaperMod b/themes/PaperMod index 031e2ba5..b288ede8 160000 --- a/themes/PaperMod +++ b/themes/PaperMod @@ -1 +1 @@ -Subproject commit 031e2ba57ea2b7b1de2e73c20bf2b5716f654fac +Subproject commit b288ede80cbd70151e2f07f2e09ad6258e64f7fa