The Simplest Way to Make Active Directory and Terraform Work Like They Should

Published
Length
3 min read

Luis Felipe Dias

All posts

Pairing active directory and terraform lets teams manage users, groups, and cloud servers with code. This setup turns manual directory edits into repeatable plans that run through your pipeline. Teams often create directory groups by hand when they spin up new cloud resources. Manual steps lead to mistakes, slow approvals, and leftover permissions after a project ends. Linking both tools in one workflow removes that manual work. When a team builds a new database, the same plan creates the direc

Your coding agents run with your access.

Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.

Pairing active directory and terraform lets teams manage users, groups, and cloud servers with code. This setup turns manual directory edits into repeatable plans that run through your pipeline.

Teams often create directory groups by hand when they spin up new cloud resources. Manual steps lead to mistakes, slow approvals, and leftover permissions after a project ends. Linking both tools in one workflow removes that manual work. When a team builds a new database, the same plan creates the directory group. When the team deletes the database, the plan removes the group right away.

How active directory and terraform share identity data

Your automation tool needs a way to log in to your directory server. It uses a dedicated service account or an identity token from your cloud host. Once it logs in, the tool reads the current state of your directory objects. It checks which groups, users, and organizational units already exist on the server.

Next, the tool compares that live directory state against your written code files. If a group is missing, the tool sends a write request to the directory. It can also read existing directory groups and pass their IDs to cloud roles. This data flow keeps your directory and your cloud permissions in sync every day.

For on-premises directories, the runner talks to a domain controller over a secure network. For cloud directories, the runner talks to the vendor API using a scoped app role. Both setups record the object IDs inside a central state file for future runs.

Common failure modes and how to scope roles

Many teams give their automation runner full domain admin rights just to save time. This is a huge risk for your whole company. If an attacker steals that shared key, they own the entire directory and network. Shared service accounts also make it hard to see who triggered a bad change.

Unlogged directory queries and broad tokens create another major blind spot for security teams. When every pipeline uses the same broad account, domain logs show only one name. You cannot tell which engineer or branch deleted a vital security group.

Active Directory + Terraform Security (tfsec, Checkov): architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

To fix this, scope the runner account to one specific organizational unit. Grant it rights to create and edit groups only inside that single folder. Never let the automation account touch top-level admin groups or domain controller settings. Place the runner on a private subnet with strict firewall rules to the directory.

Keeping state files and directory secrets safe

Your state file holds a full map of every group and user you manage. Sometimes it even stores raw passwords or sensitive user attributes in plain text. If you keep that state file on a laptop, anyone with file rights can read it.

Always store your state file in a remote backend with encryption turned on. Limit read and write rights on that state storage to the pipeline runner alone. Engineers should propose changes through pull requests instead of running plans from their laptops.

Rotate the directory service account key on a regular schedule. Better yet, use short-lived workload tokens so no static password sits in your pipeline settings. Log every directory write event and send those logs to your central security tool. Set up alerts for any group changes that happen outside your main pipeline.

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

Should we manage human user accounts in code?

Most teams avoid managing individual human users in code because staff changes happen daily. Instead, let your HR system create users and use code to manage groups and roles.

How do we import existing directory groups into our state file?

You can write a resource block for the group and run an import command. This links the live directory object to your state file without recreating the group.

What happens if someone edits a managed group by hand?

On the next run, the tool spots the difference between the server and your code. It will overwrite the manual change so the directory matches your code file again.

The query runs. The emails stay hidden.