Terraform-provider-aws: Secrets manager policy validation fails for principals that are just created

Created on 3 Aug 2020  ·  4Comments  ·  Source: hashicorp/terraform-provider-aws

Community Note

  • Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request
  • 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
  • If you are interested in working on this issue or have submitted a pull request, please leave a comment

Terraform CLI and Terraform AWS Provider Version


Terraform v0.12.16

  • provider.archive v1.3.0
  • provider.aws v3.0.0
  • provider.null v2.1.2

Affected Resource(s)

  • aws_iam_role
  • aws_secretsmanager_secret

Terraform Configuration Files

resource "aws_iam_role" "this" {
  name                 = join("-", ["instance-profile", "test"])
  assume_role_policy   = <<EOF
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Action": "sts:AssumeRole",
      "Principal": {
        "Service": "ec2.amazonaws.com"
      },
      "Effect": "Allow",
      "Sid": ""
    }
  ]
}
EOF
}

data "aws_iam_policy_document" "this" {
  statement { 
    effect    = "Allow"
    resources = ["*"]
    actions   = ["secretsmanager:Get*"]
    principals {
      type        = "AWS"
      identifiers = [aws_iam_role.this.arn]
    }
  }
}
resource "aws_secretsmanager_secret" "this" {
  name                    = "test"
  description             = "Created by test"
  recovery_window_in_days = 0
  policy                  = data.aws_iam_policy_document.this.json
}

Debug Output

Panic Output

Expected Behavior

Secret is generated with the appropriate permission at the first apply

Actual Behavior

The validation for the policy fails with the following output
data.aws_caller_identity.current: Refreshing state...
aws_iam_role.this: Creating...
aws_iam_role.this: Creation complete after 2s [id=instance-profile-test]
data.aws_iam_policy_document.this: Refreshing state...
aws_secretsmanager_secret.this: Creating...
Error: error setting Secrets Manager Secret "arn:aws:secretsmanager:ap-southeast-2:XXXXXXX: secret:test-e5XYyU" policy: MalformedPolicyDocumentException: This resource policy contains an unsupported principal.
on main.tf line 6, in resource "aws_secretsmanager_secret" "this":
6: resource "aws_secretsmanager_secret" "this" {

NOTE: a subsequent apply works as expected. I suspect the validation of the policy happens before the IAM role arn is actually available for querying due to IAM eventual consistency

Steps to Reproduce

  1. Ensure is the first execution or run terraform destroy
  2. terraform apply --> Generate an error
  3. terraform apply --> Successful execution
bug serviciam servicsecretsmanager

All 4 comments

The fix for this has been merged and will release with version 3.1.0 of the Terraform AWS Provider, likely later today. Thanks to @DrFaust92 for the implementation. 👍

@DrFaust92 thanks for the prompt implementation :)

This has been released in version 3.1.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 for triage. Thanks!

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 feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. Thanks!

Was this page helpful?
0 / 5 - 0 ratings