Infrastructure as code with Terraform and GitLab (FREE)
Motivation
The Terraform integration features in GitLab enable your GitOps / Infrastructure-as-Code (IaC) workflows to tie into GitLab authentication and authorization. These features focus on lowering the barrier to entry for teams to adopt Terraform, collaborate effectively in GitLab, and support Terraform best practices.
Quick Start
Use the following .gitlab-ci.yml
to set up a basic Terraform project integration
for GitLab versions 13.5 and later:
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
# If your terraform files are in a subdirectory, set TF_ROOT accordingly
# TF_ROOT: terraform/production
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.
- Using the GitLab managed Terraform State as the Terraform state storage backend.
- Creating four pipeline stages:
init
,validate
,build
, anddeploy
. These stages run the Terraform commandsinit
,validate
,plan
,plan-json
, andapply
. Theapply
command only runs onmaster
.
This video from January 2021 walks you through all the GitLab Terraform integration features:
GitLab Managed Terraform state
Terraform remote backends enable you to store the state file in a remote, shared store. GitLab uses the Terraform HTTP backend to securely store the state files in local storage (the default) or the remote store of your choice.
The GitLab managed Terraform state backend can store your Terraform state easily and securely. It spares you from setting up additional remote resources like 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.
Read more on setting up and using GitLab Managed Terraform states
WARNING: Like any other job artifact, Terraform plan data is viewable by anyone with Guest access 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.
Terraform integration in Merge Requests
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.
Read more on setting up and using the merge request integrations.
The GitLab terraform provider
WARNING: The GitLab Terraform provider is released separately from GitLab. We are working on migrating the GitLab Terraform provider for GitLab.com.
You can use the GitLab Terraform provider to manage various aspects of GitLab using Terraform. The provider is an open source project, owned by GitLab, where everyone can contribute.
The documentation of the provider is available as part of the official Terraform provider documentations.