{"id":1104,"date":"2023-02-18T10:26:03","date_gmt":"2023-02-18T09:26:03","guid":{"rendered":"http:\/\/192.168.1.213:8088\/?p=1104"},"modified":"2023-10-13T05:40:12","modified_gmt":"2023-10-13T04:40:12","slug":"gitlab-as-a-terraform-state-file-backend","status":"publish","type":"post","link":"http:\/\/192.168.1.213:8088\/gitlab-as-a-terraform-state-file-backend\/","title":{"rendered":"GitLab as a Terraform state file backend \ud83d\udcdd"},"content":{"rendered":"\t\t
\n\t\t\t\t\t\t
\n\t\t\t\t\t\t
\n\t\t\t\t\t
\n\t\t\t
\n\t\t\t\t\t\t
\n\t\t\t\t
\n\t\t\t\t\t
\n\t\t\t

\n\t\t\t\tTable of Contents\t\t\t<\/h4>\n\t\t\t\t\t\t\t
<\/i><\/div>\n\t\t\t\t
<\/i><\/div>\n\t\t\t\t\t<\/div>\n\t\t
\n\t\t\t
\n\t\t\t\t<\/i>\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t
\n\t\t\t\t
\n\t\t\t
Introduction<\/h5>\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t
\n\t\t\t\t
\n\t\t\t\t\t\t\t

When I started my first Terraform project within GitLab I wondered where should I store my state file. One option could be to store it in my Git repository but is there a better way?!\u00a0<\/p>

The answer is yes there is! This article is about how I set up my pipeline in order to use the GitLab server as my state file backend. There are a couple things that need to be considered.\u00a0\u00a0<\/p>

Read more about it here: https:\/\/docs.gitlab.com\/ee\/user\/infrastructure\/iac\/terraform_state.html<\/a><\/p>

In the following article, I will configure an ACI environment and use GitLab CE 15.8 on Ubuntu 22.04 as my state file backend.<\/p>\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t

\n\t\t\t\t
\n\t\t\t
Create an environment, API key & variables<\/h5>\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t
\n\t\t\t\t
\n\t\t\t\t\t\t\t

First, create a separate environment. In the GitLab WebUI go to Deployments > Environments > New environment<\/strong><\/p>\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t

\n\t\t\t\t
\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t\"\"\t\t\t\t\t\t\t\t<\/a>\n\t\t\t\t\t\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t
\n\t\t\t\t
\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t\"\"\t\t\t\t\t\t\t\t<\/a>\n\t\t\t\t\t\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t
\n\t\t\t\t
\n\t\t\t\t\t\t\t

Next create an access token, which is needed to communicate with the GitLab server via API and create\/update the state file after changes.\u00a0<\/p>

Go to\u00a0Settings > Access Tokens\u00a0<\/strong>and enter the following input for the project (as an example).<\/p>

Token name:\u00a0terraform<\/strong>
Role:\u00a0Maintainer<\/strong>
Select\u00a0api\u00a0<\/strong><\/p>\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t

\n\t\t\t\t
\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t\"\"\t\t\t\t\t\t\t\t<\/a>\n\t\t\t\t\t\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t
\n\t\t\t\t
\n\t\t\t\t\t\t\t

Copy the token and save it in a temp file.
The token can not be viewed again and you need it later!<\/strong><\/p>\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t

\n\t\t\t\t
\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\n\t\t\t\t\t\t\t\"\"\t\t\t\t\t\t\t\t<\/a>\n\t\t\t\t\t\t\t\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t
\n\t\t\t\t
\n\t\t\t\t\t\t\t

Create the following variables in Settings > CI\/CD > Expand Variables<\/strong>:<\/p>