Terraform Modules in Environments

Terraform Module attachments for enrvironments

Terraform Modules in Bunnyshell deal with re-utilising and abstracting the behaviour of Terraform Modules.

In order to use Terraform Modules within an Environment, they must be attached to the respective Environment.
A Terraform Module can be attached more than once to the same Environment, and used with different parameters.

Attach a Terraform Module

To attach a Terraform Module to an environment, you only need to follow the steps below:

  1. Go to the Environments page and click on the desired environment.

  2. Click the + button and select Terraform Module.

  3. Select the Terraform Module you wish to attach to the environment.

1770

📘

Note

If you haven't yet configured a module, you can do it now by clicking +Add new terraform module. Detailed instructions on how to do this are available here.

At this step, two additional features can be activated:

  • Auto-sync: turn on this option to automatically update the Terraform module every time a change is implemented, be it user configuration or Git commits.
  • Auto-attach to Ephemeral Environments: turn on this option to automatically attach the Terraform module to future ephemeral environments.

Click the Continue button.

  1. Next, Bunnyshell will identify the Terraform variables that have been defined as configurable at the module level. These variables must be configured before the module is attached to the environment.
1772

Click Continue.

📘

Note

Bunnyshell will then test the Terraform plan, previewing the changes that are to be implemented by the module.

  1. In this next step, you can map Terraform output values to your environment components. This will help you connect your applications to your infrastructure.
1768

Mapping Output Values

Output values can be mapped to variables from your components (apps, services etc) deployed in Kubernetes.

Mapping output values is not a mandatory step, but it can help you integrate Terraform with your services. It is not only useful for random output values, but also for those with static values, since you will no longer need to manage them both in config and in Terraform. You simply create a mapping and when a Terraform module is applied the new value is automatically injected in the app.

When selecting the Terraform output value, the drop-down menu will only display the output values you chose to include when you added the Terraform module.

In the Resource Type drop-down menu you can select the way in which Bunnyshell injects an output value. Choose one of the two options:

  • Environment. In this case, the value will be injected in all the services inside that environment. This feature comes in handy, for example, if your environment includes a number of services that use the same database and share one or more variables.
  • Environment Components. In this case, the value will be injected in just one service inside the environment. So, for example, if you want to name a variable password, but another service has a variable with the same name, this feature can help you avoid a situation where there are two variables named password inside the same environment.

📘

Note

The same output value can be mapped several times, with different names, in different variables from different services.

Mapped output values will override any variable with the same name on any level (project, environment or component).

Bunnyshell now checks if all the variables provided are in order and makes sure there no errors that could affect the module. This is the final and most important check performed. If everything is in order, the attachment will be functional.

Click Continue.

  1. Bunnyshell now creates an execution plan, which lets you preview the changes the Terraform module plans to make to your infrastructure. Should you wish to make further changes, click Back to return to the previous screen.
    When everything is properly configured, click Attach Terraform Module.

📘

Bunnyshell will automatically apply the attached Terraform module the next time the environment is deployed.

Alternatively, you can click on the module's Options button and select Apply. The module will be applied the next time the environment is deployed.

1716

 

Detach a Terraform Module

  1. Select the environment where the module is attached.

  2. On the left side of the page, select the Terraform Module Component you want to detach.

  3. On the right side of the page, click Actions and then Detach.

3582
  1. If the module was applied at least once, you can choose to keep or delete the resources created by Terraform.

 

Automatic Updates

Terraform Modules are automatically updated with every push command performed in your Git repository. While the vast majority of these updates will generally be implemented successfully, some of them might invalidate the module.

🚧

Note

Modules are updated after every successful push command, even if this leads to the module being invalidated.

Attachments will not be affected by changes that invalidate the module. Read more about how attachments are updated in our dedicated article.

To revalidate a module:

  1. Click on the project name in the upper-right corner of the Bunnyshell interface.

  2. Select View all projects.

  3. Click the Settings button of the desired project.

  4. From the left-side menu, select Terraform modules.

  5. Click the Options button corresponding to your module and select Edit.

You now need to follow the same steps you followed when you added the module. The difference is most of the fields are already completed. The only changes are the ones that invalidated your module (e.g. a modified variable).