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_ssm_activation ValidationException: Nonexistent role or missing ssm service principal in trust policy #27952

Closed
anden-dev opened this issue Nov 22, 2022 · 6 comments · Fixed by #30280
Labels
bug Addresses a defect in current functionality. eventual-consistency Pertains to eventual consistency issues. service/ssm Issues and PRs that pertain to the ssm service.

Comments

@anden-dev
Copy link

Terraform Core Version

1.3.5

AWS Provider Version

4.40.0

Affected Resource(s)

  • aws_ssm_activation

Expected Behavior

Using the example code from https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ssm_activation

resource "aws_iam_role" "test_role" {
  name = "test_role"

  assume_role_policy = <<EOF
  {
    "Version": "2012-10-17",
    "Statement": {
      "Effect": "Allow",
      "Principal": {"Service": "ssm.amazonaws.com"},
      "Action": "sts:AssumeRole"
    }
  }
EOF
}

resource "aws_iam_role_policy_attachment" "test_attach" {
  role       = aws_iam_role.test_role.name
  policy_arn = "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore"
}

resource "aws_ssm_activation" "foo" {
  name               = "test_ssm_activation"
  description        = "Test"
  iam_role           = aws_iam_role.test_role.id
  registration_limit = "5"
  depends_on         = [aws_iam_role_policy_attachment.test_attach]
}

One would expect all resources to be created.

Actual Behavior

  • 1st apply: aws_ssm_activation fails to create with error message regarding iam_role
  • 2nd apply: creates the resources as expected

Relevant Error/Panic Output Snippet

Terraform used the selected providers to generate the following execution
plan. Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # module.ssm_inventory_all.module.activation.aws_iam_role.test_role will be created
  + resource "aws_iam_role" "test_role" {
      + arn                   = (known after apply)
      + assume_role_policy    = jsonencode(
            {
              + Statement = {
                  + Action    = "sts:AssumeRole"
                  + Effect    = "Allow"
                  + Principal = {
                      + Service = "ssm.amazonaws.com"
                    }
                }
              + Version   = "2012-10-17"
            }
        )
      + create_date           = (known after apply)
      + force_detach_policies = false
      + id                    = (known after apply)
      + managed_policy_arns   = (known after apply)
      + max_session_duration  = 3600
      + name                  = "test_role"
      + name_prefix           = (known after apply)
      + path                  = "/"
      + tags_all              = (known after apply)
      + unique_id             = (known after apply)

      + inline_policy {
          + name   = (known after apply)
          + policy = (known after apply)
        }
    }

  # module.ssm_inventory_all.module.activation.aws_iam_role_policy_attachment.test_attach will be created
  + resource "aws_iam_role_policy_attachment" "test_attach" {
      + id         = (known after apply)
      + policy_arn = "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore"
      + role       = "test_role"
    }

  # module.ssm_inventory_all.module.activation.aws_ssm_activation.foo will be created
  + resource "aws_ssm_activation" "foo" {
      + activation_code    = (known after apply)
      + description        = "Test"
      + expiration_date    = (known after apply)
      + expired            = (known after apply)
      + iam_role           = (known after apply)
      + id                 = (known after apply)
      + name               = "test_ssm_activation"
      + registration_count = (known after apply)
      + registration_limit = 5
      + tags_all           = (known after apply)
    }

Plan: 3 to add, 0 to change, 0 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

module.ssm_inventory_all.module.activation.aws_iam_role.test_role: Creating...
module.ssm_inventory_all.module.activation.aws_iam_role.test_role: Creation complete after 1s [id=test_role]
module.ssm_inventory_all.module.activation.aws_iam_role_policy_attachment.test_attach: Creating...
module.ssm_inventory_all.module.activation.aws_iam_role_policy_attachment.test_attach: Creation complete after 1s [id=test_role-20221122154054481200000001]
module.ssm_inventory_all.module.activation.aws_ssm_activation.foo: Creating...
╷
│ Error: Error creating SSM activation: ValidationException: Nonexistent role or missing ssm service principal in trust policy: arn:aws:iam::<AWS_ACCOUNT_NUMBER>:role/test_role
│ 	status code: 400, request id: <REQUEST_ID>
│
│   with module.ssm_inventory_all.module.activation.aws_ssm_activation.foo,
│   on ../../../modules/terraform-component-ssm-inventory/modules/activation/main.tf line 21, in resource "aws_ssm_activation" "foo":
│   21: resource "aws_ssm_activation" "foo" {
│
╵
Releasing state lock. This may take a few moments...
ERRO[0014] 1 error occurred:
	* exit status 1

Terraform Configuration Files

https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ssm_activation

Steps to Reproduce

https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ssm_activation

resource "aws_iam_role" "test_role" {
  name = "test_role"

  assume_role_policy = <<EOF
  {
    "Version": "2012-10-17",
    "Statement": {
      "Effect": "Allow",
      "Principal": {"Service": "ssm.amazonaws.com"},
      "Action": "sts:AssumeRole"
    }
  }
EOF
}

resource "aws_iam_role_policy_attachment" "test_attach" {
  role       = aws_iam_role.test_role.name
  policy_arn = "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore"
}

resource "aws_ssm_activation" "foo" {
  name               = "test_ssm_activation"
  description        = "Test"
  iam_role           = aws_iam_role.test_role.id
  registration_limit = "5"
  depends_on         = [aws_iam_role_policy_attachment.test_attach]
}

Debug Output

No response

Panic Output

No response

Important Factoids

No response

References

No response

Would you like to implement a fix?

No

@anden-dev anden-dev added bug Addresses a defect in current functionality. needs-triage Waiting for first response or review from a maintainer. labels Nov 22, 2022
@github-actions github-actions bot added service/iam Issues and PRs that pertain to the iam service. service/ssm Issues and PRs that pertain to the ssm service. labels Nov 22, 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 Nov 24, 2022
@iMichka
Copy link

iMichka commented Dec 6, 2022

I think

resource "aws_iam_role_policy_attachment" "test_attach" {
  role       = aws_iam_role.test_role.id
  policy_arn = "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore"
}

might fix it. Replace name by id for the role.

Can you confirm that? If yes, the documentation needs an update.

@danishrehman99
Copy link

replacing name with id is not making any difference

@iMichka
Copy link

iMichka commented Dec 19, 2022

I think it's a race condition. Running terraform apply a second time fixes it.

The first time; when aws_ssm_activation is created, the aws_iam_role has probably not yet been created. Is that possible?

@pstagner
Copy link

pstagner commented Jan 4, 2023

I am seeing the role be created and the policy get attached before and I even use a depends_on statement for the role attachment so the role has to exist technically for everything to be true. The "missing ssm service principal in trust policy" seems to be more of the smoking gun however it does create the activation for me on the second apply attempt. I am using version 1.0.6 in the environment I am experiencing this in.

@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 Apr 30, 2023
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. eventual-consistency Pertains to eventual consistency issues. service/ssm Issues and PRs that pertain to the ssm service.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants