2020-05-11 23:09:31 -04:00
---
stage: Configure
group: Configure
2020-11-26 01:09:20 -05:00
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-11 23:09:31 -04:00
---
2021-02-08 13:09:49 -05:00
# Infrastructure as code with Terraform and GitLab **(FREE)**
2020-05-18 14:08:22 -04:00
2020-06-23 11:08:41 -04:00
## Motivation
2020-12-17 19:10:04 -05:00
The Terraform integration features in GitLab enable your GitOps / Infrastructure-as-Code (IaC)
2020-12-10 16:10:15 -05:00
workflows to tie into GitLab authentication and authorization. These features focus on
2020-12-17 19:10:04 -05:00
lowering the barrier to entry for teams to adopt Terraform, collaborate effectively in
2020-06-23 11:08:41 -04:00
GitLab, and support Terraform best practices.
2020-12-17 19:10:04 -05:00
## Quick Start
Use the following `.gitlab-ci.yml` to set up a basic Terraform project integration
for GitLab versions 13.5 and later:
```yaml
include:
- template: Terraform.latest.gitlab-ci.yml
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-02-01 13:09:17 -05:00
# If your terraform files are in a subdirectory, set TF_ROOT accordingly
# TF_ROOT: terraform/production
2020-12-17 19:10:04 -05:00
```
This template uses `.latest.` , instead of stable, and may include breaking changes.
This template also includes some opinionated decisions, which you can override:
- 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.
- Creating [four pipeline stages ](https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/gitlab/ci/templates/Terraform.latest.gitlab-ci.yml ):
`init` , `validate` , `build` , and `deploy` . These stages
[run the Terraform commands ](https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/gitlab/ci/templates/Terraform/Base.latest.gitlab-ci.yml )
`init` , `validate` , `plan` , `plan-json` , and `apply` . The `apply` command only runs on `master` .
2021-02-01 13:09:17 -05:00
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 >
2020-11-09 16:08:48 -05:00
## GitLab Managed Terraform state
2020-06-22 17:08:42 -04:00
2021-02-19 19:10:55 -05:00
[Terraform remote backends ](https://www.terraform.io/docs/language/settings/backends/index.html )
2020-05-08 14:09:55 -04:00
enable you to store the state file in a remote, shared store. GitLab uses the
2021-02-19 19:10:55 -05:00
[Terraform HTTP backend ](https://www.terraform.io/docs/language/settings/backends/http.html )
2020-05-08 14:09:55 -04:00
to securely store the state files in local storage (the default) or
[the remote store of your choice ](../../administration/terraform_state.md ).
2019-12-17 04:07:48 -05:00
2020-05-08 14:09:55 -04:00
The GitLab managed Terraform state backend can store your Terraform state easily and
2020-12-17 19:10:04 -05:00
securely. It spares you from setting up additional remote resources like
2020-05-08 14:09:55 -04:00
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.
2020-11-09 16:08:48 -05:00
Read more on setting up and [using GitLab Managed Terraform states ](terraform_state.md )
2020-08-10 20:10:18 -04:00
2020-12-07 16:10:08 -05:00
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.
2020-11-09 16:08:48 -05:00
## Terraform integration in Merge Requests
2020-05-18 14:08:22 -04:00
2020-12-17 19:10:04 -05:00
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-18 14:08:22 -04:00
2020-11-10 13:09:07 -05:00
Read more on setting up and [using the merge request integrations ](mr_integration.md ).
2020-12-17 19:10:04 -05:00
## The GitLab terraform provider
2020-05-18 14:08:22 -04:00
2020-12-17 19:10:04 -05:00
WARNING:
The GitLab Terraform provider is released separately from GitLab.
We are working on migrating the GitLab Terraform provider for GitLab.com.
2020-07-21 02:09:45 -04:00
2020-12-17 19:10:04 -05:00
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-07-21 02:09:45 -04:00
2020-12-17 19:10:04 -05:00
The [documentation of the provider ](https://registry.terraform.io/providers/gitlabhq/gitlab/latest/docs )
is available as part of the official Terraform provider documentations.