diff --git a/builtin/providers/aws/resource_aws_autoscaling_group.go b/builtin/providers/aws/resource_aws_autoscaling_group.go index f457e6dcd..d5a87e33b 100644 --- a/builtin/providers/aws/resource_aws_autoscaling_group.go +++ b/builtin/providers/aws/resource_aws_autoscaling_group.go @@ -25,7 +25,8 @@ func resourceAwsAutoscalingGroup() *schema.Resource { Schema: map[string]*schema.Schema{ "name": &schema.Schema{ Type: schema.TypeString, - Required: true, + Optional: true, + Computed: true, ForceNew: true, ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) { // https://github.com/boto/botocore/blob/9f322b1/botocore/data/autoscaling/2011-01-01/service-2.json#L1862-L1873 @@ -144,7 +145,16 @@ func resourceAwsAutoscalingGroupCreate(d *schema.ResourceData, meta interface{}) conn := meta.(*AWSClient).autoscalingconn var autoScalingGroupOpts autoscaling.CreateAutoScalingGroupInput - autoScalingGroupOpts.AutoScalingGroupName = aws.String(d.Get("name").(string)) + + var asgName string + if v, ok := d.GetOk("name"); ok { + asgName = v.(string) + } else { + asgName = resource.PrefixedUniqueId("tf-asg-") + d.Set("name", asgName) + } + + autoScalingGroupOpts.AutoScalingGroupName = aws.String(asgName) autoScalingGroupOpts.LaunchConfigurationName = aws.String(d.Get("launch_configuration").(string)) autoScalingGroupOpts.MinSize = aws.Int64(int64(d.Get("min_size").(int))) autoScalingGroupOpts.MaxSize = aws.Int64(int64(d.Get("max_size").(int))) diff --git a/builtin/providers/aws/resource_aws_autoscaling_group_test.go b/builtin/providers/aws/resource_aws_autoscaling_group_test.go index 1a25c9dea..5f87bc3d0 100644 --- a/builtin/providers/aws/resource_aws_autoscaling_group_test.go +++ b/builtin/providers/aws/resource_aws_autoscaling_group_test.go @@ -3,6 +3,7 @@ package aws import ( "fmt" "reflect" + "regexp" "strings" "testing" @@ -71,6 +72,26 @@ func TestAccAWSAutoScalingGroup_basic(t *testing.T) { }) } +func TestAccAWSAutoScalingGroup_autoGeneratedName(t *testing.T) { + asgNameRegexp := regexp.MustCompile("^tf-asg-") + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSAutoScalingGroupDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccAWSAutoScalingGroupConfig_autoGeneratedName, + Check: resource.ComposeTestCheckFunc( + resource.TestMatchResourceAttr( + "aws_autoscaling_group.bar", "name", asgNameRegexp), + ), + }, + }, + }) + +} + func TestAccAWSAutoScalingGroup_tags(t *testing.T) { var group autoscaling.Group @@ -348,6 +369,32 @@ func testAccCheckAWSAutoScalingGroupAttributesVPCZoneIdentifer(group *autoscalin } } +const testAccAWSAutoScalingGroupConfig_autoGeneratedName = ` +resource "aws_launch_configuration" "foobar" { + image_id = "ami-21f78e11" + instance_type = "t1.micro" +} + +resource "aws_autoscaling_group" "bar" { + availability_zones = ["us-west-2a"] + max_size = 1 + min_size = 1 + health_check_grace_period = 300 + health_check_type = "ELB" + desired_capacity = 1 + force_delete = true + termination_policies = ["OldestInstance","ClosestToNextInstanceHour"] + + launch_configuration = "${aws_launch_configuration.foobar.name}" + + tag { + key = "Foo" + value = "foo-bar" + propagate_at_launch = true + } +} +` + const testAccAWSAutoScalingGroupConfig = ` resource "aws_launch_configuration" "foobar" { image_id = "ami-21f78e11" diff --git a/website/source/docs/providers/aws/r/autoscaling_group.html.markdown b/website/source/docs/providers/aws/r/autoscaling_group.html.markdown index 7cb166142..6f2b0e511 100644 --- a/website/source/docs/providers/aws/r/autoscaling_group.html.markdown +++ b/website/source/docs/providers/aws/r/autoscaling_group.html.markdown @@ -41,7 +41,7 @@ resource "aws_autoscaling_group" "bar" { The following arguments are supported: -* `name` - (Required) The name of the auto scale group. +* `name` - (Optional) The name of the auto scale group. By default generated by terraform. * `max_size` - (Required) The maximum size of the auto scale group. * `min_size` - (Required) The minimum size of the auto scale group. (See also [Waiting for Capacity](#waiting-for-capacity) below.)