cycloidio/terracognita

Import as a Module

xescugc opened this issue · 0 comments

Abstract

Would be good to have an import directly as a Terraform Module so we are able to directly use the imported infrastructure as a module.

Proposal

As the import is directly filled there is no way in which we can define which attributes of a Resource need to be defined as variables so they can be modified when it's used as a Module.

For this we could have an specific configuration for each Provider in which we would define from each resource which attributes are the most "common" to have variables so it can be changed from the Module.

Implementation

The implementation I have in mind is to add a new flag of --module-name that will generate the HCL as a Module which means to have the HCL inside a ./module-{module-name}/hcl.tf and have also a ./{module-name}.tf.

The {module-name}.tf will source the ./module-{module-name}/ and have all the variables that are defined on the hcl.tf with the filled data that it had when importing, so it can be generated as it was imported.

The config that was mentioned that we'll have for each Provider will be something like:

{
  "aws_instance": [
    "count",
  ],
}

It'll be able to be overrided by a --module-config with the same format, in which case that one would be used instead of the default one.

Which means that any aws_instance.count will be defined as a variable (different for each aws_instance, something like var.aws_instance_{instance-name}_count

And then the module would be like:

module "{module-name}" {
  source = "./module-{module-name}"

  # If it has a value
  aws_instance_{instance-name}_count = 4
  
  # If it has no value inside of the module
  # so the module usage can have them defined
  # aws_instance_{instance-name}_count
}

Open issues (if applicable)