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.61k stars 9k forks source link

[Enhancement]: aws_iam_service_linked_role adopt existing resource #29318

Open sidekick-eimantas opened 1 year ago

sidekick-eimantas commented 1 year ago

Description

Multiple aws_iam_service_linked_role's are created automatically by AWS when an account is opened. Namely the "organizations.amazonaws.com", "support.amazonaws.com" and "trustedadvisor.amazonaws.com". As part of an account setup, we try to create those service linked roles, and a few others. Those three however fail as they already exist. Importing them is too inconvenient in fully CI/CD'd setups. Would be great if the aws_iam_service_linked_role resource could automatically adopt an existing service linked role.

Affected Resource(s) and/or Data Source(s)

Potential Terraform Configuration

No response

References

No response

Would you like to implement a fix?

None

github-actions[bot] commented 1 year ago

Community Note

Voting for Prioritization

Volunteering to Work on This Issue

gdavison commented 1 year ago

Thanks for filing this, @sidekick-eimantas. I was about to do the same.

timsutton commented 1 year ago

Would be great if the aws_iam_service_linked_role resource could automatically adopt an existing service linked role.

I had a similar thought. Is there a workaround that might be usable today? As in to maybe try importing an existing IAM role with the expected name, and then invoke this role conditionally if a resource could be imported?

WolverineFan commented 2 months ago

NOTE: If someone implements this, the adopted resource should NOT be deleted with a terraform destroy. It would be quite possible that multiple modules/state files would end up referencing the same adopted resource, and if you ran a destroy on just one of them it would break them all. An example of that is the AWSServiceRoleForAutoScaling role, which might be "created" alongside every autoscaling group, but shouldn't be destroyed or it will impact ALL autoscaling groups.

WolverineFan commented 2 months ago

Would be great if the aws_iam_service_linked_role resource could automatically adopt an existing service linked role.

I had a similar thought. Is there a workaround that might be usable today? As in to maybe try importing an existing IAM role with the expected name, and then invoke this role conditionally if a resource could be imported?

You could maybe use an import block in Terraform 1.5+ but you would hit the problem I describe in my comment above where the imported resource would be destroyed and that may not be what you want.