-
Notifications
You must be signed in to change notification settings - Fork 9.6k
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
Configuring one provider with a dynamic attribute from another (was: depends_on for providers) #2430
Comments
fleet is another example of a service for which this is a problem. That provider used to work: if the ip address is an empty string, then it would use a mock API that failed on everything. But that solution is no longer working in Terraform 0.6.3. |
I think this is describing the same issue I wrote up in #2976, in which case unfortunately the problem is a bit more subtle than supporting Terraform is actually already able to correctly handle provider instantiation in the dependency graph, correctly understanding that (in your example) the docker provider instantiation depends on the completion of the EC2 instance. The key issue here is that providers need to be instantiated for all operations, not just When I attempted to define this problem in #2976 I was focused on working with resources that don't really have a concept of "creation", like The workaround for this problem is to explicitly split the problem into two steps: make one Terraform config that creates the EC2 instance and produces the instance IP address as an output, publish the state from that configuration somewhere, and then use the Unfortunately if you follow my above advice, you will then run into the issue that I described in #2976: the |
@apparentlymart: In that issue, you are describing a case "which is very handy when parts of the config are set dynamically from outside the configuration." And you propose to get around the issue by making those resources that represent the outside configuration "pre-refreshed", meaning you skip the create step and immediately go to the read step. But I'm describing a case where parts of the config are set dynamically from inside the configuration. For example, I want to manipulate a Docker or Fleet service that exists on the EC2 instance I just made. Would pre-refreshing help in this case? |
@bitglue no, as my (rather verbose) comment described, having a "true" resource from one provider be used as input to another is not compatible with Terraform's model of separating plan from apply. The only way to solve that, without changing Terraform's architecture considerably, is to break the problem into two separate configurations and then use |
edited a bit to clarify the plan/apply stages @apparentlymart I don't think it's impossible, because After the plan is made and it's time to apply, then the provider can be initialized after having created the EC2 instance, and now it has a proper endpoint and can actually work and create the fleet resources. On subsequent planning runs, the public IP address of the EC2 instance is already known, so planning can happen as usual. Bonus points for refreshing the EC2 instance before initializing the fleet provider to do its refreshing. I'd also think it's not the separation of plan and apply that's really the issue here, but more specifically refresh. You can always You can run into a little trouble if you delete the EC2 instance to which fleet was connecting but after the fleet resources have been created. Now plan can't work. But there are two ways to resolve that situation:
Granted, these resolutions require a little hackish manual action, but it's not a situation I ever hit in practice. I'm sure with a little refinement it could be made less hackish. |
@bitglue it sounds like you're saying that in principle the providers could tolerate their configurations being incomplete until they are asked to do something. That is certainly theoretically true... while today most of them verify their config during instantiation and fail hard if the config is incomplete (as you saw the Docker provider do), they could potentially just let an incomplete config pass and then have it fail if any later operation tries to do any operations. So one thing we could prototype is to revise how Having a prototype of that would allow us to try out the different cases and see what it fixes and when/how it fails. As you said, it should resolve the initial creation case because at that point the |
@apparentlymart That's more or less my thinking, yeah. Though from what I've observed trying to get |
@bitglue I guess the schema validation will catch cases where a field is required but yet empty, so you're right that what I described won't entirely fix it unless we make all provider arguments optional and handle them being missing inside the |
This is issue for postgresql provider as well, when you e.g. want to create AWS RDS instance and then use the port in the provider configuration. This fails, as the provider initializes before RDS instance is created, the port number is returned as "" and that doesn't convert to int:
TF code:
|
Don't specify port, as the RDS instance doesn't exist yet in the moment of postgresQL provider initialization, which then breaks, because port is returned as empty quotes, which doesn't convert to string. See hashicorp/terraform#2430
Interestingly, in your case TF graph does show the provider dependency, yet provider runs in parallel still. This is esp. problematic on destroy, as RDS instance gets destroyed before postgresql provider has chance to destroy the resources it has created, leaving "undestructable" state file behind. See #5340 |
Hello there, I got a similar problem with the Docker provider when used inside a Openstack instance (graph).
$ terraform plan
Error running plan: 1 error(s) occurred:
* Error initializing Docker client: invalid endpoint Even thought I'm using :
Logically it should wait for the instance to be up but sadly the provider is still initialized at the very beginning ... So as a workaround I splitted my project into modules (module.openstack & module.docker) and then execute them one at a time with the $ terraform apply -target=module.openstack && terraform apply -target=module.docker It does the job but make the whole process quite annoying as we must always specify the modules in the good order for each steps (plan, apply, destroy ...). So until we got an option such as |
I've submitted a similar question to the google group on this: https://groups.google.com/forum/#!topic/terraform-tool/OhDdMrSoWK8 The workaround of specifying the modules separately didn't seem to work for me. Weirdly the docker provider was spinning up a second EC2 instance?! I've also noticed that
|
Issue #4149 was my later proposal to alter Terraform's workflow to better support the situation of having a single Terraform config work at multiple levels of abstraction (a VM and the app running on it, as in this case). It's not an easy fix but it essentially formalizes the use of -target to apply a config in multiple steps and uses Terraform's knowledge of the dependency graph to do it automatically. |
@apparentlymart I don't fully understand why the dependency graph does actually count on the plan step. Intuitively I would guess that the dependency matrix would be applied at all stages (including instantiation on each step) . At least for all these cases would just avoid a bunch of problems. |
So, if I understand this correctly, there is no way for me to tell a provider to not configure until after certain resources have been configured. For example, this won't work because custom_provider will already be initialized before my_machine is built: The only option would be to run an apply with a -target option for my_machine first, the run the apply again after the dependency has been satisfied. |
but this will never work until this issue is resolved hashicorp/terraform#2430
+1 for My use case: To do so, I have to connect as the "root" user first, create the new role with appropriate permissions, and then connect again with the new user to create the database and schema in it. So I need two providers with aliases, one with root and one for the application db. My workaround currently is to comment out the second part first, apply the first part, then comment in the second part again to apply it as well. :( # ====================================================================
# Execute first
# ====================================================================
provider "postgresql" {
alias = "root"
host = "${var.db_pg_application_host}"
port = "${var.db_pg_application_port}"
username = "root"
password = "${lookup(var.rds_root_pws, "application")}"
database = "postgres"
}
resource "postgresql_role" "application" {
provider = "postgresql.root"
name = "application"
login = true
create_database = true
password = "${lookup(var.rds_user_pws, "application")}"
}
# ====================================================================
# Execute second
# ====================================================================
provider "postgresql" {
alias = "application"
host = "${var.db_pg_application_host}"
port = "${var.db_pg_application_port}"
username = "application"
password = "${lookup(var.rds_user_pws, "application")}"
database = ""
}
resource "postgresql_database" "application" {
provider = "postgresql.application"
name = "application"
owner = "${postgresql_role.application.name}"
}
resource "postgresql_schema" "myschema" {
provider = "postgresql.application"
name = "myschema"
owner = "${postgresql_role.application.name}"
policy {
create = true
usage = true
role = "${postgresql_role.application.name}"
}
policy {
create = true
usage = true
role = "root"
}
} |
I've managed to his this same issue with the postgres provider depending on an aws_db_instance outside my module. Is there a workaround available now? |
Any progress here? |
Hi all. I had a similar issue where I am using Terraform to: The A double resource "local_file" "kubeconfig" {
# HACK: depends_on for the helm provider
# Passing provider configuration value via a local_file
depends_on = ["module.typhoon"]
content = "${module.typhoon.kubeconfig}"
filename = "./terraform.tfstate.helmprovider.kubeconfig"
}
provider "helm" {
kubernetes {
# HACK: depends_on via an another resource
# config_path = "${module.typhoon.kubeconfig}", but via the dependency
config_path = "${local_file.kubeconfig.filename}"
}
}
resource "helm_release" "openvmtools" {
count = "${var.enable_addons ? 1 : 0}"
# HACK: when destroy, don't delete the resource dependency before the resource
depends_on = ["module.typhoon"]
name = "openvmtools"
namespace = "kube-system"
chart = "${path.module}/addons/charts/open-vm-tools"
} NB: This hack works because the provider expect a file path as config value. Hope it can help. |
Hi,
This is due to the rancher provider failing during the terraform plan step, as it can't reach the API. |
Probably a lot of people come here looking for how to provision Kubernetes objects with the same terraform that is creating a kube.
|
@rchernobelskiy Isn't that provider executed at build time? Meaning that the module of which it's pointing to already needs to exist prior to? |
In practice that seems to not be the case and arguments from other modules seem to be ok to put in providers prior to those modules existing. |
As long as no resources are added yet that depend on the provider being added. In which case why are you adding the provider? |
I'm using one |
It may work if you don't have any resources which depend on k8s resources via for_each or count, don't use any k8s data sources which need to be read at plan time, and don't use the kubernetes_manifest resource. It will fail with anything that requires the provider to connect to the cluster at plan time (since the cluster doesn't exist yet). Non-trivial configurations tend to have things that require cluster access at plan time, as discussed above in this issue. |
Hi, I had the same issue when configuring a Kubernetes provider that uses outputs of other modules. This is what I have on my provider configuration: provider "kubernetes" { My current main.tf files uses few modules and in some of those modules I used this resource definition: data "kubectl_file_documents" "secrets_manifest" { resource "kubectl_manifest" "metrics_server" { Up to this definition my whole terraform implementation was working fine and without issues using the above described provider configuration. Then, one day I needed to set up some secrets and I decided to use the configuration below. It is using the same "resource "kubectl_manifest"" but the new configuration describe/create the k8s objets in a different way (it is not using a yam file to describe/create the k8s object), it is describing the k8s objects inside the "resource "kubectl_manifest"" as you can see below. This is when I start getting this error: cannot create REST client: no client config. Secrets to use during Jenkins Installation:resource "kubernetes_manifest" "jenkins_spc" { What was my solution? i went back to create my secrets configuration in k8s using resource "kubectl_manifest" but like this: data "kubectl_file_documents" "secrets_manifest" { resource "kubectl_manifest" "metrics_server" { Hope that helps someone with the same issue. |
Could this actually indicate that the parameters are defined in a wrong place? Maybe the parameters that are currently passed to provider should actually belong for resolvers instead? It would be more repetitive, but would solve the problem without fundamental changes to Terraform. |
We need a feature that allows us to create our Terraform infrastructure from end to end using run-time dependant providers so that I can for example: Create a Kubernetes Cluster then within the same runtime --> Deploy Helm charts into my cluster (Prometheus, Grafana, etc). This would be game-changing in terms of IaC capabilities. I sometimes question the use-case of IaC beyond creating infrastructure using other module's properties. Since I have to have multiple folders and have to manually run through each module folder and run Please HashiCorp! |
@brandongallagher1999 It gets even worse when you want to deploy an entire application stack. We have these completely independent Transform module folders:
Currently the only better way to do this would be to use CDKTF which apparently can manage multiple independent "Stacks" that share properties using Terraform state. So yes, a "native" solution inside Terraform would really be appreciated! |
I share these sentiments exactly. As a user of Terraform, I want to run
This mirrors my point exactly. We need support for some level of dependency configuration between providers, especially for read operations as well in the case of Helm. I want to configure Terraform to wait until X resource (eg. Helm packages) have successfully been installed in Kubernetes before attempting to read the CRD Open API spec. |
This comment was marked as off-topic.
This comment was marked as off-topic.
You are right, I didn't know about terragrunt. So we have two community projects that work around this issue: CDKTF and terragrunt. This is great, but it adds to the complexity - even more tools to learn and understand. IMHO it would still be better to have this solved once and for all in Terraform itself. |
Terragrunt unfortunately does not solve this use-case either. Not to mention the unnecessary complexity of managing all of the sub |
Same issue as everyone else: Going to just see if i can configure everything i need in the helm provider. |
We need run-time dependant providers. This feature would be groundbreaking in terms of IaC. |
Not groundbreaking. Other tools like Pulumi have had it for years. Terraform just needs to pick up the pace. |
Wasn't aware Pulumi had this capability. It's nice to spin up our entire infrastructure and all of it's relative resources within one command. If Pulumi is actually capable of this, I actually might consider moving over. However in terms of the DevOps industry, Terraform still seems to be the most in demand; which concerns me. |
@Sodki Follow up from your comment. I have tried out Pulumi to recreate our entire stack. I will NEVER go back to Terraform. The ability to spin up an entire stack (Database, K8s Cluster, K8s resources, Storage Accounts, etc) in a single file (or organized set of folders), where I'd previously have an issue with deploying K8s resources since it required the cluster to be created prior to runtime is no longer an issue. I'd recommend that everyone here move over to Pulumi since not only does it solve the issue present on this thread, but it also allows you to utilize TypeScript or Python which is extremely convenient, compared to the extreme complexity of HCL when it comes to things beyond static declaration (for loops for example are INSANE in HCL, especially when accessing object properties). |
The Terraform team is planning to resolve this problem as part of #30937, by introducing a new capability for a provider to report that it doesn't yet have enough information to complete planning (e.g. something crucial like the API URL isn't known yet), which Terraform Core would then handle by deferring the planning of affected resources until a subsequent plan/apply round. You could think of this as something similar to automatically adding The history of this issue seems to be causing ongoing confusion, since the underlying problem here has nothing to do with dependencies and is instead about unknown values appearing in the provider configuration. Since there's already work underway to solve this as part of a broader effort to deal with unknown values in inconvenient places, I'm going to close this issue just to consolidate with the other one that has a clearer description of what the problem is and is tracking the ongoing work to deal with it. Thanks for the discussion here! If you are subscribed to this issue and would like to continue getting notifications about this topic then I'd suggest subscribing to #30937 instead. |
I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. |
This issue was inspired by this question on Google Groups.
There are providers (docker and consul - theoretically also openstack but that's a stretch) that can be implemented with Terraform itself using other providers like AWS; if there are other resources in a Terraform deployment that use the (docker or consul) provider they cannot be provisioned or managed in any way until and unless the other resources that implement the docker server or consul cluster have been successfully provisioned.
If there were a
depends_on
clause for providers like docker and consul, this kind of dependency could be managed automatically. In the absence of this, it may be possible to adddepends_on
clauses for all the resources using the docker or consul provider, but that does not fully address the problem as Terraform will attempt (and fail, if they are not already provisioned) to discover the state of the docker/consul resources during the planning stage, long before it has completed the computation of dependencies. Multiple plan/apply runs may be able to resolve that specific problem, but having adepends_on
clause for providers would allow everything to be managed in a single pass.The text was updated successfully, but these errors were encountered: