diff --git a/builtin/providers/aws/resource_aws_db_instance.go b/builtin/providers/aws/resource_aws_db_instance.go index 34db57c051f2..527b21e7045d 100644 --- a/builtin/providers/aws/resource_aws_db_instance.go +++ b/builtin/providers/aws/resource_aws_db_instance.go @@ -85,7 +85,8 @@ func resourceAwsDbInstance() *schema.Resource { "identifier": &schema.Schema{ Type: schema.TypeString, - Required: true, + Optional: true, + Computed: true, ForceNew: true, ValidateFunc: validateRdsId, }, @@ -291,12 +292,25 @@ func resourceAwsDbInstanceCreate(d *schema.ResourceData, meta interface{}) error conn := meta.(*AWSClient).rdsconn tags := tagsFromMapRDS(d.Get("tags").(map[string]interface{})) + identifier := d.Get("identifier").(string) + // Generate a unique ID for the user + if identifier == "" { + identifier = resource.PrefixedUniqueId("tf-") + // SQL Server identifier size is max 15 chars, so truncate + if engine := d.Get("engine").(string); engine != "" { + if strings.Contains(strings.ToLower(engine), "sqlserver") { + identifier = identifier[:15] + } + } + d.Set("identifier", identifier) + } + if v, ok := d.GetOk("replicate_source_db"); ok { opts := rds.CreateDBInstanceReadReplicaInput{ SourceDBInstanceIdentifier: aws.String(v.(string)), CopyTagsToSnapshot: aws.Bool(d.Get("copy_tags_to_snapshot").(bool)), DBInstanceClass: aws.String(d.Get("instance_class").(string)), - DBInstanceIdentifier: aws.String(d.Get("identifier").(string)), + DBInstanceIdentifier: aws.String(identifier), Tags: tags, } if attr, ok := d.GetOk("iops"); ok { diff --git a/builtin/providers/aws/resource_aws_db_instance_test.go b/builtin/providers/aws/resource_aws_db_instance_test.go index e8dbf9df6f3c..a1dd431ea923 100644 --- a/builtin/providers/aws/resource_aws_db_instance_test.go +++ b/builtin/providers/aws/resource_aws_db_instance_test.go @@ -362,10 +362,8 @@ func testAccCheckAWSDBInstanceExists(n string, v *rds.DBInstance) resource.TestC // Database names cannot collide, and deletion takes so long, that making the // name a bit random helps so able we can kill a test that's just waiting for a // delete and not be blocked on kicking off another one. -var testAccAWSDBInstanceConfig = fmt.Sprintf(` +var testAccAWSDBInstanceConfig = ` resource "aws_db_instance" "bar" { - identifier = "foobarbaz-test-terraform-%d" - allocated_storage = 10 engine = "MySQL" engine_version = "5.6.21" @@ -383,7 +381,7 @@ resource "aws_db_instance" "bar" { backup_retention_period = 0 parameter_group_name = "default.mysql5.6" -}`, rand.New(rand.NewSource(time.Now().UnixNano())).Int()) +}` func testAccReplicaInstanceConfig(val int) string { return fmt.Sprintf(` diff --git a/website/source/docs/providers/aws/r/db_instance.html.markdown b/website/source/docs/providers/aws/r/db_instance.html.markdown index 058d4cdc9c55..1fdc41273d09 100644 --- a/website/source/docs/providers/aws/r/db_instance.html.markdown +++ b/website/source/docs/providers/aws/r/db_instance.html.markdown @@ -28,16 +28,15 @@ for more information. ``` resource "aws_db_instance" "default" { - identifier = "mydb-rds" - allocated_storage = 10 - engine = "mysql" - engine_version = "5.6.17" - instance_class = "db.t1.micro" - name = "mydb" - username = "foo" - password = "bar" - db_subnet_group_name = "my_database_subnet_group" - parameter_group_name = "default.mysql5.6" + allocated_storage = 10 + engine = "mysql" + engine_version = "5.6.17" + instance_class = "db.t1.micro" + name = "mydb" + username = "foo" + password = "bar" + db_subnet_group_name = "my_database_subnet_group" + parameter_group_name = "default.mysql5.6" } ``` @@ -51,7 +50,7 @@ The following arguments are supported: * `allocated_storage` - (Required unless a `snapshot_identifier` or `replicate_source_db` is provided) The allocated storage in gigabytes. * `engine` - (Required unless a `snapshot_identifier` or `replicate_source_db` is provided) The database engine to use. * `engine_version` - (Optional) The engine version to use. -* `identifier` - (Required) The name of the RDS instance +* `identifier` - (Optional) The name of the RDS instance, if omitted, Terraform will assign a random, unique name * `instance_class` - (Required) The instance type of the RDS instance. * `storage_type` - (Optional) One of "standard" (magnetic), "gp2" (general purpose SSD), or "io1" (provisioned IOPS SSD). The default is "io1" if