

cdktf init and runs yarn install to fetch the project dependencies.
The simplified project code now appears as follows:
yarn install completes, Arthur refreshes VS Code to recognize the new dependencies. He then performs a quick “Hello World” test by deploying a simple Terraform output:
cdkout folder to review the stack and the generated Terraform configuration files. An example of the synthesized Terraform state file is shown below:
terraform init after adding the provider), in CDKTF you must declare the dependency explicitly.
First, Arthur installs the CDKTF core library:
yarn cdktf deploy, the output confirms the creation of the file resource:
cdktf synth followed by cdktf deploy, CDKTF generates a Terraform plan indicating that one resource will be added (the local file). The output looks similar to:
- A construct represents any resource or infrastructure component in your construct tree.
- A provider manages the connection and configuration for resources (e.g.,
provider.LocalProvider). - A resource (such as
file.File) defines a local file, similar to HashiCorp Configuration Language (HCL) in native Terraform projects.
yarn cdktf synth creates the necessary Terraform configuration, and yarn cdktf deploy applies the configuration after displaying a detailed plan.
If Arthur decides to modify the README file content, Terraform will replace the file resource imperatively. For example, updating the README content:
Local files are managed imperatively by Terraform. Any modification to the file content will trigger a resource replacement. For cloud resources, such as an AWS S3 bucket, updates might not require replacement, and lifecycle rules can be applied using the
lifecycle configuration.readme-file to readme-file-2) results in the destruction of the old resource and creation of a new one.
To add a package.json file along with the README, Arthur extends his code:
yarn cdktf deploy, Terraform confirms that the new file resource has been added:
package.json confirms its content:
Terraform State and Resource Updates
Consider the following points regarding Terraform state and resource updates:-
Where is the Terraform state stored?
By default, Terraform stores state locally. You can find atf.statefile within thecdktf.outfolder of your stack, which contains details such as the filename and file permissions. -
What happens when the README file content is modified?
Any change to the README file content forces a replacement of the resource, unless you configurelifecycle: { ignoreChanges: ['content'] }. -
What occurs if the logical ID for the README file is changed?
Changing the logical ID removes the existing resource from the construct tree and creates a new one, prompting the destruction of the old file and the creation of a new file. -
How do you add a
package.jsonfile alongside the README file?
As demonstrated above, you can introduce an additional file resource specifying both the filename and content forpackage.json.
