The Simplest Way to Make Azure Service Bus and OpenTofu Work Like They Should

Published
Length
3 min read

Luis Felipe Dias

All posts

Using azure service bus and opentofu lets teams build message queues from plain text files. OpenTofu reads your plans and calls Azure APIs to create namespaces, queues, and topics. This workflow stops manual clicks in the cloud portal. Every queue rule lives in version control so your team can review each change. How azure service bus and opentofu share roles and data OpenTofu needs an identity in Azure before it can build or change any queues. Most teams create a service principal or use a

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.

Using azure service bus and opentofu lets teams build message queues from plain text files. OpenTofu reads your plans and calls Azure APIs to create namespaces, queues, and topics.

This workflow stops manual clicks in the cloud portal. Every queue rule lives in version control so your team can review each change.

How azure service bus and opentofu share roles and data

OpenTofu needs an identity in Azure before it can build or change any queues. Most teams create a service principal or use a managed identity for the build runner.

During a run, OpenTofu logs in to Azure Active Directory with that chosen identity. Azure checks the role assigned to that identity at the resource group or subscription scope.

If the role allows edits, OpenTofu sends requests to the Azure Resource Manager API. That API creates the Service Bus namespace, sets up queues, and writes topic filter rules.

Azure then sends the new resource details and shared keys back to the OpenTofu runner. OpenTofu writes all of those details into a state file so it remembers what exists.

You should store this state file in a remote Azure storage account with a lock. State locks stop two people from running changes at the exact same time.

Your apps do not talk to OpenTofu when they send or read messages. Instead, your apps use Azure identities or queue keys that OpenTofu set up earlier.

Common failure modes and how to scope roles

Many teams make the mistake of giving OpenTofu a broad owner role across Azure. If that runner key leaks, an attacker can delete databases or read secret queue keys.

A second big problem comes from how Azure Service Bus handles shared access policies. When OpenTofu creates a queue policy, it pulls the primary key into the state file.

Anyone who can read the state file can also read that plain text key. With that key, a person can read or delete messages in your production queues.

Service-to-Service Authentication + Azure RBAC: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Teams also run into trouble when engineers run OpenTofu from their own laptops. Shared service accounts make it hard to see which person changed a topic rule.

To fix this, give the OpenTofu runner a role scoped to one resource group. Only grant the exact permissions needed to manage Service Bus namespaces and queues.

Stop creating shared access keys for your apps inside your OpenTofu plans. Instead, use OpenTofu to assign Azure role-based permissions to each app identity.

Give sending apps the sender role and reading apps the receiver role on one queue. This setup keeps raw connection strings out of your OpenTofu state files.

You should also lock down network paths between your runners and your cloud resources. Put your Service Bus namespace on a private network endpoint when your tier allows it.

Run your OpenTofu jobs from a private build agent inside that same virtual network. Turn on Azure activity logs and Service Bus diagnostic logs for every single namespace.

Managing state and drift across team workflows

Queue settings often drift when engineers fix an outage by clicking in the Azure portal. Someone might raise a message time-to-live value or add a topic subscription by hand.

On the next run, OpenTofu sees the drift and tries to undo that manual fix. That sudden rollback can drop live messages or break an app that needs the topic.

You can stop drift by giving engineers read-only roles in the Azure portal. Force every queue and topic change to go through your version control review flow.

Split your OpenTofu files so base namespaces live apart from fast-changing app queues. This split keeps a small queue edit from touching the main message bus.

Add runtime guardrails and masking beside your databases and APIs

Login rules, role grants, and CI checks govern who connects to your stack. Yet any allowed script, engineer, or AI agent still runs commands with the full scope of its role. For workloads on Postgres, MySQL, SQL Server, MongoDB, ClickHouse, Kubernetes, SSH, or internal HTTP services, hoop.dev sits beside the service as an open-source proxy, a sidecar for your database or API.

Before a query runs, hoop.dev guardrails check the statement against your rules. An AI Analyzer you configure can score intent or hold risky calls for human review. Next, hoop.dev data masking rewrites sensitive fields in responses, and each verdict is logged per listener. Explore the MIT-licensed project on GitHub.

Frequently asked questions

Does OpenTofu store Azure Service Bus keys in plain text?

Yes, OpenTofu writes any shared access keys it creates into the state file. Use Azure role assignments for your apps and encrypt your remote state storage.

How do we stop OpenTofu from deleting live queues by mistake?

Add a lifecycle rule in your OpenTofu plan that blocks destroy actions on queues. You can also place an Azure delete lock on the Service Bus namespace.

Should apps use the same Azure identity as OpenTofu?

No, OpenTofu needs management permissions to create and change queues via the Azure API. Your apps only need data plane permissions to send or receive messages.

The query runs. The emails stay hidden.