- Integrate your project with Terraform
- GitLab-managed Terraform state
- Terraform module registry
- Terraform integration in merge requests
- The GitLab Terraform provider
- Create a new cluster through IaC
- Related topics
To manage your infrastructure with GitLab, you can use the integration with Terraform 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.
- Incorporate GitOps deployments and Infrastructure-as-Code (IaC) workflows.
- Use GitLab as a Terraform state storage.
- Store and use Terraform modules to simplify common and complex infrastructure patterns.
Watch a video overview of the features GitLab provides with the integration with Terraform.
SAST test was introduced in GitLab 14.6.
The integration with GitLab and Terraform happens through GitLab CI/CD.
include attribute to add the Terraform template to your project and
customize from there.
To get started, choose the template that best suits your needs:
- Use the GitLab-managed Terraform state as the Terraform state storage backend.
- Trigger four pipeline stages:
- Run Terraform commands:
plan-json. It also runs the
applyonly on the default branch.
- Run the Terraform SAST scanner.
The latest template is compatible with the most recent GitLab version. It provides the most recent GitLab features, but can potentially include breaking changes.
You can safely use the latest Terraform template:
- If you use GitLab.com.
- If you use a self-managed instance updated with every new GitLab release.
If you use earlier versions of GitLab, you might face incompatibility errors between the GitLab version and the template version. In this case, you can opt to use one of these templates:
- The stable template with an skeleton that you can built on top of.
- The advanced template to fully customize your setup.
To use a Terraform template:
- On the top bar, select Menu > Projects and find the project you want to integrate with Terraform.
- On the left sidebar, select Repository > Files.
.gitlab-ci.ymlfile, use the
includeattribute to fetch the Terraform template:
include: # To fetch the latest template, use: - template: Terraform.latest.gitlab-ci.yml # To fetch the stable template, use: - template: Terraform/Base.gitlab-ci.yml # To fetch the advanced template, use: - template: Terraform/Base.latest.gitlab-ci.yml
Add the variables as described below:
variables: TF_STATE_NAME: default TF_CACHE_KEY: default # If your terraform files are in a subdirectory, set TF_ROOT accordingly. For example: # TF_ROOT: terraform/production
- (Optional) Override in your
.gitlab-ci.yamlfile the attributes present in the template you fetched to customize your configuration.
Use the GitLab-managed Terraform state to store state files in local storage or in a remote store of your choice.
Use GitLab as a Terraform module registry to create and publish Terraform modules to a private registry.
Use the Terraform integration in merge requests to collaborate on Terraform code changes and Infrastructure-as-Code workflows.
- Learn how to create a new cluster on Amazon Elastic Kubernetes Service (EKS).
- Learn how to create a new cluster on Google Kubernetes Engine (GKE).