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

Failure to detect drift in aws_iam_instance_profile resource #7160

Closed
ghost opened this issue Jan 16, 2019 · 2 comments · Fixed by #16188
Closed

Failure to detect drift in aws_iam_instance_profile resource #7160

ghost opened this issue Jan 16, 2019 · 2 comments · Fixed by #16188
Labels
bug Addresses a defect in current functionality. service/iam Issues and PRs that pertain to the iam service.
Milestone

Comments

@ghost
Copy link

ghost commented Jan 16, 2019

This issue was originally opened by @milh0use as hashicorp/terraform#20023. It was migrated here as a result of the provider split. The original body of the issue is below.


Terraform Version

0.11.11 (provider.aws 1.52.0)

...

Terraform Configuration Files

resource "aws_iam_instance_profile" "example_instance_profile" {
  name = "example_instance_profile"
  role = "${aws_iam_role.example_role.name}"
}

resource "aws_iam_role" "example_role" {
  name = "example_role"

  assume_role_policy = <<EOF
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Action": "sts:AssumeRole",
            "Principal": {
               "Service": "ec2.amazonaws.com"
            },
            "Effect": "Allow",
            "Sid": ""
        }
    ]
}
EOF
  tags = {
    "Terraform" = "True"
  }
}

resource "aws_iam_role_policy" "example_iam_role_policy" {
  name = "example_iam_role_policy"
  role = "${aws_iam_role.example_role.id}"

  policy = <<EOF
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "AllowToAssumeRole",
      "Action": [
        "sts:AssumeRole"
      ],
      "Effect": "Allow",
      "Resource": [
        "${data.terraform_remote_state.example-policy.role_arn}"
      ]
    },
    {
          "Effect": "Allow",
          "Action" : [
            "ec2:RunInstances",
            ...
          ],
          "Resource" : "*"
  }
  ]
}
EOF
}

Debug Output

Crash Output

Expected Behavior

I've read the excellent article on drift, but we've hit a situation where terraform-managed infrastructure was modified in the AWS console and a terraform apply failed to restore the changes as it failed to detect a modification of the configuration (the list of roles attached to the instance profile) of an aws_iam_instance_profile resource. I would have thought this is a change that terraform could potentially detect and fix.

Actual Behavior

terraform plan recognises that deleted resources need to be recreated but doesn't restore all the links between the resources.

Steps to Reproduce

Note that in the steps below, some of the steps might happen automatically when other steps are done (such as removing a role from an instance profile might happen automatically when you delete the role). A colleague made the changes in the AWS console and I only have the CloudTrail log as evidence of what happened.

  1. Create the resources above using terraform
  2. Delete the example_role from the AWS console
  3. Remove example_role from the example_instance_profile instance profile from the AWS console
  4. Delete the example_iam_role_policy role policy from the AWS console

Now run a terraform plan. Terraform detects that it needs to recreate the deleted resources, but doesn't notice that the configuration of the instance profile has changed such that it is no longer linked to the example_role role.

Additional Context

References

@bflad bflad added the service/iam Issues and PRs that pertain to the iam service. label Jan 17, 2019
@aeschright aeschright added the needs-triage Waiting for first response or review from a maintainer. label Jun 24, 2019
@aeschright aeschright added bug Addresses a defect in current functionality. and removed needs-triage Waiting for first response or review from a maintainer. labels Nov 20, 2019
@github-actions github-actions bot added this to the v3.28.0 milestone Feb 11, 2021
@ghost
Copy link
Author

ghost commented Feb 12, 2021

This has been released in version 3.28.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!

@ghost
Copy link
Author

ghost commented Mar 14, 2021

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!

@ghost ghost locked as resolved and limited conversation to collaborators Mar 14, 2021
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/iam Issues and PRs that pertain to the iam service.
Projects
None yet
2 participants