Integrating Cloud Governance with CI/CD Pipeline using Terraform

By Firefly
Discover how integrating cloud governance into CI/CD pipelines with Firefly ensures security, compliance, and cost optimization through automation, early detection, and seamless multi-cloud management.
Cloud governance
Multi-cloud

In this article

Cloud governance, done right, gives you a way to manage policies for security, compliance, resource management, and operational efficiency in order to, in turn, ensure that cloud infrastructure, applications, and data are secure, cost-effective, and compliant with regulations.

Without proper governance, cloud infrastructure faces several problems:

  • Misconfigured settings, poor access controls and a lack of encryption expose sensitive data to breaches
  • Failure to comply with regulations such as GDPR, HIPAA, or PCI-DSS risks fines and reputation damage.
  • Poor governance leads to inefficiencies, redundant work, uncontrolled costs, and infrastructure sprawl.
  • Lack of oversight results in difficult-to-manage environments with inconsistent configurations and limited team visibility.

When it comes to governance, many cloud practitioners lean on CI/CD pipelines because they can speed up development and reduce errors, but governance at each pipeline stage can be challenging as teams move quickly. That challenge compounds at enterprise scale, where dozens of teams and multiple clouds turn "challenging" into genuinely unmanageable without a federated model; our guide to enterprise cloud governance covers what that looks like in practice.

Why is Cloud Governance Important in CI/CD Pipelines?

Integrating governance into the CI/CD pipeline ensures that cloud infrastructure and applications are compliant according to the governance rules before production deployment. Governance should be automated and enforced as part of the CI/CD lifecycle for the following reasons:

Security Assurance: Cloud environments are often targeted by attackers. Enforcing security governance within the CI/CD pipeline helps prevent vulnerabilities or misconfigurations from reaching the production environment.

Consistency Across Environments: Governance policies ensure that all resources, such as databases, storage, and compute instances, are consistent across development, staging, and production environments. This ensures there is no difference between the environments and reduces the possibility of errors during deployments.

Early Detection of Issues: It enforces governance early on in the pipeline so that misconfigurations or violations are caught before they get into production. This significantly reduces downtime and operational disruption.

Cost Control: Cloud usage monitoring in the CI/CD pipeline enables DevOps teams to provision only the necessary resources and avoid cost-inefficient configurations, such as over-provisioned VMs.

Automation: Automated governance ensures that security checks, cost monitoring, and compliance rules are applied consistently throughout the CI/CD pipeline, and manual oversight is reduced as much as possible, improving efficiency. Pipeline-stage enforcement is one half of the picture — for how Firefly extends the same governance continuously across live, running infrastructure too, see our guide to governing your cloud and staying compliant.

How to Integrate Cloud Governance Using Firefly in CI/CD Pipelines

Firefly is a cloud asset management solution that integrates with major cloud providers like AWS, Azure, and Google Cloud. It helps organizations enforce security, compliance, and cost policies in their cloud environments. Introducing Firefly into your CI/CD pipeline will help enforce governance rules automatically during the development and deployment process. 

Capabilities include:

  •  Custom Governance Policies: Firefly enables custom governance policies for security, compliance, and cost management. You can define policies around encryption, IAM roles, tagging, instance types, and so on.
  •  Real-Time Compliance: Firefly continuously scans your infrastructure and deployment pipeline for compliance with your governance policies on all resources.
  • Cost Control: You can set up cost-saving policies to prevent unnecessary resource provisioning and enforce limits on cloud spending.

Below is how to add Firefly to a GitLab CI pipeline. Steps for other CI/CD tools like Jenkins or GitHub Actions are analogous.

Step 1: Define Governance Policies in Firefly

If you wish to enforce tags for EC2 instances for AWS. 

  • Click on "Create Policy" or "Add New Policy" depending on your interface.
  • Select "Custom Policy" to write your own rule.
Firefly Policy Details form creating a custom policy named EC2_should_always_have_tags with category, severity, data source and asset type
Firefly custom policy detail for EC2_should_always_have_tags showing the Rego rule matching resources with no tags and 3 matching assets

 This policy checks that all EC2 instances have tags configured. That's a UI-driven policy, but the same Rego logic can be enforced directly at the Terraform plan stage before anything reaches Firefly - see our walkthrough on automating Terraform security checks with OPA and Rego for how to wire that gate into the pipeline itself.

