# Terraform files

### Description

When you create your architecture, Brainboard automatically and instantly generates a Terraform code for your infrastructure.

This code is stored in different files according to the best practices of Terraform.

### Files structure

By default, if you don't change rename or change the generated files, you have the following ones:

1. **main.tf**: contains all the definition of resources and their configuration. See how you can change or rename it below.
2. **output.tf**: contains the output variables.
3. **providers.tf**: contains the definition of `terraform` block and the providers.
4. **terraform.tfvars**: contains **only** the values of the variables if defined.
5. **variables.tf**: contains the definition of variables and their blocks.
6. **locals.tf**: contains the definition of Terraform `locals`.
7. **backend.tf**: contains the configuration of the remote backend.

### Create new Terraform file

To create a new Terraform file that contains some resources:

1. Select the resource you want to put in the same file.
2. Click on `Group resource in the same Terraform file` button:&#x20;

   <figure><img src="https://2733077811-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7YhVIZuz5Brv8kisTlFL%2Fuploads%2Fgit-blob-56c6f1bfb83c702ccfedf08c1cba6063ee9e1524%2Fgroup-resources.png?alt=media" alt=""><figcaption></figcaption></figure>
3. Give a new name of the file.
4. Click on `Save` to create the new file and put resources inside it.

Once the file is created, it will be visible in the list of Terraform files:&#x20;

<figure><img src="https://2733077811-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7YhVIZuz5Brv8kisTlFL%2Fuploads%2Fgit-blob-aea97f7c24cf060ee2b23584c6a118e7621e3bda%2Ftf-files.png?alt=media" alt=""><figcaption></figcaption></figure>

### Rename existing Terraform file

To rename an existing file:

1. Click on the button to display the list of groups:
2. Click on the name of the group:&#x20;

   <figure><img src="https://2733077811-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7YhVIZuz5Brv8kisTlFL%2Fuploads%2Fgit-blob-47848bef30e135a2e39cb7b09b75083b802e1177%2Fgroups-list.png?alt=media" alt=""><figcaption></figcaption></figure>
3. Rename it and save.

### Delete a Terraform file

To delete an existing file:

1. Click on the button to display the list of groups:
2. Click on the `Delete group` button:&#x20;

   <figure><img src="https://2733077811-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7YhVIZuz5Brv8kisTlFL%2Fuploads%2Fgit-blob-7a28b3452a8ffede0aa7ca8c9c134328287b23d0%2Fdelete-group.png?alt=media" alt=""><figcaption></figcaption></figure>
3. Confirm to permanently delete it.

{% hint style="info" %}
When you delete a group, the resources inside it **will not be deleted**. They will be put back in `main.tf`. To delete them, you need to select and delete them
{% endhint %}

### Add resource into an existing file

To add a resource into an existing or new Terraform file (group):

1. Select the resource.
2. Click on the `Group resource in the same Terraform file` button:&#x20;

   <figure><img src="https://2733077811-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7YhVIZuz5Brv8kisTlFL%2Fuploads%2Fgit-blob-901c6ddabb1ecb6315d008007311e33ed9df8aae%2Fadd-resource-to-group.png?alt=media" alt=""><figcaption></figcaption></figure>
3. Either select group from the available list or create a new one.
4. Click on `Save` to persist the change.

### Remove resource from a file

To remove a resource from an existing Terraform file (group):

1. Select the resource.
2. Click on the `Group resource in the same Terraform file` button:&#x20;

   <figure><img src="https://2733077811-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F7YhVIZuz5Brv8kisTlFL%2Fuploads%2Fgit-blob-901c6ddabb1ecb6315d008007311e33ed9df8aae%2Fadd-resource-to-group.png?alt=media" alt=""><figcaption></figcaption></figure>
3. Select `main`.
4. Click on `Save` to persist the change.

### Best practices

* Always group resources that are supposed to work together or have the same logic in a separate group / Terraform file.
* Use explicit names for your Terraform files. Usually, there are 2 conventions of naming:
  * Infrastructure base naming: `vpc.tf`, `db.tf`...
  * Application base naming: `microservice1.tf`, `backend.tf`...
