Skip to content

Commit

Permalink
Remove ES service linked role creation
Browse files Browse the repository at this point in the history
This is not currently working due to the error message being different than expected ('cannot be found' instead of the expected 'Role not found').

Instead of fixing the bug it was decided that a better approach would be to force users to explicitly create the IAM service linked role instead of doing it for them if it didn't exist.
This more closely matches what we do elsewhere and also means we aren't implicitly creating an IAM role despite the user not trying to do that which is particularly dangerous with IAM even if it is just a service linked role.

This is also reasonably necessary because it's going to be an absolute pain to catch this in acceptance tests as it's something that gets implicitly created once per account and then not managed by Terraform after that.

See the discussion on hashicorp#5218 for more information.
  • Loading branch information
tomelliff committed Jan 17, 2019
1 parent c308ad1 commit 928af82
Showing 1 changed file with 0 additions and 37 deletions.
37 changes: 0 additions & 37 deletions aws/resource_aws_elasticsearch_domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
elasticsearch "github.com/aws/aws-sdk-go/service/elasticsearchservice"
"github.com/aws/aws-sdk-go/service/iam"
"github.com/hashicorp/terraform/helper/customdiff"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
Expand Down Expand Up @@ -320,37 +319,6 @@ func resourceAwsElasticSearchDomainImport(
return []*schema.ResourceData{d}, nil
}

// This would be created automatically if the domain is created via Console
// see http://docs.aws.amazon.com/elasticsearch-service/latest/developerguide/es-vpc.html#es-enabling-slr
func createAwsElasticsearchIAMServiceRoleIfMissing(meta interface{}) error {
serviceRoleName := "AWSServiceRoleForAmazonElasticsearchService"
serviceName := "es.amazonaws.com"

conn := meta.(*AWSClient).iamconn

getRequest := &iam.GetRoleInput{
RoleName: aws.String(serviceRoleName),
}
_, err := conn.GetRole(getRequest)
if err != nil {
if isAWSErr(err, iam.ErrCodeNoSuchEntityException, "Role not found") {
createRequest := &iam.CreateServiceLinkedRoleInput{
AWSServiceName: aws.String(serviceName),
}
_, err := conn.CreateServiceLinkedRole(createRequest)
if err != nil {
if isAWSErr(err, iam.ErrCodeInvalidInputException, "has been taken in this account") {
return nil
}
return fmt.Errorf("Error creating IAM Service-Linked Role %s: %s", serviceRoleName, err)
}
return nil
}
return fmt.Errorf("Error reading IAM Role %s: %s", serviceRoleName, err)
}
return nil
}

func resourceAwsElasticSearchDomainCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).esconn

Expand Down Expand Up @@ -438,11 +406,6 @@ func resourceAwsElasticSearchDomainCreate(d *schema.ResourceData, meta interface
}

if v, ok := d.GetOk("vpc_options"); ok {
err = createAwsElasticsearchIAMServiceRoleIfMissing(meta)
if err != nil {
return err
}

options := v.([]interface{})
if options[0] == nil {
return fmt.Errorf("At least one field is expected inside vpc_options")
Expand Down

0 comments on commit 928af82

Please sign in to comment.