Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: aws_organizations_policy_attachment throws an error when a managed policy is deleted but still in state #27231

Closed
devonbleak opened this issue Oct 13, 2022 · 3 comments · Fixed by #27238
Labels
bug Addresses a defect in current functionality. service/organizations Issues and PRs that pertain to the organizations service.
Milestone

Comments

@devonbleak
Copy link
Contributor

devonbleak commented Oct 13, 2022

Terraform Core Version

0.13.5

AWS Provider Version

4.34.0

Affected Resource(s)

  • aws_organizations_policy_attachment

Expected Behavior

The missing policy should be detected and TF should attempt to re-create

Actual Behavior

Error: PolicyNotFoundException: You specified a policy that doesn't exist.

Relevant Error/Panic Output Snippet

No response

Terraform Configuration Files

resource "aws_organizations_organizational_unit" "main_ou" {
  name      = "main-ou"
  parent_id = aws_organizations_organization.org.roots[0].id
}

# SCP to lock out all actions for root user
data "aws_iam_policy_document" "deny_root_access" {
  statement {
    effect = "Deny"
    not_actions = [
      "iam:CreateVirtualMFADevice",
      "iam:EnableMFADevice",
      "iam:Get*",
      "iam:List*"
    ]
    resources = ["*"]

    condition {
      test     = "StringLike"
      variable = "aws:PrincipalArn"
      values   = ["arn:aws:iam::*:root"]
    }
  }
}

resource "aws_organizations_policy" "deny_root_access_policy" {
  name    = "deny-root-access"
  content = data.aws_iam_policy_document.deny_root_access.json
}

resource "aws_organizations_policy_attachment" "scp_deny_root_attach" {
  policy_id = aws_organizations_policy.deny_root_access_policy.id
  target_id = aws_organizations_organizational_unit.main_ou.id
}

Steps to Reproduce

  1. Create an aws_organizations_policy using Terraform
  2. Manually detach and delete the policy outside of Terraform
  3. Run a plan

Debug Output

