cdktf watch
command
#801
Labels
cdktf-cli
enhancement
New feature or request
feature/watch
priority/important-soon
High priority, to be worked on as part of our current release or the following one.
Milestone
The
watch
command is supposed to watch source files and to automatically do acdktf deploy (--auto-approve)
when they change to support quickly iterating on infrastructure (especially useful for serverless use-cases).Community Note
Description
Add a
watch
command to thecdktf-cli
package. When executed, this command islong-running and keeps watching the source directory for changes to files. When
a change is detected, it will run the
app
script of thecdktf.json
and tobuild and synthesize the application.
After running the
synth
it will compare thecdk.tf.json
output with aprevious version (probably using hashes kept
in-memory)
and determine whether changes need to be deployed. If there are any, it will
automatically deploy them (auto-approving), ideally also skipping the
diff
/plan
phase to save time.To make sure the user knows that the initial iteration of the
watch
commanddeploys automatically, the
--auto-approve
flag is required on thewatch
command until it supports a manual approval step before deploying. So, in the
first iteration only
cdktf watch --auto-approve
will work butcdktf watch
will fail with a short note about this behaviour.
By using supplying
-refresh=false
to Terraform on subsequent deploys, we canpossibly further speed up things.
Determining the source directory
For a first iteration the command could watch any file in the same directory as
the
cdktf.json
file, except for the build / synth output of the CDKTF andexcept for files specified in the
.gitignore
file.If this might impose problems, the source directory could also be inferred for
the target language, however it might be useful to expose an option (on the CLI
command and/or in the
cdktf.json
config) as the code structure used by userscould differ quite substantially.
Handling subsequent changes
If new file updates are detected while a deploy is currently running a second
deploy should be queued so that changes can always be expected to get applied
(even if they take longer this way). In future iterations this could be made
smarter (e.g. just redoing the
synth
step if the deploy itself wasn't runningyet).
Handling errors
If an error happens, the
watch
should continue running but display a messageto the user that informs him that something failed. Possible errors include:
Todos for the first iteration
cdktf watch --auto-approve
watch
command takes a stack as argument or uses the only one available if there's just one.gitignore
forsynth
synth
when source files changedgomon
). A possible way could be to have the "deploy watcher" listen on the specificcdk.tf.json
output file from the prior synth step.cdk.tf.json
for a stack and keeps the hash in memorySupports deploying via Terraform Cloud Remote execution (ignored for now)--auto-approve
to speed up things--stack
was not found (currently the server just exits and we get an connection error - that should be changed)watch
command (including importance of .gitignore file for synth, include that it does not support nested .gitignore files yet).gitignore
files for synthstacktrace
part of thecdk.tf.json
which does not affect the deploy or follow-up issue created for this improvement -> No issue created as the stacktrace does not change when a resource is moved around in the codebase.cdk.tf.json
and asset files (for that stack) in outDir (e.g.cdktf.out
) fordeploy
(not just all, as a local file resource could write a new file there)Improvements for future iterations
The following sections contain improvements for the aforementioned first minimal
viable implementation. They will probably be converted into follow-up issues after the first iteration of this feature is finished (depending on whether they are still relevant then).
Manual Approval
When invoking
cdktf watch
without the--auto-approve
flag the user has toconfirm applying the changes by pressing the enter key.
Detecting which resources changed
When checking the new
cdk.tf.json
against the previous version, we could checkwhich resources were changed and supply them as a
-target
to the Terraform CLI. As we don't expect outside changes to our resources while
using
watch
we could save the time Terraform would take to refresh all theother resources. This would further speed up the deployment of the actual
changes and improve the user experience.
This might possibly not be worth the effort if supplying
-refresh=false
tosubsequent runs already speeds up things enough. However we might still want to
test this.
Make use of target language tooling for recompiling
Most of the target languages of the CDKTF have tooling for watching the source
files and incrementally compiling the resulting binary (e.g.
tsc --watch
,gradle --watch-fs
[Maven does not seem to support watching],gomon
). Wecould make use of this to speed up the build and synth part of the
cdktf watch
command. We could e.g. add a
watch
option to thecdktf.json
config whichcontains a command that is spawned by the
cdktf watch
command.How exactly the
watch
command would then do thesynth
has still to beexplored.
References
The text was updated successfully, but these errors were encountered: