diff --git a/aws/resource_aws_appautoscaling_target.go b/aws/resource_aws_appautoscaling_target.go index d23f72e6dd6..4a1a39322d6 100644 --- a/aws/resource_aws_appautoscaling_target.go +++ b/aws/resource_aws_appautoscaling_target.go @@ -37,7 +37,8 @@ func resourceAwsAppautoscalingTarget() *schema.Resource { }, "role_arn": { Type: schema.TypeString, - Required: true, + Optional: true, + Computed: true, ForceNew: true, }, "scalable_dimension": { @@ -64,10 +65,13 @@ func resourceAwsAppautoscalingTargetCreate(d *schema.ResourceData, meta interfac targetOpts.MaxCapacity = aws.Int64(int64(d.Get("max_capacity").(int))) targetOpts.MinCapacity = aws.Int64(int64(d.Get("min_capacity").(int))) targetOpts.ResourceId = aws.String(d.Get("resource_id").(string)) - targetOpts.RoleARN = aws.String(d.Get("role_arn").(string)) targetOpts.ScalableDimension = aws.String(d.Get("scalable_dimension").(string)) targetOpts.ServiceNamespace = aws.String(d.Get("service_namespace").(string)) + if roleArn, exists := d.GetOk("role_arn"); exists { + targetOpts.RoleARN = aws.String(roleArn.(string)) + } + log.Printf("[DEBUG] Application autoscaling target create configuration %#v", targetOpts) var err error err = resource.Retry(1*time.Minute, func() *resource.RetryError { diff --git a/aws/resource_aws_appautoscaling_target_test.go b/aws/resource_aws_appautoscaling_target_test.go index 0f6b3977b32..f7b8027781b 100644 --- a/aws/resource_aws_appautoscaling_target_test.go +++ b/aws/resource_aws_appautoscaling_target_test.go @@ -2,6 +2,7 @@ package aws import ( "fmt" + "regexp" "testing" "github.com/aws/aws-sdk-go/aws" @@ -122,6 +123,30 @@ func TestAccAWSAppautoScalingTarget_multipleTargets(t *testing.T) { }) } +func TestAccAWSAppautoScalingTarget_optionalRoleArn(t *testing.T) { + var readTarget applicationautoscaling.ScalableTarget + + rInt := acctest.RandInt() + tableName := fmt.Sprintf("tf_acc_test_table_%d", rInt) + + r, _ := regexp.Compile("arn:aws:iam::.*:role/aws-service-role/dynamodb.application-autoscaling.amazonaws.com/AWSServiceRoleForApplicationAutoScaling_DynamoDBTable") + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSAppautoscalingTargetDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSAppautoscalingTarget_optionalRoleArn(tableName), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSAppautoscalingTargetExists("aws_appautoscaling_target.read", &readTarget), + resource.TestMatchResourceAttr("aws_appautoscaling_target.read", "role_arn", r), + ), + }, + }, + }) +} + func testAccCheckAWSAppautoscalingTargetDestroy(s *terraform.State) error { conn := testAccProvider.Meta().(*AWSClient).appautoscalingconn @@ -192,58 +217,6 @@ func testAccCheckAWSAppautoscalingTargetExists(n string, target *applicationauto func testAccAWSAppautoscalingTargetConfig( randClusterName string) string { return fmt.Sprintf(` -resource "aws_iam_role" "autoscale_role" { - name = "autoscalerole%s" - path = "/" - - assume_role_policy = <