Step 2: Write a Terraform script.

Now let's define the infrastructure you want to provision using Terraform.

terraform {  required_providers {    aws = {      source  = "registry.terraform.io/hashicorp/aws"      version = ">= 4.2.0"    }  } } provider "aws" {  region = "us-west-2" } resource "aws_instance" "ec2-instance" {  ami           = "ami-04dd23e62ed049936"  instance_type = "t2.micro" }

This Terraform configuration provisions an EC2 instance using an AMI in the us-west-2 region.

Step 3: Create a GitLab CI Pipeline

To create a CI/CD pipeline in GitLab, define the stages, jobs, and configurations required for your project. The following are the steps necessary to create a pipeline in GitLab:

Step 1: Setting Up Your GitLab Repository

To start with the creation of a pipeline, make sure that you have a set-up GitLab project/repository ready for your codebase. In case you do not have a GitLab repository:

Navigate to your GitLab account.

Click on New Project and either create a new repository or import an existing one.

Step 2: Create a.gitlab-ci.yml File

The pipeline in GitLab is defined by a YAML file called.gitlab-ci.yml. This file must be placed in the root directory of your repository.

In your project repository, create a new file called.gitlab-ci.yml.

Add the pipeline configuration to the.gitlab-ci.yml file. Here's an example of a simple CI/CD pipeline configuration:

image:  name: "$CI_TEMPLATE_REGISTRY_HOST/gitlab-org/terraform-images/releases/1.4:v1.0.0" stages:  - init  - validate  - plan  - apply variables:  TF_VERSION: "1.3.0"  AWS_REGION: "us-west-2"  TF_ROOT: ${CI_PROJECT_DIR} init:  stage: init  before_script: cd ${TF_ROOT}  script:    - terraform init validate:   stage: validate   before_script: cd ${TF_ROOT}   script:     - terraform validate plan:  stage: plan  script:    - terraform plan -out=tfplan apply:  stage: apply  before_script: cd ${TF_ROOT}  script:    - terraform apply tfplan

In this example, the CI/CD pipeline includes the following stages:

Stage 1 - Init: Initializes Terraform.
Stage 2 - Validate: Validate the Terraform configuration.
Stage 3 - Plan: Preview the changes Terraform will make.
Stage 4 - Apply: Applies the Terraform plan to provision the resources.

Step 3: Push the.gitlab-ci.yml to Your GitLab Repository

Add and commit the.gitlab-ci.yml file in your GitLab repository.

git add.gitlab-ci.yml git commit -m "Add GitLab CI/CD pipeline configuration" git push origin main

Step 4: Pipeline Execution Monitoring

After adding the.gitlab-ci.yml file in your repository, GitLab will automatically trigger a pipeline.

Navigate to your GitLab repository's CI/CD section.

Open Pipelines to check on your pipeline.

You will see the phases and tasks defined in the.gitlab-ci.yml file as well as their status; for example, success, failed, or running.

GitLab pipeline view listing the CI/CD stages and jobs with their statuses
GitLab CI job log showing terraform apply creating an S3 bucket and EC2 instance, apply complete with 3 resources added

Step 4: Monitor and Audit

After setting up the pipeline, monitoring and auditing the infrastructure for compliance is important. You can check the compliance status by either going to the Governance tab to view the assets that match the policy or by searching for the policy in the Inventory tab.

Firefly Governance view listing custom policies with severity, compliance percentage, frameworks and violating-asset counts
Firefly Inventory view with IaC Coverage and asset counts by drifted, unmanaged, ghost and codified

Why Integrate Firefly into Your CI/CD Pipeline?

Firefly makes cloud governance easier than ever to get right by providing a comprehensive solution that enhances security, optimizes costs, and ensures continuous compliance across multi-cloud environments. By automatically enforcing security best practices, detecting misconfigurations, and integrating compliance checks directly into the infrastructure lifecycle, Firefly enables organizations to maintain robust, efficient, and secure cloud infrastructure with minimal manual intervention. Plus, Firefly’s AI-driven approach allows teams to scale their cloud operations seamlessly, reducing risks, controlling expenses, and maintaining consistent governance policies across different cloud providers and deployment environments.

Ready to see Firefly in action?

Discover how Firefly can help you recover your infrastructure from outages
and keep your cloud resilient