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.

cdktf deploy does not respect `--output` directory for `.terraform/terraform.tfstate`

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

cdktf & Language Versions

cdktf v0.10.1 Python 3.9.12

Affected Resource(s)

When running cdktf deploy --output=/deployed/deployed_cdktf_state from /tmp

The synthesized cdk.tf.json ends up containing:

"backend": {
      "local": {
        "path": "/tmp/terraform.stack-name.tfstate"
      }
    },

ā€¦and saves the tfstate there (/tmp/terraform.stack-name.tfstate) on deployment - which is surprising.

Expected Behavior

Expected /tmp/terraform.stack-name.tfstate to be at /deployed/deployed_cdktf_state/terraform.stack-name.tfstate instead.

Surprisingly /deployed/deployed_cdktf_state/stacks/stack-name/.terraform/terraform.tfstate is created under the stated --output directory (/deployed/deployed_cdktf_state) but it too contains:

"backend": {
    "type": "local",
    "config": {
        "path": "/tmp/terraform.stack-name.tfstate",
        "workspace_dir": null
    },
},
  1. Is there a way to explicitly use a different directory for terraform.stack-name.tfstate?
  2. Is this a bug or expected?

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:5
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
ansgarmcommented, Apr 21, 2022

Hi @gc-ss, you donā€™t have to use another backend provider, you could also explictly add a LocalBackend yourself. However, it is currently not possible to pass any configuration to the LocalBackend that is automatically added in case no backend was defined explicitly.

If you donā€™t want to configure the backend explicitly in every stack, you could subclass the TerraformStack class and use that new stack class for all your stacks. That stack could then add a correctly configured LocalBackend for example in its constructor. Another option would be to use an Aspect that adds the required LocalBackend.

1reaction
DanielMSchmidtcommented, Apr 21, 2022

You would do

class LocalBackendTerraformStack(
    TerraformStack,
):
   def __init__(
        self, scope: Construct, stack_name: str, *, tfstate_file_name="deployed_cdktf_state.tfstate"
    ):
        super().__init__(scope, stack_name)
       LocalBackend(scope=self,
            # Path where the state file is stored. defaults to terraform.${stackId}.tfstate
            path=pathlib.Path(scope.outdir).joinpath(tfstate_file_name).absolute().as_posix(),
            # workspace_dir="/usr/src/generic/deployed/",
        )

We donā€™t need to override the local backend, itā€™s only added if you donā€™t have one configured for your stack. As for the output directory, I disagree with the notion that it should be taken into consideration. More often than not the output directory is in the gitignore (like in all our templates), because if you donā€™t have a strong reason you donā€™t want to commit the HCL JSON files. If we change it like you propose this would mean we ignore the state file. Plus this folder gets cleaned by us, e.g. if you delete a stack, again making you loose your state.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Deploy Lambda Functions with TypeScript and CDK for ...
In this tutorial, you will deploy a CDKTF application made up of two ... that the directories containing the Lambda handlers are not...
Read more >
Use Your Preferred Programming Language with CDKTF
HashiCorp Terraform now supports writing configurations in HCL or your choice of C#, Python, TypeScript, Java, or Go with Cloud DevelopmentĀ ...
Read more >
SweetOps #terraform for September, 2020
Not sure if this from the logs helpsā€¦sure doesn't help me: ... steps: https://github.com/cloudposse/terraform-aws-tfstate-backend#create.
Read more >
Managing Terraform State | Google Cloud Skills Boost
If Terraform doesn't output a message, state locking is still occurring. Not all backends support locking. View the list of backend types for...
Read more >
Hashicorp Terraform & Packer. Kubernetes Boilerplates
HashiCorp Cloud Platform is a fully managed platform for Terraform, Vault, ... nitheeshp.dev: Practical CI/CD Guide to Deploying AWS Infrastructure throughĀ ...
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