Terraform Insecurities: Why Cloud-Native is More Secure

When selecting an Infrastructure-as-Code (IaC) tool, teams often focus on features, syntax preferences, and ecosystem maturity. However, the architectural decisions baked into these tools create fundamentally different security postures — differences that can mean the gap between a secure infrastructure and a catastrophic breach. While Terraform has gained widespread adoption for its multi-cloud capabilities, its client-side execution model introduces security vulnerabilities that CloudFormation's server-side architecture simply doesn't have.

Understanding the Architectural Differences

Before diving into specific vulnerabilities, it's crucial to understand how these tools fundamentally differ in their execution models.

Terraform's Client-Side Model

Terraform operates as a client-side tool that requires direct access to cloud provider APIs. When you run terraform apply, the Terraform binary on your machine (or CI/CD runner) authenticates directly to AWS using credentials you've provided. These credentials must have sufficient permissions to create, modify, and destroy the resources defined in your configuration. Terraform then maintains a state file — a JSON document containing the complete current state of your infrastructure, including resource IDs, dependencies, and often sensitive data like database passwords or API keys.

CloudFormation's Server-Side Execution

CloudFormation operates entirely differently. When you submit a template to CloudFormation, AWS takes over. The service executes within AWS's infrastructure using IAM roles you specify. Your local machine or CI/CD system only needs permission to call CloudFormation APIs (like CreateStack or UpdateStack) — it never needs direct access to create EC2 instances, RDS databases, or any other resources. CloudFormation manages state internally within AWS, never exposing it to users or external systems.

I'd caution anyone against treating this architectural difference as a meaningless implementation detail. Your decision here creates a foundation for one of two entirely different security models.

Security Issue #1: The Credential Externalization Problem

The most significant security vulnerability in Terraform's architecture is the requirement for externalized cloud credentials with broad permissions.

How Terraform Credentials Become Attack Vectors

To execute Terraform, you must provide AWS credentials through one of several mechanisms:

# Environment variables

export AWS_ACCESS_KEY_ID="AKIAIOSFODNN7EXAMPLE"

export AWS_SECRET_ACCESS_KEY="wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY"

# Or in ~/.aws/credentials

[default]

aws_access_key_id = AKIAIOSFODNN7EXAMPLE

aws_secret_access_key = wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY

These credentials must exist somewhere outside AWS — on developer laptops, in CI/CD configuration, or in secrets management systems. Each location represents an attack surface. Consider these real-world scenarios:

CI/CD System Breach: Your Jenkins or GitHub Actions environment stores AWS credentials as secrets. An attacker could exploit a vulnerability in a third-party action or plugin, gaining access to the runner environment. Or, a simple command like echo $AWS_SECRET_ACCESS_KEY | base64 in a workflow step could exfiltrate your credentials — whether characterized as system secrets or not — through logs.

Credential Leakage: Terraform errors sometimes expose environment details. Debug logs, error messages, or verbose output can inadvertently leak credential information, especially in shared CI/CD logs or support tickets.

The fundamental problem is that Terraform requires credentials to exist in the trust boundary of systems you control (systems that can be compromised.

CloudFormation's Credential Isolation

CloudFormation eliminates this entire attack vector through its execution model. Here's a typical CloudFormation deployment:

aws cloudformation create-stack \

--stack-name my-infrastructure \

--template-body file://template.yaml \

--role-arn arn:aws:iam::123456789012:role/CloudFormationExecutionRole

The credentials used here only need permission to call CloudFormation APIs. The actual infrastructure provisioning happens using the CloudFormationExecutionRole, which exists entirely within AWS. Your CI/CD system never possesses credentials capable of creating EC2 instances or RDS databases directly, nor can it delete any of those resources either.

Even better, you can use IAM roles for service accounts (IRSA) in Kubernetes, EC2 instance profiles, or AWS CodeBuild service roles — all of which provide temporary credentials that never exist as static keys. The credentials that actually provision infrastructure never leave AWS's trust boundary.

Security Issue #2: State File Injection Attacks

Terraform's state file represents a second critical vulnerability — one that's less understood but equally dangerous.

The State File: A Security Liability

Terraform's state file is a JSON document that contains the complete representation of your infrastructure:

{

"version": 4,

"terraform_version": "1.5.0",

"resources": [

{

"type": "aws_db_instance",

"name": "production",

"instances": [

{

"attributes": {

"password": "MyS3cr3tP@ssw0rd",

"endpoint": "prod-db.abc123.us-east-1.rds.amazonaws.com:5432",

"username": "admin"

}

}

]

}

]

}

This file contains sensitive data: database passwords, API keys, private endpoints, and the complete topology of your infrastructure. Even with remote state storage in S3 with encryption, the state file must be accessible to Terraform during execution — meaning it's accessible to anyone with the right S3 permissions or anyone who compromises a system running Terraform.

The Injection Attack Vector

State file manipulation enables sophisticated attacks. An attacker who gains access to your state storage can:

Resource Deletion: Remove critical resources from state. Terraform will attempt to recreate them, potentially causing outages or data loss if the resources still exist in AWS. Alternatively, injecting existing resources into a state file that don't exist in the template will trigger Terraform to delete those resources from the environment.

Privilege Escalation: Modify state to change resource configurations. For example, altering a security group's state to show it allows traffic from 0.0.0.0/0, then running terraform apply with --refresh=false to skip the refresh phase.

The Ongoing State Management Burden

Even without malicious actors, Terraform state management requires constant vigilance:

  • Configuring and maintaining remote state backends with proper encryption
  • Implementing state locking to prevent concurrent modifications
  • Managing state file access controls across teams
  • Handling state file corruption or conflicts
  • Backing up state files and planning recovery procedures
  • Auditing state file access and modifications

This is an unending operational burden. Every new team member needs state access. Every new CI/CD pipeline needs state configuration. Every compliance audit must verify state file security controls.

CloudFormation's State Immunity

CloudFormation doesn't expose state files to users. Period. The service maintains state internally, and you interact with it only through AWS APIs:

# Check stack status

aws cloudformation describe-stacks --stack-name my-infrastructure

# Detect configuration drift

aws cloudformation detect-stack-drift --stack-name my-infrastructure

There's no state file to secure, no state backend to configure, no state locking to implement. The attack vector simply doesn't exist. An attacker would need to compromise AWS's internal systems — a far higher bar than compromising your S3 bucket or CI/CD environment.

CloudFormation's drift detection provides visibility into infrastructure changes without exposing the underlying state representation. You get the operational benefits without the security liability.

When Architecture Matters More Than Features

Terraform advocates often point to features like multi-cloud support or a larger provider ecosystem. These are valid considerations, but they don't address the fundamental security architecture differences outlined above.

Security vulnerabilities in IaC tools are particularly dangerous because they operate at the infrastructure layer — a breach here compromises everything built on top. A compromised Terraform credential or state file can expose your entire AWS environment.

For organizations operating exclusively or primarily in AWS, CloudFormation (and its developer-friendly abstraction, AWS CDK) provides a security model that eliminates entire classes of vulnerabilities. When you add these types of security controls, you remove attack vectors entirely.

Making the Secure Choice

If your organization is evaluating IaC tools or considering migrating from Terraform, the security implications should weigh heavily in your decision. When we teach people to get the benefits of IaC, security is one of the first things we talk about. CloudFormation's architecture provides:

  • No externalized credentials for infrastructure provisioning
  • No exposed state files to secure and manage
  • Native AWS integration with CloudTrail, IAM, and AWS security services
  • Reduced operational burden for security teams
  • Simplified compliance posture

For teams already invested in Terraform, migration is admittedly complex. However, the security benefits of CloudFormation's architecture compound over time, while Terraform's security burden only grows with scale.

This is where Sketch Development Services can help. With specialized CloudFormation and AWS CDK certification, Sketch Development Services evaluates your existing infrastructure, identifies security risks in your current IaC approach, and builds secure, maintainable CDK-based infrastructure that leverages CloudFormation's architectural advantages. Whether you're starting fresh or migrating from Terraform, expert guidance ensures you're you're deploying secure infrastructure.

IaC Security

Infrastructure-as-Code has transformed how we build and manage cloud resources, but not all IaC tools are created equal from a security perspective. Terraform's client-side execution model and exposed state files create inherent vulnerabilities that require constant vigilance and operational overhead. CloudFormation's server-side execution within AWS eliminates these attack vectors entirely.

The best cloud security starts with choosing architectures that minimize risk from the ground up. For AWS infrastructure, CloudFormation provides that foundation. The question isn't whether you can secure Terraform; it's whether you should accept those security trade-offs when a more secure alternative exists.

Choose your infrastructure tools based on your security requirements, instead of just feature checklists. Your future security team will thank you.

 

Additional Resources

AWS CloudFormation Security

Terraform Security Vulnerabilities and Concerns



Ryan Jensen

Ryan spent 10 years working at a prepaid card company, developing ordering and card balance platforms. At Sketch, he provides critical software development for our clients, and leads our managed service for cloud infrastructures. His many other hats include coaching, training (DevOps is his thing...among others), and...

Other posts you might be interested in