Terraform and GitLab

Store Terraform State in GitLab

Run Terraform Commands in GitLab CI/CD and Store Terraform State in GitLab Infrastructure

Apr 18, 2023

--

GitLab Infrastructure

In this article, I will show how to store Terraform state in GitLab while deploying Terraform resources with GitLab CI/CD pipeline.

First, add HTTP part to your Terraform block

terraform {
backend "http" {}
}

Second, set two global environment variables in GitLab. These will be used for storing Terraform state inside your GitLab

GITLAB_USER_NAME=<your-gitlab-username>
GITLAB_ACCESS_TOKEN=<gitlab-personal-access-token>

Do not forget to select api scope while creating the access token

Then configure your pipeline accordingly

After the pipeline successfully runs, you can check the Terraform state in GitLab under Infrastructure/Terraform states

Terraform States in GitLab

RESOURCES:
https://docs.gitlab.com/ee/user/infrastructure/iac/terraform_state.html

--

--

Responses (1)