question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItĀ collects links to all the places you might be looking at while hunting down a tough bug.

And, if youā€™re still stuck at the end, weā€™re happy to hop on a call to see how we can help out.

API to interact with terraform cli

See original GitHub issue

Community Note

  • Please vote on this issue by adding a šŸ‘ reaction to the original issue to help the community and maintainers prioritize this request
  • Please do not leave ā€œ+1ā€ or other comments that do not add relevant new information or questions, they generate extra noise for issue followers and do not help prioritize the request
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment

Description

It would be very useful to have an API to interact with terraform cli (not cloud) from cdk.

Example below:

app = App()
MyStack(app, "automate-everything")
synth = app.synth()
plan = app.plan(synth)

if plan.changes is not None:
    if len(plan.changes.module.custom.fruits) > 4:
       app.apply(synth, target='module.custom.fruits')

References

https://github.com/hashicorp/terraform-cdk/issues/225

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:66
  • Comments:14 (5 by maintainers)

github_iconTop GitHub Comments

7reactions
skorfmanncommented, Jun 20, 2021

Was playing around with this a bit. While it requires the usage of JS / Typescript, itā€™s sort of possible to drive synth and deploy / destroy bypassing the full cdktf-cli workflow. Itā€™s a bit clunky, but works. Please be aware, that this is using internal APIs of the cdktf-cli which might break without prior warning.

deploy

import { SynthStack } from 'cdktf-cli/bin/cmds/helper/synth-stack';
import { TerraformCli } from 'cdktf-cli/bin/cmds/ui/models/terraform-cli';

const setup = async () => {
  const stacks = await SynthStack.synth('node stack.js', 'cdktf.test.out');
  const cli = new TerraformCli(stacks[0]);
  await cli.init();
  await cli.deploy('', (chunk: Buffer) => (console.log(chunk.toString('utf-8'))));
}

(async function() { await setup() } ());

destroy

import { SynthStack } from 'cdktf-cli/bin/cmds/helper/synth-stack';
import { TerraformCli } from 'cdktf-cli/bin/cmds/ui/models/terraform-cli';

const teardown = async () => {
  const stacks = await SynthStack.synth('node stack.js', 'cdktf.test.out');
  const cli = new TerraformCli(stacks[0]);
  await cli.init();
  await cli.destroy((chunk: Buffer) => (console.log(chunk.toString('utf-8'))));
}

(async function() { await teardown() } ());

Iā€™m using this right now for driving automated test setups / teardowns. Since this skips generating the plan and is running headless, itā€™s also a bit faster compared to using the cdktf-cli directly.

5reactions
kc-dot-iocommented, Jan 21, 2022

Thanks for these examples @skorfmann! This is my new favorite way to use CDKTF. Iā€™d love to see a more formal APIā€¦

A few things from my experience that would help thisā€¦

  1. Better error handling with remote runners using try/catch or promise
  2. Better API for handing / templating response to stdin/stdout
  3. Ideally JSON events with lifecycle hooks would be great
  4. API support for multi-stack delpoyments / cross stack references

Looking forward to seeing this evolve. This is the most excited Iā€™ve been about IaC in a while.

Read more comments on GitHub >

github_iconTop Results From Across the Web

API-driven Runs - Terraform Cloud - HashiCorp Developer
The CLI-driven run workflow, which uses Terraform's standard CLI tools to execute runs in Terraform Cloud. Summary. In the API-driven workflow, workspaces areĀ ......
Read more >
Provisioning Resources using Terraform Cloud with an API ...
Terraform Cloud enables provisioning of resources using a VCS driven workflow, a CLI driven workflow as well as an API driven workflow.
Read more >
Docs overview | hashicorp/tfe - Terraform Registry
This provider is used to interact with the many resources supported by Terraform Cloud. As Terraform Enterprise is a self-hosted distribution of Terraform...
Read more >
CLI-driven Runs - Terraform Enterprise
The UI/VCS-driven run workflow, which is TFE's primary mode of operation. Ā· The API-driven run workflow, which is more flexible but requires you...
Read more >
Create a Terraform Cloud Workspace
Similar to how a cloud provider configuration requires a token to interact with the provider, the Fake Web Services provider requires a Terraform...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found