For AI agents: A markdown version of this page is available at https://docs.datadoghq.com/security/code_security/iac_security/iac_rules/terraform-aws-no-stack-policy.md. A documentation index is available at /llms.txt.
This product is not supported for your selected Datadog site. ().

Metadata

Id: terraform-aws-no-stack-policy

Provider: AWS

Platform: Terraform

Severity: Medium

Category: Resource Management

Learn More

Description

AWS CloudFormation stacks should implement a stack policy, set using the policy_body or policy_url attributes in Terraform, to restrict which actions can modify or delete critical resources within the stack. Without a stack policy, any update operation could unintentionally overwrite, disrupt, or remove essential resources, increasing the risk of accidental outages or security issues. Properly configuring a stack policy helps enforce change management controls and reduces the attack surface for unauthorized or accidental actions.

Compliant Code Examples

resource "aws_cloudformation_stack" "negative1" {

     name = "networking-stack"

     parameters = {
     VPCCidr = "10.0.0.0/16"
     }

     policy_url = "somepolicyurl"
}



resource "aws_cloudformation_stack" "negative2" {

     name = "networking-stack"

     parameters = {
     VPCCidr = "10.0.0.0/16"
     }

     policy_body = "somepolicy"
}
resource "aws_s3_bucket" "policy_bucket" {
  bucket = "my-cf-policy-bucket"
}

resource "aws_s3_object" "policy" {
  bucket  = aws_s3_bucket.policy_bucket.id
  key     = "stack-policy.json"
  content = "{\"Statement\":[{\"Effect\":\"Deny\",\"Action\":\"Update:*\",\"Principal\":\"*\",\"Resource\":\"*\"}]}"
}

resource "aws_cloudformation_stack" "negative3" {
  name = "networking-stack"

  parameters = {
    VPCCidr = "10.0.0.0/16"
  }

  policy_url = "https://${aws_s3_bucket.policy_bucket.bucket_regional_domain_name}/${aws_s3_object.policy.key}"
}

Non-Compliant Code Examples

resource "aws_cloudformation_stack" "positive1" {

  name = "networking-stack"

  parameters = {
    VPCCidr = "10.0.0.0/16"
  }

}