TL;DR: What is the role and purpose of the prepare(): void
method in AWS CDK's Construct
class? When and how should it be utilized or avoided?
The information provided about prepare()
states:
prepare()
function is called after child constructs have been prepared.Note that this feature is advanced - only proceed if you fully grasp the consequences.
... yet, the exact "consequences" are not explicitly outlined.
Based on my observation of certain AWS CDK projects (like this one), the internal arrangement of constructs and stacks seems to be predominantly set up within the constructor()
function (here's a typical example).
I personally find this approach less than ideal for class usage, but I struggled to identify an alternative method of organizing stacks and constructs until I encountered the prepare()
function and its indication ("...will be called after child constructs have been prepared"). My assumption was that it might be intended for usage like this:
export class SomeStack extends AWS.Stack {
prepare() {
// add constructs
// define permissions
// essentially, perform all tasks done in `constructor()` here: https://github.com/aws-samples/aws-cdk-examples/blob/master/typescript/api-cors-lambda-crud-dynamodb/index.ts
}
}
... although this interpretation doesn't seem to align perfectly with the wording "perform final modifications" as described in the method.