Streamlining Infrastructure as Code with Terraform and GitLab on Google Cloud Platform
- Daniyal Javed
- Nov 19, 2023
- 2 min read
Updated: Dec 26, 2023
Streamlining Infrastructure as Code with Terraform and GitLab on Google Cloud Platform
Introduction
In today's fast-paced and dynamic IT landscape, Infrastructure as Code (IaC) has become a cornerstone for managing and provisioning infrastructure efficiently. Terraform, a popular IaC tool, provides a robust and declarative approach to defining, deploying, and managing infrastructure. When coupled with GitLab, a powerful DevOps platform, the collaboration and version control capabilities make it an ideal combination for managing infrastructure throughout its lifecycle. In this blog, we'll explore how to leverage Terraform and GitLab together, showcasing the benefits of this integration.
Setting Up the Project
The first step is to create a new project in GitLab where you'll manage your infrastructure code. Let's assume you have a simple web application that you want to deploy on Google Cloud Platform using Terraform. Here's a step-by-step guide:
Create a new GitLab project: Log in to GitLab, navigate to your dashboard, and create a new project. Choose a name and set the visibility level according to your requirements.
Clone the project: Clone the newly created project to your local machine using the GitLab repository URL.
bashCopy code
git clone <your-gitlab-repo-url>
cd <your-project-directory>Integrating Terraform with Google Cloud Platform
Now, let's integrate Terraform into our GitLab project.
Create Terraform files: Create a directory structure for your Terraform files. For example:
/terraform
└── main.tf
└── variables.tf
└── outputs.tf
└── terraform.tfvarsWrite Terraform code: Open the Terraform files using your preferred text editor and define your infrastructure. Below is a basic example for a Google Cloud Compute Engine instance.
provider "google" {
credentials = file("<path-to-your-gcp-service-account-key>")
project = "your-gcp-project-id"
region = "us-central1"
}
resource "google_compute_instance" "web" {
name = "web-instance"
machine_type = "n1-standard-1"
tags = ["web-server"]
boot_disk {
initialize_params {
image = "debian-cloud/debian-10"
}
}
}
Initialize and apply Terraform: Initialize Terraform within your project directory and apply the configuration.
cd terraform
terraform init
terraform apply
Version Control with Git
Now, let's leverage GitLab for version control.
Create a .gitignore file: Ensure that Terraform state files and any sensitive information are not committed. Create a .gitignore file and add the following:
*.tfstate
*.tfstate.*
*.tfvars
.terraform/
Commit and push changes: Add your Terraform files, commit the changes, and push them to GitLab.
git add .
git commit -m "Initial Terraform configuration"
git push origin master
Continuous Integration with GitLab CI/CD
Automate your infrastructure deployments using GitLab CI/CD pipelines.
Create a .gitlab-ci.yml file: Define your CI/CD pipeline stages and jobs in a .gitlab-ci.yml file.
stages:
- plan
- apply
terraform:
stage: plan
script:
- cd terraform
- terraform init
- terraform plan -out=plan
apply:
stage: apply
script:
- cd terraform
- terraform apply "plan"
only:
- master
This simple pipeline will run Terraform plan in the planning stage and apply the changes in the applying stage when changes are pushed to the master branch.
Conclusion
Integrating Terraform with GitLab offers a streamlined approach to managing infrastructure as code. The combination of Terraform's declarative syntax and GitLab's version control and CI/CD capabilities enhances collaboration and ensures a robust and automated infrastructure deployment process. Start leveraging these tools today to bring efficiency and scalability to your infrastructure management workflows.
_edited.png)



Comments