Terraform Cloud Integration with GitLab

Hello guys, back with another article. In article i will be going through terraform cloud set up and its integration with GitLab to create a basic Dynamo-db table in aws.

What is Terraform Cloud ?

Terraform Cloud is HashiCorps managed service offering that eliminates the need for unnecessary tooling and documentation to use Terraform in production. Provision infrastructure securely and reliably in the cloud with free remote state storage. As you scale, add workspaces for better collaboration with your team.

Take Away from this article:

1. Terraform Cloud account creation.

2. Terraform login.

3. Creating your workflow.

4. Integration with gitlab and terraform cloud.

5. Cost Estimation of every resource.

6. Init, plan, apply using terraform cloud

So lets begin with creating account in terraform cloud.

1. Terraform Cloud account create:

We will be creating terraform account from the terraform link. When you open this link you will be landed on the below page and it will ask you to write the username, email id and password. Accept the terms and condition and click on create account.

After that it will ask you to click on the confirmation and you need to login with the username and password you have created. This page will appear when you will be clicking on the confirmation link. Click on Try your setup workflow.

2. Create your workflow:

You need to run below commands to login into terrafrom and cloning the repository and running the script.

terraform logingit clone https://github.com/hashicorp/tfc-getting-started.gitcd tfc-getting-started

After running it will take you to the browser and you need to generate token and paste it in CLI and then hit enter.

You will be seeing the Terraform Logo in the cli. Now you have cline the repository and run the setup.sh script.

git clone https://github.com/hashicorp/tfc-getting-started.gitcd tfc-getting-started

Now it will ask you to do initialize the terraform code which you have cloned from github. This is just a demo terraform code which will set the workflow environment in your respective account.

It will also show you the cost estimation of the resources. You can check the amount of cost will be charged while deploying the resources. As this is just a demo resource provided by terraform only so the cost estimation charge will be 0.

After cost estimation, terraform will ask you to proceed with apply. If you and your organization is ok with the cost of the resources then you can head towards deploying the resources.

3. Integrating GitLab with Terraform Cloud:

After the applying command run successfully, just navigate to workspace in terraform gui. Then you will be seeing that only getting-started workspace is created which you have created by running all those terraform commands.

We will be creating a new workspace. Click on create workspace and then choose the VCS(version control system) as a version control workflow.

While connecting to VCS click on connect to a different vcs. And select gitlab.com.

Now navigate to your gitlab profile and click on edit profile. Then click on application and it will ask you the following fields to be filled in the below image.

As you can see in the below image, that left part is of gitlab and right part is of terraform cloud.

  1. Enter the name from terraform cloud dashboard to gitlab application page.
  2. Enter the Redirect URI from terraform cloud dashboard to gitlab application page.
  3. Select api from the scopes section of the gitlab application page.
  4. Now save the gitlab application.

5. After saving the gitlab application it will provide you Application ID and you need to copy the application ID from gitlab application page and paste it to terraform cloud dashboard.

6. Also you have to copy the secret and paste it in the terraform cloud dashboard.

7. Click continue in gitlab application page and click on connect and continue in the terraform cloud dashboard.

After saving the terraform cloud dashboard it will take you to below page to authorize the account. Click on authorize.

After authorize it will ask you to generate ssh key pair. just ignore as for now. we dont need it.

As you can see that in the provider section we have list of providers added. The gitlab provider we just have created.

Now got to workspace and click on new workspace.

Choose a type of version control flow. Here it is same as we have done while creating our gitlab provider. so we will be choosing version control system (VCS).

4. Creating Workspace:

Now you see that the provider for gitlab which we have created can be seen here. Click on the gitlab.com. AmitWs1 is the name of the workspace.

Now you will be able to see how many repositories you will have. I have created fresh gtilab account so you not be seeing much repositories. I created only one just for demo.

In the configure setting section, just click on create repository. If you have directory then you can also add directory.

6. Adding Variables

After clicking on Create Workspace then you can see the variable option above. just ,click on that and we will adding environmental variables and terraform files variables.

As you can see in the below two images I am adding AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY as an environmental variable. While selecting environmental variable you also have check the sensitive box. This environmental variable will help to authenticate terraform to deploy resources over aws.

While adding terraform variables you only have to select terraform varible and the add the key names and value names of the variables. Remember that this is same as terraform.tfvars file. In the gitlab i have only provider.tf(aws provider), dynamodb.tf(for synamodb), variable.tf(just only declared all the variables). So, terraform cloud will be assigning the values of undeclared variable in the gitlab respective repository.

As you can see in the below image that i have total 10 variable including access keys and secret keys.

7. Terraform init, plan, apply:

After all that, we need to run he terraform code. so you can run via navigating towards action. select start a new plan.

Just commit the plan, it will rename the job and run the terraform code.

As you can see below that the plan part has run successfully. you can drop down the plan part and see what is resources terraform will be provisioning.

Terraform cloud offers you to check the cost of the resources before applying into production. This is nice to see the cost of the resources before deploying.

After seeing the cost terraform cloud will hold on developers option whether we can procced with apply or not. Just commit for what purpose you applying.

As you can see that the terraform cloud has successfully deployed the dynamo db table in aws.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store