IaC CI with GitLab and Terraform

IaC CI with GitLab and Terraform

Gitlab instance for source and CI job control/triggers; A machine running Gitlab CI agent(Gitlab runner) to execute jobs; Terraform environment on ... Infrastructure as a code has never been easier.

There are lots of options nowadays to describe and actually deploy your infrastructure as a code (IaC) — here we are gonna take a look at how to set up some quick& dirty continuous integration (CI) using the increasingly popular Terraform tool to spin up IaC and Gitlab as a source-control and CI platform.

How does it work? Well, ideally, you simply edit your Terraform resource files using your favorite editor (MS Word not allowed), push the repo via git to Gitlab and the GitLab’s CI Pipeline will make required changes directly to your infrastructure. There’s a couple components to this:

  • your local git and Terraform tools for managing the repo and the code
  • Gitlab instance for source and CI job control/triggers
  • A machine running Gitlab CI agent(Gitlab runner) to execute jobs
  • Terraform environment on Gitlab runner machine to CI-execute terraform plans.

First of all, you should start by setting up your Terraform repo on your Gitlab instance, and clone it locally (yes, of course, you should have git!). We’ll presuppose your Gitlab’s URL as gitlab.comfor example sake.

You should also download and install the Terraform binary from https://www.terraform.io/.

Create a file backend.tf in project’s root so Terraform knows to use HTTP state backend:

terraform {
 backend "http" {}

After that’s said and done, you should initialize the Terraform workspace (with a bit of a twist):

terraform init \
-backend-config="address=https://gitlab.com/api/v4/projects/<PROJECT_ID>/terraform/state/<YOUR_STATE>" \
-backend-config="lock_address=https://gitlab.com/api/v4/projects/<PROJECT_ID>/terraform/state/<YOUR_STATE>/lock" \
-backend-config="unlock_address=https://gitlab.com/api/v4/projects/<PROJECT_ID>/terraform/state/<YOUR_STATE>/lock" \
-backend-config="username=<YOUR_USERNAME>" \
-backend-config="password=<YOUR_TOKEN>" \
-backend-config="lock_method=POST" \
-backend-config="unlock_method=DELETE" \

Terraform backend is usually stored locally, but we declare it as a Http remote in backend.tf and initialize it on your Gitlab’s terraform project so it remains consistent between various agents using it (including the Gitlab runner). Otherwise each agent (human or machine) will have its own state which leads to creating duplicates of defined infrastructure — and on each run of the CI pipeline!

devops infrastructure-as-code gitlab-ci terraform

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

How To Manage Infrastructure With Terraform

Terraform enables you to safely and predictably create, change, and improve ... Use Infrastructure as Code to provision and manage any cloud, infrastructure

How to Extend your DevOps Strategy For Success in the Cloud?

DevOps and Cloud computing are joined at the hip, now that fact is well appreciated by the organizations that engaged in SaaS cloud and developed applications in the Cloud. During the COVID crisis period, most of the organizations have started using cloud computing services and implementing a cloud-first strategy to establish their remote operations. Similarly, the extended DevOps strategy will make the development process more agile with automated test cases.

Infrastructure to Code: Terraformer

terraformer — a CLI tool that generates tf/json and tfstate files based on existing infrastructure. What if I say, that you can just draw your cloud infrastructure in the web interface.

Microtica vs. GitLab CI | What Are the Differences?

Microtica and GitLab CI both have the goal to efficiently and reliably deliver software in the cloud. Although both tools. The differences between the core concepts are significant.

Complex Infrastructure as Code via Azure Devops YAML Pipeline

These days we can define infrastructure as code (ARM template)and CI/CD pipeline as code (YAML pipelines) in the Azure ecosystem. But why do we want to do these tasks as code?