Infrastructure as Code with OpenTofu and GitLab
- Tier: Free, Premium, Ultimate
- Offering: GitLab.com, GitLab Self-Managed, GitLab Dedicated
To manage your infrastructure with GitLab, you can use the integration with OpenTofu to define resources that you can version, reuse, and share:
- Manage low-level components like compute, storage, and networking resources.
- Manage high-level components like DNS entries and SaaS features.
- Use GitLab as an OpenTofu state storage.
- Store and use OpenTofu modules to simplify common and complex infrastructure patterns.
- Incorporate GitOps deployments and Infrastructure-as-Code (IaC) workflows.
Watch a video overview of the features GitLab provides with the integration with OpenTofu.
The following examples primarily use OpenTofu, but they can work with Terraform as well.
Terraform and OpenTofu support
GitLab integrates with both Terraform and OpenTofu. Most features are fully compatible, including:
- GitLab-managed Terraform/OpenTofu state
- Terraform/OpenTofu integration in merge requests
- Terraform/OpenTofu Module Registry
For simplicity, the GitLab documentation refers primarily to OpenTofu. However, differences between the Terraform and OpenTofu integration are documented.
Quickstart an OpenTofu project in pipelines
OpenTofu can integrate with all Terraform-specific GitLab features with the GitLab OpenTofu CI/CD component.
You can add a validate, plan, and apply workflow to your pipeline by including the component:
include:
- component: gitlab.com/components/opentofu/validate-plan-apply@<VERSION>
inputs:
version: <VERSION>
opentofu_version: <OPENTOFU_VERSION>
root_dir: terraform/
state_name: production
stages: [validate, build, deploy]
For more information about templates, inputs, and how to use the OpenTofu CI/CD component, see the OpenTofu CI/CD component README.
Build and host your own Terraform CI/CD templates
Although GitLab no longer distributes the Terraform CI/CD templates
and terraform-images
(the underlying job images, including terraform
),
you can still use Terraform in GitLab pipelines.
To learn how to build and host your own templates and images, see the Terraform Images project.
Related topics
- Use GitLab as a Terraform/OpenTofu Module Registry.
- To store state files in local storage or in a remote store, use the GitLab-managed Terraform/OpenTofu state.
- To collaborate on Terraform code changes and IaC workflows, use the Terraform integration in merge requests.
- To manage GitLab resources like users, groups, and projects, use the GitLab Terraform provider. The GitLab Terraform provider documentation is available on the Terraform docs site.
- Create a new cluster on Amazon Elastic Kubernetes Service (EKS).
- Create a new cluster on Google Kubernetes Engine (GKE).
- Troubleshoot issues with GitLab and Terraform.
- View the images that contain the
gitlab-terraform
shell script.