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

depends_on module not working as expected #10883

Closed
hafizullah opened this issue Dec 21, 2016 · 4 comments
Closed

depends_on module not working as expected #10883

hafizullah opened this issue Dec 21, 2016 · 4 comments

Comments

@hafizullah
Copy link

Hi
I was looking forward to using the depends_on module feature [10076].

However it is not working as expected I think.

So my setup:
I have two modules, network and environment

In the environment module I have some resources defined(ec2 instances) that needs to depend on the network module being fully created.

For each resource that depends on network module have added:

depends_on = [
   "module.network"]

Expected:
Network module is created and then my resources that depends on it are created

Actual:

error loading Terraform: module env.root: 1 error(s) occurred:
* aws_instance.instance2: resource depends on non-existent module 'network'

I feel this is an issue but would like to be proven wrong :)

@mitchellh
Copy link
Contributor

Modules are only visible in the module they're used. Within your environment module, the "network" module doesn't exist (it exists in the parent). I tried with a couple modules and resources in the same module and did you exact line above and it worked great.

@cemo
Copy link

cemo commented Jan 10, 2017

@mitchellh What is the proper way of handling such cases? I want true isolated modules and also using some modules from 3rd party projects as a dependency which makes impossible to create nested structures.

@FilBot3
Copy link

FilBot3 commented Sep 17, 2018

Alright to clarify my deleted post, what I did was use a Rakefile, and split my parts of my infrastructure up into multiple plans and apply's. I have one Rake task plan and apply my network specific stuff, then another for my security groups, and a 3rd for my compute infrastructure. That kind of logically splits it up, even though they're in the same main.tf calling those modules.

This is my workaround until Terraform has a module depends_on function.

This may seem over the top, but it works.

namespace :terraform do
  namespace :openstack do
    desc 'Deploy the Full Stack, erythang'
    task deploy: ['terraform:openstack:deploy_infra', 'terraform:openstack:deploy_secgrps', 'terraform:openstack:deploy_compute']

    desc 'Use Terraform to deploy the infrastructure'
    task deploy_infra: ['terraform:openstack:plan_infra', 'terraform:openstack:apply_infra']

    task :plan_infra do
      sh %(
        terraform plan \
          -target=module.splunk_network_infra \
          -out=terraform_state/splunk_deployment_infra.tfplan \
          -state=terraform_state/terraform_infra.tfstate
      )
    end

    task :apply_infra do
      sh %(
        terraform apply \
          -target=module.splunk_network_infra \
          -state-out=terraform_state/terraform_infra.tfstate \
          terraform_state/splunk_deployment_infra.tfplan
      )
    end

    desc 'Use Terraform to deploy the security groups'
    task deploy_secgrps: ['terraform:openstack:plan_secgrps', 'terraform:openstack:apply_secgrps']

    task :plan_secgrps do
      sh %(
        terraform plan \
          -target=module.splunk_security_group \
          -out=terraform_state/splunk_deployment_secgrps.tfplan \
          -state=terraform_state/terraform_secgrps.tfstate
      )
    end

    task :apply_secgrps do
      sh %(
        terraform apply \
          -target=module.splunk_security_group \
          -state-out=terraform_state/terraform_secgrps.tfstate \
          terraform_state/splunk_deployment_secgrps.tfplan
      )
    end

    desc 'Use Terraform to deploy the compute instances'
    task deploy_compute: ['terraform:openstack:plan_compute', 'terraform:openstack:apply_compute']

    task :plan_compute do
      sh %(
        terraform plan \
          -target=module.splunk_instance_01 \
          -out=terraform_state/splunk_deployment_compute.tfplan \
          -state=terraform_state/terraform_compute.tfstate
      )
    end

    task :apply_compute do
      sh %(
        terraform apply \
          -target=module.splunk_instance_01 \
          -state-out=terraform_state/terraform_compute.tfstate \
          terraform_state/splunk_deployment_compute.tfplan
      )
    end

    desc 'Destroy the Full Stack, erythang'
    task destroy: ['terraform:openstack:destroy_infra', 'terraform:openstack:destroy_secgrps', 'terraform:openstack:destroy_compute']

    desc 'Use Terraform to destroy the infrastructure'
    task destroy_infra: ['terraform:openstack:plan_destroy_infra', 'terraform:openstack:apply_destroy_infra']

    task :plan_destroy_infra do
      sh %(
        terraform plan \
          -destroy \
          -target=module.splunk_network_infra \
          -out=terraform_state/splunk_destroy_infra.tfplan \
          -state=terraform_state/terraform_infra.tfstate
      )
    end

    task :apply_destroy_infra do
      sh %(
        terraform apply \
          -target=module.splunk_network_infra \
          -state-out=terraform_state/terraform_infra.tfstate \
          terraform_state/splunk_destroy_infra.tfplan
      )
    end

    desc 'Use Terraform to destroy the security groups'
    task destroy_secgrps: ['terraform:openstack:plan_destroy_secgrps', 'terraform:openstack:apply_destroy_secgrps']

    task :plan_destroy_secgrps do
      sh %(
        terraform plan \
          -destroy \
          -target=module.splunk_security_group \
          -out=terraform_state/splunk_destroy_secgrps.tfplan \
          -state=terraform_state/terraform_secgrps.tfstate
      )
    end

    task :apply_destroy_secgrps do
      sh %(
        terraform apply \
          -target=module.splunk_security_group \
          -state-out=terraform_state/terraform_secgrps.tfstate \
          terraform_state/splunk_destroy_secgrps.tfplan
      )
    end

    desc 'Use Terraform to destroy the compute instances'
    task destroy_compute: ['terraform:openstack:plan_destroy_compute', 'terraform:openstack:apply_destroy_compute']

    task :plan_destroy_compute do
      sh %(
        terraform plan \
          -destroy \
          -target=module.splunk_instance_01 \
          -out=terraform_state/splunk_destroy_compute.tfplan \
          -state=terraform_state/terraform_compute.tfstate
      )
    end

    task :apply_destroy_compute do
      sh %(
        terraform apply \
          -target=module.splunk_instance_01 \
          -state-out=terraform_state/terraform_compute.tfstate \
          terraform_state/splunk_destroy_compute.tfplan
      )
    end
  end
end

@ghost
Copy link

ghost commented Apr 2, 2020

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.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@ghost ghost locked and limited conversation to collaborators Apr 2, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants