-
Notifications
You must be signed in to change notification settings - Fork 9.2k
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
aws_iam_instance_profile DataSource should return Role Name in addition to or instead of Role ID #4080
Comments
Hey – The It accepts I believe this would work for you, can you try it out? data "aws_iam_role" "docker_staging_stack_manager" {
name = "${data.aws_iam_instance_profile.docker_staging_stack_manager.name}"
unique_id = "${data.aws_iam_instance_profile.docker_staging_stack_manager.role_id}"
} |
@catsby I'm happy to try, but I think that is the name of the instance profile, not the associated role, right? In my particular example,
The role it's referencing, on the other hand, has the name |
Running it initially I get
Removing
Running The general role format of the AWS URLs is like:
Putting the instance profile name in there yields a "No Entity Found" error, while putting the expected role name yields the expected role. |
Thanks for the follow up! I'm not sure we (HashiCorp) be able to tackle this right away, hopefully someone from the community will be able to |
It seems like it should be pretty easy. The AWS API does return the role name, as you can see in the response from the CLI:
I'm not a Go programmer, but I started taking a stab at updating the tests here. I'm sure I'm doing something wrong, but I can't get the tests to fail! I've updated the tests as follows: func TestAccAWSDataSourceIAMInstanceProfile_basic(t *testing.T) {
roleName := fmt.Sprintf("test-datasource-user-%d", acctest.RandInt())
profileName := fmt.Sprintf("test-datasource-user-%d", acctest.RandInt())
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDatasourceAwsIamInstanceProfileConfig(roleName, profileName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("data.aws_iam_instance_profile.test", "role_id"),
# I added the line below!
resource.TestCheckResourceAttrSet("data.aws_iam_instance_profile.test", "role_name"),
resource.TestCheckResourceAttr("data.aws_iam_instance_profile.test", "path", "/testpath/"),
resource.TestMatchResourceAttr("data.aws_iam_instance_profile.test", "arn",
regexp.MustCompile("^arn:aws:iam::[0-9]{12}:instance-profile/testpath/"+profileName+"$")),
),
},
},
})
} It seems like this should fail, but it doesn't. |
FYI, I am currently working around this using a provisioner "local-exec" {
command = <<EOF
aws iam get-instance-profile --instance-profile-name=${data.aws_iam_instance_profile.docker_staging_stack_manager.name} \
| grep -oP 'RoleName": "\K(${aws_cloudformation_stack.docker_staging_stack.name}[-0-9A-Za-z]+)(?=",)' \
| xargs -I '{}' aws iam attach-role-policy --policy-arn ${aws_iam_policy.docker_staging_deploy.arn} --role-name '{}'
EOF
} This grabs the instance profile JSON, parses it to get the associated role name (which is all the data source needs to do to actually be useful), and then pipes that to the |
The |
This has been released in version 1.16.0 of the AWS provider. Please see the Terraform documentation on provider versioning or reach out if you need any assistance upgrading. |
Thanks @bflad - we'll update asap and replace our existing hack |
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! |
Terraform Version
0.11.5
AWS Provider Version
1.13.0
Affected Resource(s)
Terraform Configuration Files
Expected Behavior
We're working with the Docker Cloudformation template, and attempting to use terraform to add a policy to the Instance Profile automatically created by the template. The template's outputs are pretty paltry:
If it were possible to navigate back to the dynamically generated role associated with the EC2 instances, we could add our policy to that role, no problem. However, there is no Data Source that allows searching for a Role by anything other than a complete name (which we don't have, since it was automatically generated).
However, I found a way (as shown in the configs above) to wind my way all the way to the Instance Policy, which is associated with the Role I'm trying to find. Unfortunately, it only exposes the unique Role ID, which is not used by any other Role-related Data Source or Resource, making it basically useless.
Ideally there would either be a way to get the RoleName from the
aws_iam_instance_profile
, or to use the RoleID to search for a role. The former seems better, since RoleNames are used throughout all other Role-related resources.References
None that I could find
The text was updated successfully, but these errors were encountered: