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

Import resources from module into state file #7020

Closed
ldelossa01 opened this issue Jun 5, 2016 · 5 comments
Closed

Import resources from module into state file #7020

ldelossa01 opened this issue Jun 5, 2016 · 5 comments

Comments

@ldelossa01
Copy link

ldelossa01 commented Jun 5, 2016

Unless I'm missing something it seems that when creating a 'module' you must use output variables to export information from the module into the state using the module. This becomes cumbersome when you're not exactly sure which attributes you may need in the state you are writing.

Is it possible to export the entire resource to using state? For example:

I create a module called sandbox. The main.tf looks like this:

resource "aws_vpc" "sandbox-vpc" {
    ...
}

In my tf state file I could do the following:

module "sandbox" {
    source = "./sandbox"
}

resource "aws_internet_gateway" "ig01" {
    vpc_id = "${module.sandbox.aws_vpc.sandbox-vpc.id}"
}

Now I understand that I could make a output variable in the module called "aws_vpc.sandbox-vpc.id" However this only gives me ID, if I use a resource with many exported attributes, my code becomes very bloated with output variable declarations.

I searched the docs and I don't see any other usage. Also played around a little and got a bunch of errors trying to do what I'm stating.

@PetaPetaPeta
Copy link

PetaPetaPeta commented Jun 6, 2016

I am not aware of any functionality to export entire resources.
I do understand your worry of your code becoming bloated. To reduce this issue I split each module into at least three files for to maintain structure:

  1. main.tf: The purpose of this file is to instantiate all resources. Can be split across several files if necessary.
  2. variables.tf: Contains the variables used in the main.tf file
  3. output.tf: Contains all the output variables of the module

By only defining output variables in your module you define a nice "interface" to your module that makes it easy to work with. Think of it as a class in a programming language. You do not want to expose every part of the class, but only a subset.

@apparentlymart
Copy link
Contributor

apparentlymart commented Jun 6, 2016

This is not an immediate-term solution, but once #6819 is added this could be written something like this:

(THIS WILL NOT WORK YET, SINCE THAT PR ISN'T MERGED)

module "sandbox" {
    source = "./sandbox"
}

data "aws_vpc" "sandbox" {
    id = "${module.sandbox.aws_vpc.sandbox-vpc.id}"
}

resource "aws_internet_gateway" "ig01" {
    # This is of course pointless since you already knew the id,
    # but you could also use any other attribute of the `aws_vpc`
    # data source.
    vpc_id = "${data.aws_vpc.sandbox.id}"
}

This is not 100% ideal since it will cause Terraform to re-read a redundant copy of the VPC data from the EC2 API, rather than just exposing what was already created inside the module. However, it would allow you achieve what you want to achieve here.

@ldelossa01
Copy link
Author

@PetaPetaPeta Do you have an example of your structure on github?

@jen20
Copy link
Contributor

jen20 commented Jun 17, 2016

Hi @ldelossa01! This isn't supported right now - @apparentlymart's workaround is probably closest to being implementable. However, some changes made to the remote state data source in 0.7rc2 mean that this is not beyond the realms of possibility - previously it couldn't be done because of subtleties around the interaction of the helper/schema library. I'm going to go ahead and close this issue for now, but will circle back around to it once we have the RTM of 0.7 out the door, since I don't think it's a bad idea.

@ghost
Copy link

ghost commented Apr 25, 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 25, 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