📌 How can you use Terraform modules?
Tags: terraform, modules, import
Published: 2023-12-20 08:00:00

## Description

Modules are self-contained packages of Terraform configurations that are managed as a group. Modules allow you to organize your Terraform code and reuse it across multiple projects, making it easier to manage and maintain your infrastructure as code.

There are two types of Terraform modules:

- Root modules: These are the main Terraform configurations that define your infrastructure. The root module is the top-level module in your Terraform configuration and it calls other Terraform modules as needed.

- Child modules: These are Terraform modules that are called by the root module. Child modules encapsulate a portion of your infrastructure, making it easier to manage and maintain.

When you create a Terraform module, you can use inputs and outputs to define the expected input and output values of the module:

- Inputs allow you to pass parameters into the module, making it more flexible and reusable.

- Outputs allow you to expose values from the module so that they can be used by other Terraform modules.

## Modules structure
The basic structure of a Terraform module includes:

- Variables: these are values that are passed into the module when it is called.

- Resources: The main part of a module is the collection of Terraform resources that are defined within it. These resources are used to create the infrastructure.

- Outputs: these are values that are returned by the module when it is called. These outputs can be used by other modules or in the root module of the Terraform configuration.

## Best practices
When using modules these best practices make your life easier:

- Verify the source: Before using a module from the Terraform Registry, verify the source to ensure that it's reputable and secure.

- Review the documentation: Review the documentation of the module to ensure that it meets your requirements and to understand how it can be used in your configuration.

- Use version control: Use version control to keep track of changes to the module, and make sure to use a specific version of the module in your configuration so that you are aware of any breaking changes.

- Test the module: Test the module thoroughly before using it in production to ensure that it works as expected and to catch any bugs or errors.

## How to use modules on Brainboard

## 1. Add modules as block definition

1. Add the information for the name, source and custom icon.

![Add the information for the name, source and custom icon.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fqfr3tyxuttlln2jwy6x.png)

2. Specify the source of the import for your module:

- From registry: The Terraform Registry is a centralized repository for Terraform modules. It allows Terraform users to easily find and use Terraform modules for various cloud providers and other infrastructure resources.

![Specify the source](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vvijp9qc2j6nyrgwk693.png)

When you import from registry you can add the information for the namespace/module_name/provider_name.

- From Git: Many Terraform modules are hosted on GitHub, either as standalone repositories or as part of a larger project.

If you have private modules, you can store them in a private repository, such as a private GitHub repository or an internal repository. When the repo that you want to import is private, then you need to specify also the credentials by opening the open git settings.

You can specify as service Github, Azure DevOps, Bitbucket or Gitlab.

![From Git](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0kx4k3kpgcm3fnj0s75e.png)

- From files: You can also create your own modules using Terraform code and store them as local files and then import them in Brainboard.

![From Files](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yhl6rj20owq3hewq4kr6.png)

## 2. Add modules as an architecture

There are some cases when importing a module as an architecture is useful.

- When you want to have visibility on the code and on the resources used by the module.

- When you want to modify an existing module and use it later.

To import a module as architecture you can go to the top bar menu and click the + button to import an architecture.

![Add modules as an architecture](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vy8ow2sue64d7arbo46z.png)


Start using your Terraform modules here 👉 https://app.brainboard.co