September 13, 2021
terraform_gitlab

terraform_gitlab

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.

  • gitlab_provider.tf – contains the details of the gitlab provider
  • gitlab_resources.tf – contains the details of the gitlab resources to be created (GitLab Project & GitLab Project Variable)
  • variables.tf – contains the input variables(GitLab Project)
  • outputs.tf – 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 *