2022-10-13T09:49:37.299-0700 [DEBUG] plugin.terraform-provider-aws_v4.34.0_x5: [DEBUG] [aws-sdk-go] DEBUG: Response organizations/ListTargetsForPolicy Details:
2022-10-13T09:49:37.299-0700 [DEBUG] plugin.terraform-provider-aws_v4.34.0_x5: ---[ RESPONSE ]--------------------------------------
2022-10-13T09:49:37.299-0700 [DEBUG] plugin.terraform-provider-aws_v4.34.0_x5: HTTP/1.1 400 Bad Request
2022-10-13T09:49:37.299-0700 [DEBUG] plugin.terraform-provider-aws_v4.34.0_x5: Connection: close
2022-10-13T09:49:37.299-0700 [DEBUG] plugin.terraform-provider-aws_v4.34.0_x5: Content-Length: 91
2022-10-13T09:49:37.299-0700 [DEBUG] plugin.terraform-provider-aws_v4.34.0_x5: Content-Type: application/x-amz-json-1.1
2022-10-13T09:49:37.299-0700 [DEBUG] plugin.terraform-provider-aws_v4.34.0_x5: Date: Thu, 13 Oct 2022 16:49:37 GMT
2022-10-13T09:49:37.299-0700 [DEBUG] plugin.terraform-provider-aws_v4.34.0_x5: X-Amzn-Requestid: 8fc48a08-607e-4401-947e-5c262501862d
2022-10-13T09:49:37.299-0700 [DEBUG] plugin.terraform-provider-aws_v4.34.0_x5:
2022-10-13T09:49:37.299-0700 [DEBUG] plugin.terraform-provider-aws_v4.34.0_x5:
2022-10-13T09:49:37.299-0700 [DEBUG] plugin.terraform-provider-aws_v4.34.0_x5: -----------------------------------------------------
2022-10-13T09:49:37.299-0700 [DEBUG] plugin.terraform-provider-aws_v4.34.0_x5: [DEBUG] [aws-sdk-go] {"__type":"PolicyNotFoundException","Message":"You specified a policy that doesn't exist."}
2022-10-13T09:49:37.299-0700 [DEBUG] plugin.terraform-provider-aws_v4.34.0_x5: [DEBUG] [aws-sdk-go] DEBUG: Validate Response organizations/ListTargetsForPolicy failed, attempt 0/25, error PolicyNotFoundException: You specified a policy that doesn't exist.
2022-10-13T09:49:37.299-0700 [TRACE] plugin.terraform-provider-aws_v4.34.0_x5: Called downstream: @caller=github.com/hashicorp/terraform-plugin-sdk/[email protected]/helper/schema/resource.go:1016 @module=sdk.helper_schema tf_mux_provider=*schema.GRPCProviderServer tf_provider_addr=registry.terraform.io/hashicorp/aws tf_req_id=2c6bad52-8f21-9378-a413-c7c169513656 tf_resource_type=aws_organizations_policy_attachment tf_rpc=ReadResource timestamp=2022-10-13T09:49:37.299-0700
2022-10-13T09:49:37.299-0700 [TRACE] plugin.terraform-provider-aws_v4.34.0_x5: Received downstream response: tf_provider_addr=registry.terraform.io/hashicorp/aws tf_rpc=ReadResource @caller=github.com/hashicorp/[email protected]/tfprotov5/internal/tf5serverlogging/downstream_request.go:37 tf_proto_version=5.3 tf_resource_type=aws_organizations_policy_attachment @module=sdk.proto diagnostic_error_count=1 diagnostic_warning_count=0 tf_req_duration_ms=206 tf_req_id=2c6bad52-8f21-9378-a413-c7c169513656 timestamp=2022-10-13T09:49:37.299-0700
2022-10-13T09:49:37.300-0700 [ERROR] plugin.terraform-provider-aws_v4.34.0_x5: Response contains error diagnostic: @module=sdk.proto diagnostic_detail= diagnostic_summary="PolicyNotFoundException: You specified a policy that doesn't exist." tf_proto_version=5.3 tf_provider_addr=registry.terraform.io/hashicorp/aws tf_req_id=2c6bad52-8f21-9378-a413-c7c169513656 @caller=github.com/hashicorp/[email protected]/tfprotov5/internal/diag/diagnostics.go:55 diagnostic_severity=ERROR tf_resource_type=aws_organizations_policy_attachment tf_rpc=ReadResource timestamp=2022-10-13T09:49:37.299-0700
2022-10-13T09:49:37.300-0700 [TRACE] plugin.terraform-provider-aws_v4.34.0_x5: Served request: @caller=github.com/hashicorp/[email protected]/tfprotov5/tf5server/server.go:761 tf_req_id=2c6bad52-8f21-9378-a413-c7c169513656 tf_resource_type=aws_organizations_policy_attachment tf_rpc=ReadResource @module=sdk.proto tf_proto_version=5.3 tf_provider_addr=registry.terraform.io/hashicorp/aws timestamp=2022-10-13T09:49:37.299-0700
2022/10/13 09:49:37 [ERROR] eval: *terraform.EvalRefresh, err: PolicyNotFoundException: You specified a policy that doesn't exist.
2022/10/13 09:49:37 [ERROR] eval: *terraform.EvalSequence, err: PolicyNotFoundException: You specified a policy that doesn't exist.

Panic Output

No response

Important Factoids

No response

References

No response

Would you like to implement a fix?

Yes

@devonbleak devonbleak added bug Addresses a defect in current functionality. needs-triage Waiting for first response or review from a maintainer. labels Oct 13, 2022
@github-actions github-actions bot added service/iam Issues and PRs that pertain to the iam service. service/organizations Issues and PRs that pertain to the organizations service. labels Oct 13, 2022
@github-actions
Copy link

Community Note

Voting for Prioritization

  • Please vote on this issue by adding a 👍 reaction to the original post to help the community and maintainers prioritize this request.
  • Please see our prioritization guide for information on how we prioritize.
  • Please do not leave "+1" or other comments that do not add relevant new information or questions, they generate extra noise for issue followers and do not help prioritize the request.

Volunteering to Work on This Issue

  • If you are interested in working on this issue, please leave a comment.
  • If this would be your first contribution, please review the contribution guide.

@breathingdust breathingdust removed the needs-triage Waiting for first response or review from a maintainer. label Oct 13, 2022
@devonbleak devonbleak changed the title [Bug]: aws_organizations_policy throws an error when a managed policy is deleted but still in state [Bug]: aws_organizations_policy_attachment throws an error when a managed policy is deleted but still in state Oct 13, 2022
@ewbankkit ewbankkit removed the service/iam Issues and PRs that pertain to the iam service. label Oct 16, 2022
@github-actions github-actions bot added this to the v4.35.0 milestone Oct 17, 2022
@github-actions
Copy link

This functionality has been released in v4.35.0 of the Terraform AWS Provider. Please see the Terraform documentation on provider versioning or reach out if you need any assistance upgrading.

For further feature requests or bug reports with this functionality, please create a new GitHub issue following the template. Thank you!

@github-actions
Copy link

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 17, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Addresses a defect in current functionality. service/organizations Issues and PRs that pertain to the organizations service.
Projects
None yet
3 participants