{"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
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 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 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> Copy the token and save it in a temp file. Create the following variables in Settings > CI\/CD > Expand Variables<\/strong>:<\/p> \u00a0<\/p> Make sure that you create the variables and choose the right environment \ud83d\ude09.<\/p>\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t Create a file called init_file.sh<\/strong> which will run in your pipeline to initialize the Terraform environment to use your GitLab server with all the required credentials (user \/ API key).<\/p>\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t Add the following lines to your main.tf<\/strong> which will tell Terraform to not store the state file locally.<\/p> More about possible backends can be found here: https:\/\/developer.hashicorp.com\/terraform\/language\/settings\/backends\/configuration<\/a><\/p>\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t As an example, I attached the entire pipeline content that I created in my lab to make changes in my ACI environment.\u00a0\u00a0<\/p> The pipeline consists of the following stages<\/p> Let’s make changes and to trigger the pipeline.<\/p>\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t In the GitLab WebUI, go to\u00a0Infrastructure > Terraform<\/strong>\u00a0and download the\u00a0state file (JSON)<\/strong>\u00a0and make yourself familiar with the content.<\/p>\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\tCreate an environment, API key & variables<\/h5>\t\t<\/div>\n\t\t\t\t<\/div>\n\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
\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
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\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
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\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
\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
Create a custom script<\/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
Adjust the main.tf<\/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
Adjust the pipeline<\/h5>\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t
I am building my own Docker container with the required tools and storing it in my GitLab container registry.<\/li>
Validate the Terraform config files for error.<\/li>
The Terraform plan command helps to identify exactly which resources will be created, replaced, changed, or destroyed without executing.\u00a0<\/li>
Terraform executes the necessary changes to the infrastructure.\u00a0<\/li><\/ul>\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
Run the pipeline and check the state file<\/h5>\t\t<\/div>\n\t\t\t\t<\/div>\n\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
\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
\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