Is there a hook to run some code just before synth?

Similar to How to run certain scripts/commands after stack creation, I’d like to execute one or more functions declared on my stack, just before synth.

For example, I currently have a stack that accumulates data everytime some functions are called, and then needs a final call to apply that data:

const publicApiServiceStack = new DcxServiceStack(TerraformApplication, "publicApi", profile, awsRegion, InfrastructureLayers.API_WEB, {
    name: 'publicApi',
    openApiDefinitionFilePath: 'C:/_workspaces/test-service-container/docs/open-api.yaml'


I’d like that final finalizeApiGateway() call to remain private and be invoked automatically.

Is there a hook I could use?

I’ve spotted prepareStack() in v0.9, would that work?


You might be able to use aspects to accomplish this since they run as the part part of synth.
Overriding prepareStack may work, but I don’t believe that’s an intended use case.
Lifecycle Hooks · Issue #682 · hashicorp/terraform-cdk · GitHub is meant to more directly expose hooks into the process.

1 Like

thanks - I am curious about using aspects in the meantime - might play with that idea…