Introduction
Git submodules allow a Terraform configuration repository to include and reference another Git repository at a specific commit. In Terraform Enterprise, this enables teams to reuse shared Terraform code across multiple workspaces.
This guide provides the steps to configure a Terraform Enterprise workspace to correctly clone and use a repository containing Git submodules.
Prerequisites
- An active Terraform Enterprise instance.
- A VCS Provider, such as GitHub, connected to your Terraform Enterprise instance.
Note: This approach was validated using the GitHub VCS provider and Terraform Enterprise v1.1.1.
Procedure
Step 1: Create the Main GitHub Repository
This repository will contain the primary Terraform configuration and will reference the submodule.
- Log in to GitHub.
- Create a new repository (e.g.,
terraform-main). - Select the repository visibility (Private is recommended).
- Click Create repository.
Step 2: Create the Secondary GitHub Repository
This repository will be included as a submodule and contains reusable Terraform code.
- In GitHub, create another new repository (e.g.,
terraform-secondary). - Select the repository visibility.
- Click Create repository.
Step 3: Initialize the Main Repository
Clone the main repository locally and add an initial file.
-
Create and navigate to a test directory on your local machine.
$ mkdir ~/test-directory $ cd ~/test-directory
-
Clone the main repository.
$ git clone https://github.com/<org>/<terraform-main>.git
-
Navigate into the repository directory.
$ cd terraform-main
-
Create a
README.mdfile.$ echo "# This is a test for Git Submodules - Main Repository" > README.md
-
Commit and push the changes.
$ git add . $ git commit -m "Initial commit" $ git push
Step 4: Initialize the Secondary Repository
Clone the secondary repository and add a sample Terraform configuration.
-
Navigate back to your test directory.
$ cd ~/test-directory
-
Clone the secondary repository.
$ git clone https://github.com/<org>/<terraform-secondary>.git
-
Navigate into the repository directory.
$ cd terraform-secondary
-
Create a
README.mdfile.$ echo "# This is a test for Git Submodules - Secondary Repository" > README.md
-
Create a
main.tffile with the following content.resource "null_resource" "this" { } output "test" { value = "Hello from sub-repo" } -
Commit and push the changes.
$ git add . $ git commit -m "Initial commit" $ git push
Step 5: Add the Submodule to the Main Repository
Reference the secondary repository as a submodule from within the main repository.
-
Navigate to the main repository directory.
$ cd ~/test-directory/terraform-main
-
Add
terraform-secondaryas a submodule. You can use either an HTTPS or SSH URL.## Option A: Use HTTPS URL $ git submodule add https://github.com/<org>/<terraform-secondary>.git ## Option B: Use SSH URL $ git submodule add git@github.com:<org>/<terraform-secondary>.git
-
Verify the content of the
.gitmodulesfile. The output will vary based on the URL you used.$ cat .gitmodules
Example output for HTTPS:
[submodule "terraform-secondary"] path = terraform-secondary url = https://github.com/<org>/<terraform-secondary>.git
-
Commit and push the changes.
$ git add . $ git commit -m "adding submodule" $ git push
Step 6: Connect Terraform Enterprise to Your VCS Provider
If you have not already done so, connect your Terraform Enterprise instance to your GitHub account. For detailed instructions, refer to the official documentation on how to Connect to VCS Providers.
Step 7: Create and Configure a VCS-backed Workspace
Create a new workspace in Terraform Enterprise and configure it to use the main repository and its submodules.
- Log in to your Terraform Enterprise instance and navigate to your organization.
- Click Create new workspace.
- Select the Version Control Workflow.
- Choose GitHub as the VCS provider.
- Select the
terraform-mainrepository. - Click Create workspace.
- Navigate to your workspace's Settings page, then select Version Control.
- Check the Include submodules on clone option and click Update VCS settings.
Step 8: Trigger a Run
Finally, trigger a run to verify that Terraform Enterprise correctly clones the repository and its submodule.
- In your workspace, click Actions and select Start new run.
- Observe the plan output. It should show that Terraform plans to create the
null_resourcedefined in the submodule. - Click Confirm & Apply to complete the run.