GitLab Terraform Pipeline

Published
Length
3 min read

Luis Felipe Dias

All posts

A gitlab terraform pipeline runs cloud changes in order through stages. You set up stages to check, plan, and apply your code changes. Most teams store cloud keys in project settings to start fast. Broad cloud roles let one bad change wipe out live systems. Teams need clear rules for how runners use those cloud keys. How a gitlab terraform pipeline works in practice The workflow starts when a developer pushes code to a branch. A runner picks up the job and loads the tool image. First, the run

Terraform Security (tfsec, Checkov) + DevSecOps Pipeline Design: the complete guide

Architecture patterns, implementation strategies, and security best practices. Delivered to your inbox.

Free white paper. No spam. Unsubscribe anytime.

A gitlab terraform pipeline runs cloud changes in order through stages. You set up stages to check, plan, and apply your code changes. Most teams store cloud keys in project settings to start fast. Broad cloud roles let one bad change wipe out live systems. Teams need clear rules for how runners use those cloud keys.

How a gitlab terraform pipeline works in practice

The workflow starts when a developer pushes code to a branch. A runner picks up the job and loads the tool image. First, the runner links to a shared state backend. GitLab offers a built-in HTTP state backend for this step. The backend locks the state file so two jobs never clash. Next, the runner runs a plan step on the branch. The plan step reads live cloud resources and compares them to code. Many teams post this plan output right on the merge request. Peer reviewers read the plan before they approve the code merge. After approval, the main branch runs the apply step. The runner uses cloud keys or identity tokens to make changes. Teams often use OpenID Connect tokens to trade for cloud roles. This swap avoids long-lived static keys stored inside the project settings.

Manage state and role scope across stages

State files hold sensitive details about every resource you build. They often contain raw database strings, emails, or phone numbers. Anyone who can read the state backend can see these values. You must split state files by stage and by team role. A test branch should never read or lock the live state. Use separate cloud roles for the plan and apply stages. The plan stage only needs read permissions on your cloud account. The apply stage needs write permissions, but only after merge approval. Protected branches in your project help enforce this split. Only approved merges on the main branch can use write roles. Even with protected branches, runners still hold wide cloud permissions during execution.

What native pipeline permissions solve and leave open

Native project settings control which branch can trigger a job. They also control which runner can read a given cloud role. This setup decides who starts the job and which role logs in. Still, native settings do not inspect what commands run during the job. Once a runner assumes an admin role, it has full power. A bad script or module can run destructive commands in flight. It can also query live databases and print card numbers in logs. Native branch rules cannot block a single risky command mid-run. They also cannot mask national IDs or emails in job output. Teams still face a gap between role login and runtime command control.

Terraform Security (tfsec, Checkov) + DevSecOps Pipeline Design: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Govern statements and responses across databases and APIs

Good config and scoped roles are needed setup. They do not check single queries in flight or mask sensitive fields in returned rows. Across Postgres, MySQL, SQL Server, MongoDB, ClickHouse, Kubernetes APIs, SSH, and internal HTTP endpoints, hoop.dev runs beside each supported service as an open-source proxy, a sidecar for your database or API.

First, hoop.dev guardrails block banned queries before they run. Then hoop.dev data masking rewrites matched sensitive values in responses in memory. The audit trail records every statement and verdict per resource. Furthermore, hoop.dev holds SOC 2 Type II and is open source on GitHub.

Frequently asked questions

How do you lock state in a gitlab terraform pipeline?

You can use the built-in GitLab HTTP state backend for locking. It locks the state file during each plan and apply run. This stop prevents two runners from writing state at the same time.

Should plan and apply stages share the same cloud role?

No, you should give each stage its own separate cloud role. Give the plan stage a read-only role for branch checks. Give the apply stage a write role tied to protected branches.

Why are static cloud keys risky in project variables?

Static keys do not expire on their own and can leak in logs. Any job with variable read rights can pull those static keys. Short-lived OpenID Connect tokens lower this risk for every pipeline run.

Save the open-source gateway.

hoop is MIT-licensed infrastructure for controlling how AI agents reach production data. Star hoophq/hoop so you can inspect it, deploy it, or share it when your team starts governing agent access.