简体   繁体   中英

Terraform depends_on output return value

I have some helm_release resources I want to deploy if EKS cluster is up and running (ACTIVE). In EKS module I have exported: cluster_status which can be in one of the following states CREATING , ACTIVE , DELETING , FAILED

How can I use depends_on to be based on actual value?

depends_on = [module.eks-cluster.cluster_status.active]

returns:

References in depends_on must be to a whole object (resource, etc), not to an attribute of an object.

Output configuration:

output "cluster_status" {
  value = module.eks-cluster.cluster_status
}

and it returns:

cluster_status = "ACTIVE"

, DISCLAIMER THIS ANSWER HAS FEW ASSUMPTIONS, MORE INFO IS NEEDED IF NOT HELPED

As the error states References in depends_on must be to a whole object (resource, etc), not to an attribute of an object.

output "cluster_status" {
  value = module.eks-cluster.cluster_status
}

This seems to be on the child/interface module level which is not required to make a dependency between EKS and Helm release.

I have assumed your code as following

module "eks-cluster" {
source = "path_to_modue"
[...]
}

resource helm_release some_release {
[..]
}

depends_on meta argument works on entire resources not on the specific attributes either exported(outputs) or provided(inputs).

!! Assuming that you are using a module for the eks and resource for helm_release (actually it does not matter even if helm release is a child module)

depends_on in your helm release should be

resource "helm_release" "release" {
[....]
depends_on = [module.eks-cluster] # as this is the complete resource/module on which the helm release is dependent.
}

This will make sure that the helm release is only deployed only when EKS cluster deployment is successful.

If you want to be very specific and only want to deploy helm_release when the status of EKS cluster deployed is ACTIVE you might need to come up with locals {} and count meta argument to control the deployment.

This Method is not recommended but choice if yours

  • Status can sometimes take some time to be active even after successful deployments
locals {
## assuming that "cluster_status" is the https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/eks_cluster#status output in the root module.
  eks_status = module.eks-cluster.cluster_status
}

resource "helm_release" "some_release" {
  count = local.eks_status == "ACTIVE" ? 1 : 0

  [...] 
}

Please note that you have to configure your helm provider with the outputs of eks module for authorizing and authentication purposes to the respective EKS cluster.

Extra Info:

Module support for depends_on was added in Terraform version 0.13, and prior versions can only use it with resources.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM