September 25, 2023


In this post, we are going to see about the steps achieve GitLab as a Code using Terraform CLI and Terraform Cloud.

We are going to use GitLab provider in Terraform Registry to create a GitLab project and a variable.

Using Terraform CLI:

Step 1 : Write the code

Kindly create the below mentioned files using any of the file editor (say vi, nano gedit, etc.,) and enter the below contents and save the files with the respective names.

  • – contains the details of the gitlab provider
  • – contains the details of the gitlab resources to be created (GitLab Project & GitLab Project Variable)
  • – contains the input variables(GitLab Project)
  • – contains the output variable(GitLab Project ID)

Step 2 : Create Personal Access Token (PAT) in GitLab Settings

Kindly click on the profile photo in the upper-right corner of GitLab page. Then click on Edit Profile.

Under User Settings click on Access Tokens. Personal Access Token screen will appear.

Kindly enter the below details & then click on Create Personal Access Token button.

  • Token Name : tf-gitlab-demo
  • Expiration date : 2021-08-25
  • Select scopes: Select the respective check boxes(api,read_repository,write_repository)
Personal Access Token
Personal Access Token


Personal Access Token
Personal Access Token

Kindly copy the generated token and store it in a secured place (for example, Hashicorp Vault).

Step 3: Terraform Execution

Kindly execute the below command to save the Personal Access Token as environment variable as a security best practice.

Now Kindly execute the below commands to create a repository in GitHub.



GitLab Project & Variable :

GitLab Project
GitLab Project


Project Variable
Project Variable

Leave a Reply

Your email address will not be published. Required fields are marked *