CDKTF Helpers
Helpers for creating the stacks of cdktf
, inspired by class component of React.js
.
Quick Start
export class MainStack extends CdktfStackComponent<Props, State> {
beforeCreateResources() {
new AwsProvider(this, "AWS", {
region: "us-west-1",
});
const config = JSON.parse(fs.readFileSync("./config.json", "utf-8"));
this.setState("instaceType", config.instanceType);
}
createResources() {
const ami = this.props.ami;
const ec2Instance = new Instance(this, "compute", {
ami,
instanceType: this.state.instanceType,
});
this.setOutput("ec2_instance", ec2Instance);
}
}
const app = new App();
CdktfComponentFactory.createComponent(app, MainStack.name, {
ami: "ami-01456a894f71116f2",
});
Component
A CdktfStackComponent
comes with a pair of props
and state
to help you manage the data flow of your CDKTF stack.
To define a CDKTF component class, you need to extend CdktfStackComponent
:
import { CdktfStackComponent } from "cdktf-helpers";
type Props = { ami: string };
type State = { ec2Instance: Instance };
export class MainStack extends CdktfStackComponent<Props, State> {
beforeCreateResources() {
}
createResources() {
}
afterCreateResources() {
}
}
Component API
constructor()
type constructor = (
scope: Construct,
id: string,
props?: Record<string, any> & { stackName: string }
) => CdktfStackComponent;
beforeCreateResources()
It is invoked immediately after a component is initialised.
createResources()
It is invoked after beforeCreateResources
is executed.
You can create the resources in this method.
afterCreateResources()
It is invoked after createResource
is executed.
Component Factory
CdktfComponentFactory.createComponent
import { CdktfComponentFactory } from "cdktf-helpers";
import { App } from "cdktf";
import MainStack from "./main_stack.ts";
const app = new App();
CdktfComponentFactory.createComponent(app, MainStack.name, {
ami: "ami-01456a894f71116f2",
});
CdktfComponentFactory.createComponentAsync
Factory also provide async method, you can use it to create component asynchronously.
import { CdktfComponentFactory } from "cdktf-helpers";
import { App } from "cdktf";
import MainStack from "./main_stack.ts";
const app = new App();
await CdktfComponentFactory.createComponentAsync(app, MainStack.name, {
ami: "ami-01456a894f71116f2",
});
Component lifecycle
When an instance of the CDKTF component is generated by createComponent()
, these methods are called in the below order:
constructor()
beforeCreateResources()
createResource()
afterCreateResources()
- Create Terraform outputs if any
- If you have triggered
setOutput
while creating resources, it will be called after all functions are executed.