func resource_aws_db_instance_create(
	s *terraform.ResourceState,
	d *terraform.ResourceDiff,
	meta interface{}) (*terraform.ResourceState, error) {
	p := meta.(*ResourceProvider)
	conn := p.rdsconn

	// Merge the diff into the state so that we have all the attributes
	// properly.
	rs := s.MergeDiff(d)

	var err error
	var attr string

	opts := rds.CreateDBInstance{}

	if attr = rs.Attributes["allocated_storage"]; attr != "" {
		opts.AllocatedStorage, err = strconv.Atoi(attr)
		opts.SetAllocatedStorage = true
	}

	if attr = rs.Attributes["backup_retention_period"]; attr != "" {
		opts.BackupRetentionPeriod, err = strconv.Atoi(attr)
		opts.SetBackupRetentionPeriod = true
	}

	if attr = rs.Attributes["iops"]; attr != "" {
		opts.Iops, err = strconv.Atoi(attr)
		opts.SetIops = true
	}

	if attr = rs.Attributes["port"]; attr != "" {
		opts.Port, err = strconv.Atoi(attr)
		opts.SetPort = true
	}

	if attr = rs.Attributes["availability_zone"]; attr != "" {
		opts.AvailabilityZone = attr
	}

	if attr = rs.Attributes["instance_class"]; attr != "" {
		opts.DBInstanceClass = attr
	}

	if attr = rs.Attributes["maintenance_window"]; attr != "" {
		opts.PreferredMaintenanceWindow = attr
	}

	if attr = rs.Attributes["backup_window"]; attr != "" {
		opts.PreferredBackupWindow = attr
	}

	if attr = rs.Attributes["multi_az"]; attr == "true" {
		opts.MultiAZ = true
	}

	if attr = rs.Attributes["publicly_accessible"]; attr == "true" {
		opts.PubliclyAccessible = true
	}

	if attr = rs.Attributes["subnet_group_name"]; attr != "" {
		opts.DBSubnetGroupName = attr
	}

	if err != nil {
		return nil, fmt.Errorf("Error parsing configuration: %s", err)
	}

	if _, ok := rs.Attributes["vpc_security_group_ids.#"]; ok {
		opts.VpcSecurityGroupIds = expandStringList(flatmap.Expand(
			rs.Attributes, "vpc_security_group_ids").([]interface{}))
	}

	if _, ok := rs.Attributes["security_group_names.#"]; ok {
		opts.DBSecurityGroupNames = expandStringList(flatmap.Expand(
			rs.Attributes, "security_group_names").([]interface{}))
	}

	opts.DBInstanceIdentifier = rs.Attributes["identifier"]
	opts.DBName = rs.Attributes["name"]
	opts.MasterUsername = rs.Attributes["username"]
	opts.MasterUserPassword = rs.Attributes["password"]
	opts.EngineVersion = rs.Attributes["engine_version"]
	opts.Engine = rs.Attributes["engine"]

	// Don't keep the password around in the state
	delete(rs.Attributes, "password")

	log.Printf("[DEBUG] DB Instance create configuration: %#v", opts)
	_, err = conn.CreateDBInstance(&opts)
	if err != nil {
		return nil, fmt.Errorf("Error creating DB Instance: %s", err)
	}

	rs.ID = rs.Attributes["identifier"]

	log.Printf("[INFO] DB Instance ID: %s", rs.ID)

	log.Println(
		"[INFO] Waiting for DB Instance to be available")

	stateConf := &resource.StateChangeConf{
		Pending:    []string{"creating", "backing-up", "modifying"},
		Target:     "available",
		Refresh:    DBInstanceStateRefreshFunc(rs.ID, conn),
		Timeout:    10 * time.Minute,
		MinTimeout: 10 * time.Second,
		Delay:      30 * time.Second, // Wait 30 secs before starting
	}

	// Wait, catching any errors
	_, err = stateConf.WaitForState()
	if err != nil {
		return rs, err
	}

	v, err := resource_aws_db_instance_retrieve(rs.ID, conn)
	if err != nil {
		return rs, err
	}

	return resource_aws_db_instance_update_state(rs, v)
}
func resourceAwsDbInstanceCreate(d *schema.ResourceData, meta interface{}) error {
	conn := meta.(*AWSClient).rdsconn
	opts := rds.CreateDBInstance{
		AllocatedStorage:     d.Get("allocated_storage").(int),
		SetAllocatedStorage:  true,
		DBInstanceClass:      d.Get("instance_class").(string),
		DBInstanceIdentifier: d.Get("identifier").(string),
		DBName:               d.Get("name").(string),
		MasterUsername:       d.Get("username").(string),
		MasterUserPassword:   d.Get("password").(string),
		Engine:               d.Get("engine").(string),
		EngineVersion:        d.Get("engine_version").(string),
	}

	// Special treatment for the password, as we don't want that
	// saved into the state file
	d.Set("password", "")

	if attr, ok := d.GetOk("backup_retention_period"); ok {
		opts.BackupRetentionPeriod = attr.(int)
		opts.SetBackupRetentionPeriod = true
	}

	if attr, ok := d.GetOk("iops"); ok {
		opts.Iops = attr.(int)
		opts.SetIops = true
	}

	if attr, ok := d.GetOk("port"); ok {
		opts.Port = attr.(int)
		opts.SetPort = true
	}

	if attr, ok := d.GetOk("multi_az"); ok {
		opts.MultiAZ = attr.(bool)
	}

	if attr, ok := d.GetOk("availability_zone"); ok {
		opts.AvailabilityZone = attr.(string)
	}

	if attr, ok := d.GetOk("maintenance_window"); ok {
		opts.PreferredMaintenanceWindow = attr.(string)
	}

	if attr, ok := d.GetOk("backup_window"); ok {
		opts.PreferredBackupWindow = attr.(string)
	}

	if attr, ok := d.GetOk("publicly_accessible"); ok {
		opts.PubliclyAccessible = attr.(bool)
	}

	if attr, ok := d.GetOk("db_subnet_group_name"); ok {
		opts.DBSubnetGroupName = attr.(string)
	}

	if attr, ok := d.GetOk("parameter_group_name"); ok {
		opts.DBParameterGroupName = attr.(string)
	}

	if attr := d.Get("vpc_security_group_ids").(*schema.Set); attr.Len() > 0 {
		var s []string
		for _, v := range attr.List() {
			s = append(s, v.(string))
		}
		opts.VpcSecurityGroupIds = s
	}

	if attr := d.Get("security_group_names").(*schema.Set); attr.Len() > 0 {
		var s []string
		for _, v := range attr.List() {
			s = append(s, v.(string))
		}
		opts.DBSecurityGroupNames = s
	}

	log.Printf("[DEBUG] DB Instance create configuration: %#v", opts)
	_, err := conn.CreateDBInstance(&opts)
	if err != nil {
		return fmt.Errorf("Error creating DB Instance: %s", err)
	}

	d.SetId(d.Get("identifier").(string))

	log.Printf("[INFO] DB Instance ID: %s", d.Id())

	log.Println(
		"[INFO] Waiting for DB Instance to be available")

	stateConf := &resource.StateChangeConf{
		Pending:    []string{"creating", "backing-up", "modifying"},
		Target:     "available",
		Refresh:    resourceAwsDbInstanceStateRefreshFunc(d, meta),
		Timeout:    20 * time.Minute,
		MinTimeout: 10 * time.Second,
		Delay:      30 * time.Second, // Wait 30 secs before starting
	}

	// Wait, catching any errors
	_, err = stateConf.WaitForState()
	if err != nil {
		return err
	}

	return resourceAwsDbInstanceRead(d, meta)
}