Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API to interact with terraform cli #237

Open
JayDoubleu opened this issue Jul 23, 2020 · 17 comments
Open

API to interact with terraform cli #237

JayDoubleu opened this issue Jul 23, 2020 · 17 comments
Labels
enhancement New feature or request feature/programmatic-api priority/important-longterm Medium priority, to be worked on within the following 1-2 business quarters. size/large estimated < 1 month ux/integration

Comments

@JayDoubleu
Copy link

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

#225

@JayDoubleu JayDoubleu added the enhancement New feature or request label Jul 23, 2020
@skorfmann
Copy link
Contributor

The general idea of this is quite appealing to me.

However, At the moment, the cdktf cli is almost independent from a synthesized Stack. Means, it's really just interfacing with the Terraform CLI and interacting with the generated plan and the stdout / stderr output from it.

In the cdktf cli, we do have the plan at hand , synth isn't available as it's just doing a terraform apply in the cdktf.out directory. Means, as long as it's just about analyzing a plan and dynamically changing cli arguments, this could theoretically work within the current cli concept. However, I don't think this would be part of the app, more like some sort of hook in the CLI. Since the cli is just available in Typescript, it'd likely be Typescript code.

I think a similar concept could work for #219

@1mamute
Copy link

1mamute commented Jun 15, 2021

Maybe we can use terraform-exec to interact with synthesized outputs from the stack?

@skorfmann
Copy link
Contributor

Maybe we can use terraform-exec to interact with synthesized outputs from the stack?

Since terraform-exec is a go module, it's not the best fit for this project since it's mostly written in Typescript. We sort of built something similar in Typescript.

@1mamute
Copy link

1mamute commented Jun 17, 2021

Maybe we can use terraform-exec to interact with synthesized outputs from the stack?

Since terraform-exec is a go module, it's not the best fit for this project since it's mostly written in Typescript. We sort of built something similar in Typescript.

Mind sharing the link or is it not publicly avaiable?

@skorfmann
Copy link
Contributor

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.

@drandell
Copy link

@skorfmann I think this would be a great feature, it would mirror some of the functionality available in pulumi with its API. Is there a potential python approach to your sample?

@skorfmann
Copy link
Contributor

Is there a potential python approach to your sample?

Since the CLI is written in Typescript only, I don't think there's a similar way to do this right now - except for shelling out und using the cli directly. However, cross language support for programmatic interaction is something we'd like to address - but I can't give you an ETA on that.

@drandell
Copy link

drandell commented Jul 5, 2021

Is there a potential python approach to your sample?

Since the CLI is written in Typescript only, I don't think there's a similar way to do this right now - except for shelling out und using the cli directly. However, cross language support for programmatic interaction is something we'd like to address - but I can't give you an ETA on that.

Took a bit of time, but I managed to play around with JSII and add the necessary files into a local cdktf package, its not clear and I've probably brutalised it somewhat but I've got the following working in python;

def main():
    app = App()
    tfStack = MyStack(app, "python-cdk", GCP_PROJECT_NAME)
    app.synth()
    stack = SynthesizedStack(**tfStack.get_stack_args())
    cli = TerraformCli(stack=stack)
    print(cli.version())
    # print(cli.init())
    # plan = cli.plan()
    # print(plan)
    # print(cli.deploy())
    # print(cli.destroy())

And running that produces;
image

@DanielMSchmidt DanielMSchmidt added needs-priority Issue has not yet been prioritized; this will prompt team review size/large estimated < 1 month ux/integration labels Dec 7, 2021
@kc-dot-io
Copy link
Contributor

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.

@DanielMSchmidt DanielMSchmidt added priority/important-longterm Medium priority, to be worked on within the following 1-2 business quarters. and removed needs-priority Issue has not yet been prioritized; this will prompt team review labels Feb 17, 2022
@danielloader
Copy link

danielloader commented Feb 19, 2022

Just to add to this issue, though it's not anything other than more justification:
I'm looking to design a SaaS deployment project to deploy a simple Lambda Serverless stack with some auxiliary services, and for simplicity sakes was hoping I could utilise CDKTF in a Lambda function to deploy a new customer, patch/update it or delete a customer (fed off a DynamoDB table with streams for some audit log of infrastructure changes).

Pulumi works with the automation API but hesitant to commit to using it when both AWS CDK, and Terraform CDK are maturing and both offer advantages (already a Terraform Cloud customer at work and cloudformation state in AWS appeals in some scenarios). AWS CDK still doesn't have a programmatic interface, and the demo example from @drandell looks promising - though as cited, you'd want a formalised API before taking this into production.

Ideally the interface would allow you to run a stack synth, stack deploy and then take the outputs from the resolved deployed stack to do something with (insertion into dynamo/webhooks for notification of deployment and triggering an email etc).

@JayDoubleu

This comment was marked as off-topic.

@danielloader
Copy link

@JayDoubleu Sounds good, using Pulumi cloud for state or S3 etc?
I do wonder if it ultimately if it matters what to use, to do the work - The only selling point for CDKTF for me is we're already using Terraform Cloud for state management - and I'd be happy to continue doing so with this functionality in AWS Lambda functions - it's just not formalised into a public API so can't risk it - though I guess the whole CDKTF API isn't formalised yet.

@JayDoubleu

This comment was marked as off-topic.

@danielloader
Copy link

As a follow up, Pulumi is agonisingly slow to resolve changes compared to CDKTF so guess I'll be hoping this "automation API" can be formalised soonish.
Good job to Hashicorp.

@ansgarm ansgarm added priority/important-soon High priority, to be worked on as part of our current release or the following one. and removed priority/important-longterm Medium priority, to be worked on within the following 1-2 business quarters. labels Nov 8, 2022
@irfanjs
Copy link

irfanjs commented Feb 21, 2023

Hi All,
when can we see this feature release ? Any ETA please ?
we are seriously looking for API integration , how we can call this synth process (plan, apply and destroy) using sprintBoot ?
we are developing some JAVA API which will call terraform to deploy the EKS cluster in AWS .
something like POST http://localhost/api/eks/create will hit the cdktf synth process and deploy the EKS cluster .

please suggest

@xiehan xiehan added priority/important-longterm Medium priority, to be worked on within the following 1-2 business quarters. and removed priority/important-soon High priority, to be worked on as part of our current release or the following one. labels Jun 1, 2023
@anamitra-saikia
Copy link

anamitra-saikia commented Sep 22, 2023

Hi all,
It will be fantastic to have this API feature in Terraform cdk. Any estimated time for the development of this feature?
Also @skorfmann the bypass for cdktf-cli you mentioned is exactly what I was looking for but it is applicable for old versions of the cli. Could you show how to achieve the same in the current version of cdktf-cli?

@irfanjs
Copy link

irfanjs commented Sep 23, 2023

@anamitra-saikia : No workaround yet and havent implemented .
Still waiting for this feature from Terraform-cdk

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request feature/programmatic-api priority/important-longterm Medium priority, to be worked on within the following 1-2 business quarters. size/large estimated < 1 month ux/integration
Projects
None yet
Development

No branches or pull requests