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

Added new datasource for listing instance profiles for a role #24423

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/24423.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-data-source
aws_iam_instance_profiles
```
1 change: 1 addition & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,7 @@ func Provider() *schema.Provider {
"aws_iam_account_alias": iam.DataSourceAccountAlias(),
"aws_iam_group": iam.DataSourceGroup(),
"aws_iam_instance_profile": iam.DataSourceInstanceProfile(),
"aws_iam_instance_profiles": iam.DataSourceInstanceProfiles(),
"aws_iam_openid_connect_provider": iam.DataSourceOpenIDConnectProvider(),
"aws_iam_policy": iam.DataSourcePolicy(),
"aws_iam_policy_document": iam.DataSourcePolicyDocument(),
Expand Down
75 changes: 75 additions & 0 deletions internal/service/iam/instance_profiles_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package iam

import (
"fmt"
"log"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/iam"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
)

func DataSourceInstanceProfiles() *schema.Resource {
return &schema.Resource{
Read: dataSourceInstanceProfilesRead,

Schema: map[string]*schema.Schema{
"role_name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validIamResourceName(roleNameMaxLen),
},
"arns": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"paths": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"names": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
}

func dataSourceInstanceProfilesRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*conns.AWSClient).IAMConn

roleName := d.Get("role_name").(string)

req := &iam.ListInstanceProfilesForRoleInput{
RoleName: &roleName,
ewbankkit marked this conversation as resolved.
Show resolved Hide resolved
}

log.Printf("[DEBUG] Reading IAM Instance Profiles for given role %s: %s", roleName, req)
resp, err := conn.ListInstanceProfilesForRole(req)
if err != nil {
return fmt.Errorf("Error getting instance profiles: %w", err)
}
if resp == nil {
return fmt.Errorf("no IAM instance profiles found for role %s", roleName)
}

instanceProfiles := resp.InstanceProfiles

var arns, paths, names []string
for _, profile := range instanceProfiles {
arns = append(arns, aws.StringValue(profile.Arn))
paths = append(paths, aws.StringValue(profile.Path))
names = append(names, aws.StringValue(profile.InstanceProfileName))
}

d.SetId(roleName)
d.Set("arns", arns)
d.Set("paths", paths)
d.Set("names", names)

return nil
}
57 changes: 57 additions & 0 deletions internal/service/iam/instance_profiles_data_source_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package iam_test

import (
"fmt"
"testing"

"github.com/aws/aws-sdk-go/service/iam"
sdkacctest "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-provider-aws/internal/acctest"
)

func TestAccIAMInstanceProfilesDataSource_basic(t *testing.T) {
datasourceName := "data.aws_iam_instance_profiles.test"
resourceName := "aws_iam_instance_profile.test"

rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(t) },
ErrorCheck: acctest.ErrorCheck(t, iam.EndpointsID),
Providers: acctest.Providers,
Steps: []resource.TestStep{
{
Config: testAccInstanceProfilesDataSourceConfig(rName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(datasourceName, "arns.#", "1"),
resource.TestCheckResourceAttr(datasourceName, "paths.#", "1"),
resource.TestCheckResourceAttr(datasourceName, "names.#", "1"),
resource.TestCheckResourceAttrPair(datasourceName, "arns.*", resourceName, "arn"),
resource.TestCheckResourceAttrPair(datasourceName, "paths.*", resourceName, "path"),
resource.TestCheckResourceAttrPair(datasourceName, "names.*", resourceName, "name"),
),
},
},
})
}

func testAccInstanceProfilesDataSourceConfig(rName string) string {
return fmt.Sprintf(`
resource "aws_iam_role" "test" {
name = "%[1]s-role"
ewbankkit marked this conversation as resolved.
Show resolved Hide resolved
assume_role_policy = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"Service\":[\"ec2.amazonaws.com\"]},\"Action\":[\"sts:AssumeRole\"]}]}"
}

resource "aws_iam_instance_profile" "test" {
name = "%[1]s-instance-profile"
ewbankkit marked this conversation as resolved.
Show resolved Hide resolved
role = aws_iam_role.test.name
path = "/testpath/"
}

data "aws_iam_instance_profiles" "test" {
role_name = aws_iam_role.test.name
depends_on = [aws_iam_role.test, aws_iam_instance_profile.test]
}
`, rName)
}
33 changes: 33 additions & 0 deletions website/docs/d/iam_instance_profiles.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
subcategory: "IAM (Identity & Access Management)"
layout: "aws"
page_title: "AWS: aws_iam_instance_profiles"
description: |-
Get information on a Amazon IAM Instance Profiles from IAM role
---

# Data Source: aws_iam_instance_profiles

This data source can be used to fetch information about all
IAM instance profiles under a role. By using this data source, you can reference IAM
instance profile properties without having to hard code ARNs as input.

## Example Usage

```terraform
data "aws_iam_instance_profiles" "example" {
role_name = "an_example_iam_role_name"
}
```

## Argument Reference

* `role_name` - (Required) The IAM role name.

## Attributes Reference

* `arns` - Set of Amazon Resource Name (ARN) specifying the instance profile.

* `names` - Set of IAM instance profile names.

* `paths` - Set of IAM instance profile paths.