The Simplest Way to Make IAM Roles and OpenTofu Work Like They Should

Published
Length
3 min read

Luis Felipe Dias

All posts

Teams pair iam roles and opentofu to build cloud systems without long-lived keys. OpenTofu assumes a cloud role to read state and change resources safely. This workflow keeps static secrets off engineer laptops and build servers. Each run gets temporary permissions that expire on a short timer. Teams can track who started a deployment and what resources changed. How iam roles and opentofu handle logins and permissions Cloud platforms use roles to group permissions for people and apps. A role

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.

Teams pair iam roles and opentofu to build cloud systems without long-lived keys. OpenTofu assumes a cloud role to read state and change resources safely.

This workflow keeps static secrets off engineer laptops and build servers. Each run gets temporary permissions that expire on a short timer. Teams can track who started a deployment and what resources changed.

How iam roles and opentofu handle logins and permissions

Cloud platforms use roles to group permissions for people and apps. A role defines which cloud services a caller can read or write. It also has a trust rule that lists who can use it.

OpenTofu reads your template files and compares them to the remote state file. Before it checks any cloud resource, it must log in to the cloud. The provider plugin asks the cloud token service to assume a set role. The token service checks the caller identity against the role trust rule. If the check passes, the cloud sends back temporary credentials for that run.

Data flows between the tool and the cloud API over encrypted links. First, the tool uses a state role to lock and read state storage. Next, it assumes a target role for the account it needs to change. During a plan step, it only needs read access to check current settings. During an apply step, it uses write permissions to create or update resources. When the run ends, the tool releases the lock and drops the credentials.

Common failure modes and how to scope roles

Many teams start by giving one shared service account full admin rights. They put long-lived access keys into build pipelines and local machines. If an attacker steals that key, they control the entire cloud account. Shared accounts also ruin audit logs because every action looks the same.

Broad tokens cause another major problem during routine code reviews. A pull request normally only runs a plan to preview changes. Still, a plan step can still run external scripts or read secret stores. If the plan role has write rights, a bad branch can change production. Unlogged queries to state files can also expose sensitive outputs like phone numbers.

AWS IAM Policies + Lambda Execution Roles: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

You can fix these risks by splitting roles by task and stage. Create one read-only role for plan runs on open pull requests. Create a second role with write permissions that only the main branch assumes. Scope each role to the exact services your templates manage. Do not grant full account admin rights when a team only builds queues.

Separate your state storage permissions from your resource permissions as well. The identity that reads the state bucket should not alter network paths. Lock down network paths so only trusted build runners can reach private endpoints. Set short session limits on every role so tokens expire right after runs. Check cloud audit logs often to remove unused permissions from your roles.

Managing local runs and multi-account setups

Engineers often need to test templates from their laptops during an outage. Storing raw cloud keys in local text files creates a major risk. Instead, engineers should log in through single sign-on to assume a test role. That role should only touch sandbox accounts and never production state files.

Large companies often split cloud workloads across dozens of separate accounts. A central build runner can use one base identity to start the job. It then assumes a distinct target role inside each child account. This setup keeps account walls strong and limits the blast radius of mistakes. If one child role fails, the other accounts stay safe and untouched.

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

Why should we separate plan and apply roles for OpenTofu?

Untrusted code in a pull request can trigger a plan run. Using a read-only role stops bad code from deleting live cloud resources. Only merged code on the main branch should use the write role.

How do we stop state files from leaking sensitive data?

Store state in encrypted remote storage with strict role rules. Limit read permissions on the state storage to the build runner role. Mask outputs like emails or card numbers so logs stay safe.

What is the safest way for pipelines to assume cloud roles?

Use OpenID Connect so your pipeline trades a job token for cloud credentials. The cloud checks the repo and branch name before it grants the role. This approach removes stored keys from your build settings.

The query runs. The emails stay hidden.