---
title: "The Inside Story: Terraform Module Handling"
description: We've introduced Terraform Module Handling. Backend dev Francesc takes us through why and how it will soon become part of TerraCognita's future.
image: https://blog.cycloid.io/hubfs/Terraform-modules_Big-blog-header-v1.png
---

[![Cycloid](https://blog.cycloid.io/hubfs/Cycloid_August2020/Images/owl_logo.png) ![Cycloid](https://blog.cycloid.io/hubfs/Cycloid_August2020/Images/cycloid_logo_w150.png)](https://www.cycloid.io/)

Toggle navigation

- Why Cycloid? 
    - [The Overview](https://www.cycloid.io/platform-engineering)
    - [Why now?](https://www.cycloid.io/why-now-not-tomorrow)
    - [Our Customers](https://www.cycloid.io/customers)
    - [Our Partners](https://www.cycloid.io/partners)
    - [Customer Stories](https://www.cycloid.io/customer-stories)
- Solutions 
    - [Self-service Portal](https://www.cycloid.io/solutions/self-service-portal)
    - [DevOps Rollout](https://www.cycloid.io/solutions/devops-rollout)
    - [Hybrid Cloud](https://www.cycloid.io/solutions/hybrid-cloud)
    - [FinOps](https://www.cycloid.io/solutions/finops)
    - [Infra as Code](https://www.cycloid.io/solutions/infra-as-code)
    - [GitOps](https://www.cycloid.io/solutions/gitops)
    - [Executives](https://www.cycloid.io/solutions/c-level)
    - [Head of DevOps](https://www.cycloid.io/solutions/head-of-devops)
    - [End-users & Developers](https://www.cycloid.io/solutions/developers)
    - [Platform Teams](https://www.cycloid.io/solutions/platform-engineering)
    - [Cloud Sustainability Leader](https://www.cycloid.io/solutions/cloud-sustainability)
    - [Enterprises](https://www.cycloid.io/solutions/enterprise)
    - [Managed Services Providers](https://www.cycloid.io/solutions/managed-service-providers)
    - [Scale-ups](https://www.cycloid.io/solutions/scale-ups)
- Features 
    - [Stacks](https://www.cycloid.io/product/stacks)
    - [StackForms](https://www.cycloid.io/product/stackforms)
    - [Infra Import](https://www.cycloid.io/product/infraimport)
    - [CI/CD Pipelines](https://www.cycloid.io/product/managed-ci-cd-pipelines)
    - [Asset Inventory](https://www.cycloid.io/product/asset-inventory)
    - [InfraView](https://www.cycloid.io/product/infraview)
    - [Quota Management](https://www.cycloid.io/product/quota-management)
    - [Cloud Cost Estimation](https://www.cycloid.io/product/cost-estimation)
    - [Cloud Cost Management](https://www.cycloid.io/product/cloud-cost-management)
    - [Cloud Carbon Footprint](https://www.cycloid.io/product/cloud-carbon-footprint)
- [Pricing](https://www.cycloid.io/pricing)
- Resources 
    - [Blog](https://blog.cycloid.io/)
    - [Docs](https://docs.cycloid.io/)
    - [Library](https://www.cycloid.io/library)
    - [On-demand webinars](https://www.cycloid.io/on-demand-webinars)
    - [Videos](https://www.cycloid.io/demo-video)

[Log in](https://console.cycloid.io/signin) [Sign up](https://www.cycloid.io/pricing)

# The Inside Story: Terraform Module Handling

[Francesc Gil](https://blog.cycloid.io/author/francesc-gil)

 Mar 25 - 3 min read

Francesc, a backend developer here at Cycloid and talented maintainer of [TerraCognita](https://blog.cycloid.io/what-is-terracognita?hsLang=en), tells us what led to our recent launch of Terraform Module handling and what exciting developments it's paving the way for.

## What did we add?

In the newest version of<https://github.com/cycloidio/terracognita>[Terracognita](https://github.com/cycloidio/terracognita) (0.6.1) we added the ability to import a running infrastructure into a<https://www.terraform.io/docs/language/modules/syntax.html>[Terraform Module.](https://www.terraform.io/docs/language/modules/syntax.html) This is a more manageable and standard way to structure your HCL.

Terraform Modules allow you to create a **reusable** and customizable definition of the infrastructure which will help you better define your standard infrastructure components. Also, the use of Terraform Modules is considered good practice and we always try to follow best practices.

## How does it work?

Before telling you how it works, let's go over how it *used* to work so we can see the difference.

**Before,** we generated one HCL file with all the information imported from your infrastructure. To be honest, big configuration files are not manageable at all, and they're certainly not reusable, as what you see is what you get. To change it, you would need to dive into the generated file (potentially +500 lines of code) and change things manually or try to generate a module from it yourself. Honestly, that's not *anyone's* idea of fun and it can lead to errors in your IaC creation process. This is pretty silly when you bear in mind that the reason you'd use TerraCognita in the first place is to *avoid* mistakes in your IaC creation!

As we can see, this is not the best use case. Sure, it's usable and it gets you started in Terraform and HCL quickly - you would have the infrastructure in HCL and wouldn't need to write it from scratch. That said, it would require a lot of manual work to have something more refined and actually reusable.

**Now,** with the new version, we can do that work for you! The<https://github.com/cycloidio/terracognita#modules>[documentation](https://github.com/cycloidio/terracognita#modules) explains how to do it but we'll go through it, the benefits it provides, and how is it done.

First of all, use:

```
 --module module/path/name flag 
```

...to enable the feature and we'll import everything as a module! Once it finishes, if you check your module/path/name/, you'll have a Terraform Module inside! The structure is the following:

- **module.tf:** is where the Terraform Module is declared and where it is configured
- **module-name/:** is where the module configuration resides (name being the directory you specified)
- **module-name/variables.tf:** is where all the variables that we define (so the module can be configured) are placed (more about that later).

```
name/├── module-name│   ├── autoscaling.tf│   ├── cloud_front.tf│   ├── cloud_watch.tf│   ├── ec2.tf│   ├── elastic_load_balancing_v2_alb_nlb.tf│   ├── iam.tf│   ├── rds.tf│   ├── route53_resolver.tf│   ├── route53.tf│   ├── s3.tf│   ├── ses.tf│   └── variables.tf└── module.tf
```

So, the first benefit is that we no longer have to import everything in one single HCL file. Instead, we do it in multiple separate ones based on the "category" of the resources. We can get this information from another OSS library we have named<https://github.com/cycloidio/tfdocs>[tfdocs](https://github.com/cycloidio/tfdocs), which, in a nutshell, parses the documentation from the Terraform Providers and provides a normalized output so we can check it in different places in the Cycloid codebase, like [InfraView](https://blog.cycloid.io/what-is-infraview?hsLang=en) and [StackCraft](https://blog.cycloid.io/governance-and-flexibility-with-stackcraft?hsLang=en).

Now, back to TerraCognita!

This generate module is a bit raw - it's the default and most basic configuration that we provide and what it'll do is replace all the attribute values from your HCL for<https://www.terraform.io/docs/language/values/variables.html>[variables](https://www.terraform.io/docs/language/values/variables.html) with the value that they had as a default of the variable and declare those variables into a variables.tf which will be inside of your module-name/variables.tf.

Then if you check the module.tf you'll see it has all the variables added to the Module block commented with their values, so you know which value they have by default. If you wanted to change them, you could uncomment and make the modification you need.  

```
module "name" {  # aws_instance_front_instance_type = "t2.small"  [...]  source = "module-name"}
```

The second benefit we see is that modules make modifying your configuration much easier, but wait, there's more...

We can make that module even better by adding --module-varibles path/to/file flag which will allow you to define a JSON/YAML file with all the resource + attributes that would be important for you to use in the module so only those specific ones (instead of ALL of them) would be defined as variables and added to the Module block

```
{  "aws_instance": [    "instance_type",    "cpu_threads_per_core",    "cpu_core_count"  ]}
```

This file format is basically a Key Values in which the Key is the name of the Terraform Resource and the Values are the list of all the attributes of that resource that you would like to import.

## Conclusion

Using TerraCognita's Modules feature will help you better import your infrastructure into Terraform Modules. You'll be able to import in a more customizable way that will improve the reusability and configurability of your infrastructure. This provides an easy way to adopt Terraform best practice.

#### Stay up to date with everything coming from the Cycloid stable - sign up for the quarterly  newsletter.

[![Subscribe to email updates](https://no-cache.hubspot.com/cta/default/6369062/da7d1a6d-80dd-41a7-8811-0ffaef8d457e.png)](https://cta-redirect.hubspot.com/cta/redirect/6369062/da7d1a6d-80dd-41a7-8811-0ffaef8d457e)

 

[Insider](https://blog.cycloid.io/tag/insider), [Product](https://blog.cycloid.io/tag/product)

---

## Read More

<https://blog.cycloid.io/what-is-terracognita?hsLang=en>

## [What is TerraCognita?](https://blog.cycloid.io/what-is-terracognita?hsLang=en)

 TerraCognita is a key part of Cycloid. We call it our "reverse Terraform" and we're going to show...

 May 15 - 3 min read

<https://blog.cycloid.io/cycloid-open-source?hsLang=en>

## [Cycloid and the open source community](https://blog.cycloid.io/cycloid-open-source?hsLang=en)

 Today, as part of our birthday series, we're taking a look at Cycloid's relationship with open...

 Jul 24 - 4 min read

<https://blog.cycloid.io/infrastructure-as-code-for-beginners?hsLang=en>

## [Infrastructure as code: Cycloid's non-boring guide for the clueless](https://blog.cycloid.io/infrastructure-as-code-for-beginners?hsLang=en)

 If you’ve recently moved into the world of DevOps and hybrid cloud, or are thinking of it, you’ll...

 May 28 - 4 min read

![Cycloid](https://blog.cycloid.io/hubfs/raw_assets/public/Cycloid_August2020_Theme/images/cycloid_logo_white.svg "Cycloid")

- Discover 
    - [Why Cycloid?](https://www.cycloid.io/platform-engineering)
    - [Documentation](https://docs.cycloid.io/)
    - [Pricing](https://www.cycloid.io/pricing)
    - [Professional Services](https://www.cycloid.io/professional-services)
    - [Open-source](https://www.cycloid.io/open-source-software)
- Explore 
    - [Careers](https://www.cycloid.io/careers)
    - [Open Roles](https://cycloid.jobs.personio.de/)
    - [Partners](https://www.cycloid.io/partners)
    - [Press](https://www.cycloid.io/press)
- Learn 
    - [Blog](https://blog.cycloid.io/)
    - [Events](https://www.cycloid.io/events)
    - [Library](https://www.cycloid.io/library)
    - [Videos](https://www.cycloid.io/videos)
- Begin 
    - [Contact us](https://www.cycloid.io/contact-us)
    - [Log in](https://console.cycloid.io/login)
    - [Request a demo](https://www.cycloid.io/request-demo)

<https://github.com/cycloidio> <https://www.linkedin.com/company/cycloid/> <https://twitter.com/cycloid_io> <https://www.youtube.com/channel/UCJnDg9fFIhvtzelUaA0_zQQ/>

[Try Cycloid for free](https://www.cycloid.io/contact-us)

©2015-2022 Copyright Cycloid.io | [Legal Notice](https://www.cycloid.io/cookies-and-privacy-policy)

![](https://px.ads.linkedin.com/collect/?pid=2359017&fmt=gif)

```json
{
  "@context" : "https://schema.org",
  "@type" : "BlogPosting",
  "author" : {
    "@type" : "Person",
    "name" : "Francesc Gil",
    "url" : "https://blog.cycloid.io/author/francesc-gil"
  },
  "dateModified" : "2021-03-25T10:15:02.270Z",
  "datePublished" : "2021-03-25T10:15:02.000Z",
  "headline" : "The Inside Story: Terraform Module Handling",
  "image" : [ "https://blog.cycloid.io/hubfs/Terraform-modules_Big-blog-header-v1.png" ],
  "mainEntityOfPage" : {
    "@id" : "https://blog.cycloid.io/the-inside-story-terraform-module-handling",
    "@type" : "WebPage"
  },
  "publisher" : {
    "@type" : "Organization",
    "logo" : {
      "@type" : "ImageObject",
      "url" : "https://blog.cycloid.io/hubfs/Logo-RVB-avec-hibou-sans-base-line.png"
    },
    "name" : "Cycloid"
  }
}
```