hashicorp / terraform-provider-aws

The AWS Provider enables Terraform to manage AWS resources.
https://registry.terraform.io/providers/hashicorp/aws
Mozilla Public License 2.0
9.8k stars 9.15k forks source link

[Bug]: `r/aws_iam_policy`: Malformed `Condition` prevents subsequent modifications #39833

Open jar-b opened 1 hour ago

jar-b commented 1 hour ago

Terraform Core Version

1.9.5

AWS Provider Version

5.72.1

Affected Resource(s)

Expected Behavior

When malformed policy content is configured, the first apply should fail. Reverting of fixing the content should result in a successful apply.

Actual Behavior

When malformed policy content is configured, the first apply fails (expected).

│ Error: updating IAM Policy (arn:aws:iam::727561393803:policy/policy_zd163007): operation error IAM: CreatePolicyVersion, https response error StatusCode: 400, RequestID: 273b745a-e51
c-49b7-bd12-1f08ee676a58, MalformedPolicyDocument: Syntax errors in policy

Then, all subsequent attempts to resolve the syntax error fail because the invalid JSON has been stored in state and cannot be parsed when checking equivalence with the remote policy content.

│ Error: while setting policy (), encountered: while checking equivalency of existing policy ({"Statement":[{"Action":["ec2:Describe*"],"Condition":{"ForAnyValue:StringLike":["aws:Mult
iFactorAuthAge"]},"Effect":"Allow","Resource":"*"}],"Version":"2012-10-17"}) and new policy ({"Statement":[{"Action":["ec2:Describe*"],"Effect":"Allow","Resource":"*"}],"Version":"2012
-10-17"}), encountered: parsing policy 1: parsing statement 1: 1 error(s) decoding:
│
│ * '[0].Condition[ForAnyValue:StringLike]' expected a map, got 'slice'

Relevant Error/Panic Output Snippet

No response

Terraform Configuration Files

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 5.0"
    }
  }
}

# Configure the AWS Provider
provider "aws" {}

resource "aws_iam_policy" "policy" {
  name        = "policy_zd163007"
  path        = "/"
  description = "My test policy"

  policy = jsonencode({
    Version = "2012-10-17"
    Statement = [
      {
        Action = [
          "ec2:Describe*",
        ]
        # After successfully creating the policy, uncomment this malformed
        # condition and apply again, causing the unrecoverable state.
        # "Condition" : {
        #   "ForAnyValue:StringLike" : ["aws:MultiFactorAuthAge"]
        # }

        Effect   = "Allow"
        Resource = "*"
      },
    ]
  })
}

Steps to Reproduce

  1. Apply the configuration above.
  2. Uncomment the malformed Condition, apply. This should fail as expected.
  3. Comment out the problematic Condition and run plan or apply. Observe failure while setting the policy argument.

Debug Output

No response

Panic Output

No response

Important Factoids

No response

References

Relates #39202

Would you like to implement a fix?

None

github-actions[bot] commented 1 hour ago

Community Note

Voting for Prioritization

Volunteering to Work on This Issue