diff --git a/builtin/providers/aws/resource_aws_ecs_service.go b/builtin/providers/aws/resource_aws_ecs_service.go index 19225361c2da..849aaef45791 100644 --- a/builtin/providers/aws/resource_aws_ecs_service.go +++ b/builtin/providers/aws/resource_aws_ecs_service.go @@ -28,8 +28,17 @@ func resourceAwsEcsService() *schema.Resource { Schema: map[string]*schema.Schema{ "name": &schema.Schema{ Type: schema.TypeString, - Required: true, + Optional: true, + ForceNew: true, + Computed: true, + // TODO Up to 255 letters (uppercase and lowercase), numbers, hyphens, and underscores are allowed. + }, + + "name_prefix": &schema.Schema{ + Type: schema.TypeString, + Optional: true, ForceNew: true, + // TODO Up to 255 - len(unique ID); len(uniqueID) == 16 }, "cluster": &schema.Schema{ @@ -89,8 +98,23 @@ func resourceAwsEcsService() *schema.Resource { func resourceAwsEcsServiceCreate(d *schema.ResourceData, meta interface{}) error { conn := meta.(*AWSClient).ecsconn + var name string + + v, nOk := d.GetOk("name") + if nOk { + name = v.(string) + } + p, pOk := d.GetOk("name_prefix") + if pOk { + name = resource.PrefixedUniqueId(p.(string)) + } + + if (nOk && pOk) || name == "" { + return fmt.Errorf("Either 'name' or 'name_prefix' is required") + } + input := ecs.CreateServiceInput{ - ServiceName: aws.String(d.Get("name").(string)), + ServiceName: aws.String(name), TaskDefinition: aws.String(d.Get("task_definition").(string)), DesiredCount: aws.Int64(int64(d.Get("desired_count").(int))), ClientToken: aws.String(resource.UniqueId()), @@ -161,7 +185,7 @@ func resourceAwsEcsServiceRead(d *schema.ResourceData, meta interface{}) error { } if len(out.Services) < 1 { - log.Printf("[DEBUG] Removing ECS service %s (%s) because it's gone", d.Get("name").(string), d.Id()) + log.Printf("[DEBUG] Removing ECS service (%s) because it's gone", d.Id()) d.SetId("") return nil } diff --git a/builtin/providers/aws/resource_aws_ecs_service_test.go b/builtin/providers/aws/resource_aws_ecs_service_test.go index 6b9ed79600f4..3f4121c7e78b 100644 --- a/builtin/providers/aws/resource_aws_ecs_service_test.go +++ b/builtin/providers/aws/resource_aws_ecs_service_test.go @@ -185,6 +185,29 @@ func TestAccAWSEcsService_withIamRole(t *testing.T) { }) } +// Regression for https://github.com/hashicorp/terraform/issues/3444 +// func TestAccAWSEcsService_withLbChanges(t *testing.T) { +// resource.Test(t, resource.TestCase{ +// PreCheck: func() { testAccPreCheck(t) }, +// Providers: testAccProviders, +// CheckDestroy: testAccCheckAWSEcsServiceDestroy, +// Steps: []resource.TestStep{ +// resource.TestStep{ +// Config: testAccAWSEcsService_withLbChanges, +// Check: resource.ComposeTestCheckFunc( +// testAccCheckAWSEcsServiceExists("aws_ecs_service.with_lb_changes"), +// ), +// }, +// resource.TestStep{ +// Config: testAccAWSEcsService_withLbChanges_modified, +// Check: resource.ComposeTestCheckFunc( +// testAccCheckAWSEcsServiceExists("aws_ecs_service.with_lb_changes"), +// ), +// }, +// }, +// }) +// } + // Regression for https://github.com/hashicorp/terraform/issues/3361 func TestAccAWSEcsService_withEcsClusterName(t *testing.T) { clusterName := regexp.MustCompile("^terraformecstestcluster$") @@ -401,8 +424,6 @@ resource "aws_ecs_service" "ghost" { container_name = "%s" container_port = "%d" } - - depends_on = ["aws_iam_role_policy.ecs_service"] } `