The Simplest Way to Make Ec2 Systems Manager and OpenTofu Work Like They Should

Published
Length
3 min read

Luis Felipe Dias

All posts

Teams use ec2 systems manager and opentofu together to build servers and run tasks. OpenTofu sets up the cloud parts, while Systems Manager runs scripts and holds secrets. This pairing lets your team manage server fleets through code. You do not need to open public network ports on your virtual machines. Instead, OpenTofu writes the cloud rules and machine profiles. Then Systems Manager talks to the agent on each server to apply updates. How ec2 systems manager and opentofu share roles and da

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 use ec2 systems manager and opentofu together to build servers and run tasks. OpenTofu sets up the cloud parts, while Systems Manager runs scripts and holds secrets.

This pairing lets your team manage server fleets through code. You do not need to open public network ports on your virtual machines. Instead, OpenTofu writes the cloud rules and machine profiles. Then Systems Manager talks to the agent on each server to apply updates.

How ec2 systems manager and opentofu share roles and data

Both tools rely on AWS Identity and Access Management to check who is calling. OpenTofu uses an execution role when it runs in your build pipeline. That role gives it the right to create servers, roles, and stored parameters.

During a run, OpenTofu can read values from Systems Manager Parameter Store. It uses these values to fill in settings for new cloud resources. OpenTofu can also write new outputs, like database endpoints, back into Parameter Store. Other apps can then read those stored values when they start up.

For server control, OpenTofu attaches an instance profile to every new virtual machine. This profile holds a role that lets the local agent call the Systems Manager API. Once the machine boots, the agent checks in over an outbound HTTPS channel. Your team can then run commands or open shell sessions without managing SSH keys.

OpenTofu also sets up maintenance rules and command documents in code. When you merge a change, OpenTofu updates those documents in your AWS account. Systems Manager then runs the updated tasks on the matching servers by tag.

Common failure modes and how to scope roles

Many teams fail by giving the OpenTofu pipeline role full admin rights. A leaked pipeline token can then wipe out servers or read every secret. Another common mistake is sharing one broad instance profile across all servers. If one web server breaks, the attacker can read parameters meant for billing apps.

GCP Access Context Manager + End-to-End Encryption: architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Unlogged actions also cause big problems during audits. If engineers share one IAM role to start shell sessions, logs show only the role name. You cannot tell which person ran a shell command on a production box. Secrets can also leak into plain text within your OpenTofu state file. Anyone who can read the state bucket can then see those secret values.

To fix this, split your IAM roles by app and by stage. Give the OpenTofu runner permission to change only the resources in its own stack. Scope each server profile to read only a single path in Parameter Store. Use KMS keys to lock sensitive parameters, and restrict who can decrypt them.

Keep your OpenTofu state file in an encrypted bucket with strict read rules. Never pass raw passwords through OpenTofu if the resource does not need them. Instead, let OpenTofu create the empty parameter slot and the KMS key policy. Have your app or server agent pull the secret value at runtime.

How to keep server tasks and state changes safe

Good network rules make this tool pair much safer in production. Place your servers in private subnets with no public IP addresses. Set up VPC endpoints so the local agent talks to Systems Manager over private links. This keeps all management traffic inside the cloud network.

Turn on logging for both your pipeline runs and your server sessions. Send Systems Manager command output and session logs to a locked log group. Set up CloudTrail to record every read and write call to Parameter Store. Review these logs often to spot odd reads or unexpected server changes.

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

Should OpenTofu write secret values into Systems Manager Parameter Store?

No, because OpenTofu stores every value it touches in its state file in plain text. Let OpenTofu set up the access rules, and set the secret value outside the state.

How do servers connect to Systems Manager when OpenTofu puts them in private subnets?

OpenTofu must create VPC interface endpoints for the Systems Manager service in that network. The server agent then uses those private endpoints to check in without internet access.

How can we stop servers from reading each other's parameters?

Use path prefixes in Parameter Store and create separate IAM roles in OpenTofu. Give each server role read rights only to the path prefix for its own app.

The query runs. The emails stay hidden.