Skip to content

Commit 83abf5b

Browse files
authored
provider/aws: Support import of aws_iam_instance_profile (hashicorp#10436)
Fixes hashicorp#10341 ``` % make testacc TEST=./builtin/providers/aws TESTARGS='-run=TestAccAWSIAMInstanceProfile_' ==> Checking that code complies with gofmt requirements... go generate $(go list ./... | grep -v /terraform/vendor/) 2016/11/30 14:32:59 Generated command/internal_plugin_list.go TF_ACC=1 go test ./builtin/providers/aws -v -run=TestAccAWSIAMInstanceProfile_ -timeout 120m === RUN TestAccAWSIAMInstanceProfile_importBasic --- PASS: TestAccAWSIAMInstanceProfile_importBasic (20.22s) === RUN TestAccAWSIAMInstanceProfile_basic --- PASS: TestAccAWSIAMInstanceProfile_basic (18.71s) === RUN TestAccAWSIAMInstanceProfile_namePrefix --- PASS: TestAccAWSIAMInstanceProfile_namePrefix (18.58s) PASS ok github.com/hashicorp/terraform/builtin/providers/aws57.535s ```
1 parent c918c7b commit 83abf5b

4 files changed

Lines changed: 58 additions & 17 deletions

File tree

builtin/providers/aws/resource_aws_iam_instance_profile.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,27 @@ func resourceAwsIamInstanceProfile() *schema.Resource {
1818
Read: resourceAwsIamInstanceProfileRead,
1919
Update: resourceAwsIamInstanceProfileUpdate,
2020
Delete: resourceAwsIamInstanceProfileDelete,
21+
Importer: &schema.ResourceImporter{
22+
State: schema.ImportStatePassthrough,
23+
},
2124

2225
Schema: map[string]*schema.Schema{
23-
"arn": &schema.Schema{
26+
"arn": {
2427
Type: schema.TypeString,
2528
Computed: true,
2629
},
2730

28-
"create_date": &schema.Schema{
31+
"create_date": {
2932
Type: schema.TypeString,
3033
Computed: true,
3134
},
3235

33-
"unique_id": &schema.Schema{
36+
"unique_id": {
3437
Type: schema.TypeString,
3538
Computed: true,
3639
},
3740

38-
"name": &schema.Schema{
41+
"name": {
3942
Type: schema.TypeString,
4043
Optional: true,
4144
Computed: true,
@@ -56,7 +59,7 @@ func resourceAwsIamInstanceProfile() *schema.Resource {
5659
},
5760
},
5861

59-
"name_prefix": &schema.Schema{
62+
"name_prefix": {
6063
Type: schema.TypeString,
6164
Optional: true,
6265
ForceNew: true,
@@ -75,14 +78,14 @@ func resourceAwsIamInstanceProfile() *schema.Resource {
7578
},
7679
},
7780

78-
"path": &schema.Schema{
81+
"path": {
7982
Type: schema.TypeString,
8083
Optional: true,
8184
Default: "/",
8285
ForceNew: true,
8386
},
8487

85-
"roles": &schema.Schema{
88+
"roles": {
8689
Type: schema.TypeSet,
8790
Required: true,
8891
Elem: &schema.Schema{Type: schema.TypeString},

builtin/providers/aws/resource_aws_iam_instance_profile_test.go

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,50 @@ import (
88
"github.com/aws/aws-sdk-go/aws"
99
"github.com/aws/aws-sdk-go/aws/awserr"
1010
"github.com/aws/aws-sdk-go/service/iam"
11+
"github.com/hashicorp/terraform/helper/acctest"
1112
"github.com/hashicorp/terraform/helper/resource"
1213
"github.com/hashicorp/terraform/terraform"
1314
)
1415

16+
func TestAccAWSIAMInstanceProfile_importBasic(t *testing.T) {
17+
resourceName := "aws_iam_instance_profile.test"
18+
rName := acctest.RandString(5)
19+
20+
resource.Test(t, resource.TestCase{
21+
PreCheck: func() { testAccPreCheck(t) },
22+
Providers: testAccProviders,
23+
CheckDestroy: testAccCheckAWSInstanceProfileDestroy,
24+
Steps: []resource.TestStep{
25+
{
26+
Config: testAccAWSInstanceProfilePrefixNameConfig(rName),
27+
},
28+
29+
{
30+
ResourceName: resourceName,
31+
ImportState: true,
32+
ImportStateVerify: true,
33+
ImportStateVerifyIgnore: []string{"name_prefix"},
34+
},
35+
},
36+
})
37+
}
38+
1539
func TestAccAWSIAMInstanceProfile_basic(t *testing.T) {
40+
rName := acctest.RandString(5)
1641
resource.Test(t, resource.TestCase{
1742
PreCheck: func() { testAccPreCheck(t) },
1843
Providers: testAccProviders,
1944
Steps: []resource.TestStep{
20-
resource.TestStep{
21-
Config: testAccAwsIamInstanceProfileConfig,
45+
{
46+
Config: testAccAwsIamInstanceProfileConfig(rName),
2247
},
2348
},
2449
})
2550
}
2651

2752
func TestAccAWSIAMInstanceProfile_namePrefix(t *testing.T) {
2853
var conf iam.GetInstanceProfileOutput
54+
rName := acctest.RandString(5)
2955

3056
resource.Test(t, resource.TestCase{
3157
PreCheck: func() { testAccPreCheck(t) },
@@ -34,8 +60,8 @@ func TestAccAWSIAMInstanceProfile_namePrefix(t *testing.T) {
3460
Providers: testAccProviders,
3561
CheckDestroy: testAccCheckAWSInstanceProfileDestroy,
3662
Steps: []resource.TestStep{
37-
resource.TestStep{
38-
Config: testAccAWSInstanceProfilePrefixNameConfig,
63+
{
64+
Config: testAccAWSInstanceProfilePrefixNameConfig(rName),
3965
Check: resource.ComposeTestCheckFunc(
4066
testAccCheckAWSInstanceProfileExists("aws_iam_instance_profile.test", &conf),
4167
testAccCheckAWSInstanceProfileGeneratedNamePrefix(
@@ -118,26 +144,28 @@ func testAccCheckAWSInstanceProfileExists(n string, res *iam.GetInstanceProfileO
118144
}
119145
}
120146

121-
const testAccAwsIamInstanceProfileConfig = `
147+
func testAccAwsIamInstanceProfileConfig(rName string) string {
148+
return fmt.Sprintf(`
122149
resource "aws_iam_role" "test" {
123-
name = "test"
150+
name = "test-%s"
124151
assume_role_policy = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"Service\":[\"ec2.amazonaws.com\"]},\"Action\":[\"sts:AssumeRole\"]}]}"
125152
}
126153
127154
resource "aws_iam_instance_profile" "test" {
128155
name = "test"
129156
roles = ["${aws_iam_role.test.name}"]
157+
}`, rName)
130158
}
131-
`
132159

133-
const testAccAWSInstanceProfilePrefixNameConfig = `
160+
func testAccAWSInstanceProfilePrefixNameConfig(rName string) string {
161+
return fmt.Sprintf(`
134162
resource "aws_iam_role" "test" {
135-
name = "test"
163+
name = "test-%s"
136164
assume_role_policy = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"Service\":[\"ec2.amazonaws.com\"]},\"Action\":[\"sts:AssumeRole\"]}]}"
137165
}
138166
139167
resource "aws_iam_instance_profile" "test" {
140168
name_prefix = "test-"
141169
roles = ["${aws_iam_role.test.name}"]
170+
}`, rName)
142171
}
143-
`

website/source/docs/import/importability.html.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ To make a resource importable, please see the
5757
* aws_glacier_vault
5858
* aws_iam_account_password_policy
5959
* aws_iam_group
60+
* aws_iam_instance_profile
6061
* aws_iam_saml_provider
6162
* aws_iam_user
6263
* aws_instance

website/source/docs/providers/aws/r/iam_instance_profile.html.markdown

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,12 @@ The following arguments are supported:
5959
* `unique_id` - The [unique ID][1] assigned by AWS.
6060

6161
[1]: https://docs.aws.amazon.com/IAM/latest/UserGuide/Using_Identifiers.html#GUIDs
62+
63+
64+
## Import
65+
66+
Instance Profiles can be imported using the `name`, e.g.
67+
68+
```
69+
$ terraform import aws_iam_instance_profile.test_profile app-instance-profile-1
70+
```

0 commit comments

Comments
 (0)