This section is not necessary to complete the workshop, but we
recommend that you take the time to see how cdk watch
can speed up your personal deployments.
CDKTF Watch #
The watch command is experimental, so you should only use it in development environments. It also automatically deploys all changes without asking for confirmation.
We can do better than calling cdktf deploy
each time.
cdktf watch
is similar to cdktf deploy
except that instead of being a one-shot
operation, it monitors your code and assets for changes and attempts to perform a
deployment automatically when a change is detected.
Once we set it up, we can use cdktf watch --auto-approve
to detect changes that require
full Terraform apply.
Modify your cdktf.json
file
#
When the cdktf watch
command runs, the files that it observes are determined by the
"watchPattern"
setting in the cdktf.json
file. the watchPattern
expects an array
of glob patterns, e.g. ["main.ts", "../constructs/**/*.ts", "lib/*.ts"]
. See the configure files to watch section of configuration file overview.
Your cdktf.json
file should look similar to this:
{
"language": "typescript",
"app": "npx ts-node main.ts",
// ...
"watchPattern": [
"./**/*.ts",
]
}
As you can see, the sample app comes with a suggested "watchPattern"
setting. We do in
fact want to observe our .js
files in the lambda
folder, so let’s add
"lambda/*.js"
to the list:
{
"language": "typescript",
"app": "npx ts-node main.ts",
// ...
"watchPattern": [
"./**/*.ts",
"lambda/*.js",
]
}
Now you’re all set to start watching!
Running cdktf watch
#
First, call cdktf watch
:
Due to its potential destructive behaviour, you have to explicitely set the --auto-approve
flag.
cdktf watch --auto-approve
This will trigger an initial deployment and immediately begin observing the files
we’ve specified in cdktf.json
.
Let’s change our lambda asset code in lambda/hello.js
one more time:
exports.handler = async function(event) {
console.log("request:", JSON.stringify(event, undefined, 2));
return {
statusCode: 200,
headers: { "Content-Type": "text/plain" },
body: `Good Night, CDKTF! You've hit ${event.path}\n`
};
};
Once you save the changes to your Lambda code file, cdktf watch
will recognize that
your file has changed and trigger a new deployment.
Wrap Up #
The rest of this tutorial will continue using cdktf deploy
instead of cdktf watch
.
But if you want to, you can simply keep cdktf watch
on. If you need to make a full
deployment, cdktf watch
will call cdktf deploy
for you.
For a deeper dive on cdktf watch
use cases, read
Increasing Development Speed with CDKTF Watch.