2020-05-24 23:13:21 +05:30
---
stage: Configure
group: Configure
2021-02-22 17:27:13 +05:30
info: To determine the technical writer assigned to the Stage/Group associated with this page, see https://about.gitlab.com/handbook/engineering/ux/technical-writing/#assignments
2020-05-24 23:13:21 +05:30
---
2020-01-01 13:55:28 +05:30
2021-03-11 19:13:27 +05:30
# Infrastructure as code with Terraform and GitLab **(FREE)**
2020-01-01 13:55:28 +05:30
2020-07-28 23:09:34 +05:30
## Motivation
2021-03-08 18:12:59 +05:30
The Terraform integration features in GitLab enable your GitOps / Infrastructure-as-Code (IaC)
2021-02-22 17:27:13 +05:30
workflows to tie into GitLab authentication and authorization. These features focus on
2021-03-08 18:12:59 +05:30
lowering the barrier to entry for teams to adopt Terraform, collaborate effectively in
2020-07-28 23:09:34 +05:30
GitLab, and support Terraform best practices.
2021-03-08 18:12:59 +05:30
## Quick Start
Use the following `.gitlab-ci.yml` to set up a basic Terraform project integration
2021-09-30 23:02:18 +05:30
for GitLab versions 14.0 and later:
2021-03-08 18:12:59 +05:30
```yaml
include:
2021-09-04 01:27:46 +05:30
- template: Terraform.gitlab-ci.yml
2021-03-08 18:12:59 +05:30
variables:
# If not using GitLab's HTTP backend, remove this line and specify TF_HTTP_* variables
TF_STATE_NAME: default
TF_CACHE_KEY: default
2021-03-11 19:13:27 +05:30
# If your terraform files are in a subdirectory, set TF_ROOT accordingly
# TF_ROOT: terraform/production
2021-03-08 18:12:59 +05:30
```
2021-09-04 01:27:46 +05:30
This template includes some opinionated decisions, which you can override:
2021-03-08 18:12:59 +05:30
- Including the latest [GitLab Terraform Image ](https://gitlab.com/gitlab-org/terraform-images ).
- Using the [GitLab managed Terraform State ](#gitlab-managed-terraform-state ) as
the Terraform state storage backend.
2021-09-04 01:27:46 +05:30
- Creating [four pipeline stages ](https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/gitlab/ci/templates/Terraform.gitlab-ci.yml ):
2021-03-08 18:12:59 +05:30
`init` , `validate` , `build` , and `deploy` . These stages
2021-09-04 01:27:46 +05:30
[run the Terraform commands ](https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/gitlab/ci/templates/Terraform/Base.gitlab-ci.yml )
2021-09-30 23:02:18 +05:30
`init` , `validate` , `plan` , `plan-json` , and `apply` . The `apply` command only runs on the default branch.
2021-03-08 18:12:59 +05:30
2021-03-11 19:13:27 +05:30
This video from January 2021 walks you through all the GitLab Terraform integration features:
< div class = "video-fallback" >
See the video: < a href = "https://www.youtube.com/watch?v=iGXjUrkkzDI" > Terraform with GitLab< / a > .
< / div >
< figure class = "video-container" >
< iframe src = "https://www.youtube.com/embed/iGXjUrkkzDI" frameborder = "0" allowfullscreen = "true" > < / iframe >
< / figure >
2021-01-29 00:20:46 +05:30
## GitLab Managed Terraform state
2020-07-28 23:09:34 +05:30
2021-04-17 20:07:23 +05:30
[Terraform remote backends ](https://www.terraform.io/docs/language/settings/backends/index.html )
2020-05-24 23:13:21 +05:30
enable you to store the state file in a remote, shared store. GitLab uses the
2021-04-17 20:07:23 +05:30
[Terraform HTTP backend ](https://www.terraform.io/docs/language/settings/backends/http.html )
2020-05-24 23:13:21 +05:30
to securely store the state files in local storage (the default) or
[the remote store of your choice ](../../administration/terraform_state.md ).
The GitLab managed Terraform state backend can store your Terraform state easily and
2021-03-08 18:12:59 +05:30
securely. It spares you from setting up additional remote resources like
2020-05-24 23:13:21 +05:30
Amazon S3 or Google Cloud Storage. Its features include:
- Supporting encryption of the state file both in transit and at rest.
- Locking and unlocking state.
- Remote Terraform plan and apply execution.
2021-01-29 00:20:46 +05:30
Read more on setting up and [using GitLab Managed Terraform states ](terraform_state.md )
2020-05-24 23:13:21 +05:30
2021-02-22 17:27:13 +05:30
WARNING:
Like any other job artifact, Terraform plan data is [viewable by anyone with Guest access ](../permissions.md ) to the repository.
Neither Terraform nor GitLab encrypts the plan file by default. If your Terraform plan
includes sensitive data such as passwords, access tokens, or certificates, GitLab strongly
recommends encrypting plan output or modifying the project visibility settings.
2021-09-04 01:27:46 +05:30
## Terraform module registry
GitLab can be used as a [Terraform module registry ](../packages/terraform_module_registry/index.md )
to create and publish Terraform modules to a private registry specific to your
top-level namespace.
2021-01-29 00:20:46 +05:30
## Terraform integration in Merge Requests
2020-05-24 23:13:21 +05:30
2021-03-08 18:12:59 +05:30
Collaborating around Infrastructure as Code (IaC) changes requires both code changes
and expected infrastructure changes to be checked and approved. GitLab provides a
solution to help collaboration around Terraform code changes and their expected
effects using the Merge Request pages. This way users don't have to build custom
tools or rely on 3rd party solutions to streamline their IaC workflows.
2020-05-24 23:13:21 +05:30
2021-01-29 00:20:46 +05:30
Read more on setting up and [using the merge request integrations ](mr_integration.md ).
2020-05-24 23:13:21 +05:30
2021-09-30 23:02:18 +05:30
## The GitLab Terraform provider
2020-05-24 23:13:21 +05:30
2021-03-08 18:12:59 +05:30
WARNING:
The GitLab Terraform provider is released separately from GitLab.
We are working on migrating the GitLab Terraform provider for GitLab.com.
2020-10-24 23:57:45 +05:30
2021-03-08 18:12:59 +05:30
You can use the [GitLab Terraform provider ](https://github.com/gitlabhq/terraform-provider-gitlab )
to manage various aspects of GitLab using Terraform. The provider is an open source project,
owned by GitLab, where everyone can contribute.
2020-10-24 23:57:45 +05:30
2021-03-08 18:12:59 +05:30
The [documentation of the provider ](https://registry.terraform.io/providers/gitlabhq/gitlab/latest/docs )
is available as part of the official Terraform provider documentations.
2021-09-30 23:02:18 +05:30
## Create a new cluster through IaC
Learn how to [create a new cluster on Google Kubernetes Engine (GKE) ](clusters/connect/new_gke_cluster.md ).
## Troubleshooting
### `gitlab_group_share_group` resources not detected when subgroup state is refreshed
The GitLab Terraform provider can fail to detect existing `gitlab_group_share_group` resources
due to the issue ["User with permissions cannot retrieve `share_with_groups` from the API" ](https://gitlab.com/gitlab-org/gitlab/-/issues/328428 ).
This results in an error when running `terraform apply` because Terraform attempts to recreate an
existing resource.
For example, consider the following group/subgroup configuration:
```plaintext
parent-group
├── subgroup-A
└── subgroup-B
```
Where:
- User `user-1` creates `parent-group` , `subgroup-A` , and `subgroup-B` .
- `subgroup-A` is shared with `subgroup-B` .
- User `terraform-user` is member of `parent-group` with inherited `owner` access to both subgroups.
When the Terraform state is refreshed, the API query `GET /groups/:subgroup-A_id` issued by the provider does not return the
details of `subgroup-B` in the `shared_with_groups` array. This leads to the error.
To workaround this issue, make sure to apply one of the following conditions:
1. The `terraform-user` creates all subgroup resources.
1. Grant Maintainer or Owner role to the `terraform-user` user on `subgroup-B` .
1. The `terraform-user` inherited access to `subgroup-B` and `subgroup-B` contains at least one project.