diff --git a/builtin/providers/aws/resource_aws_db_instance.go b/builtin/providers/aws/resource_aws_db_instance.go index bc8cec6129..bf4076c45d 100644 --- a/builtin/providers/aws/resource_aws_db_instance.go +++ b/builtin/providers/aws/resource_aws_db_instance.go @@ -289,6 +289,13 @@ func resourceAwsDbInstance() *schema.Resource { Computed: true, }, + "kms_key_id": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + Computed: true, + ForceNew: true, + }, + "tags": tagsSchema(), }, } @@ -396,6 +403,7 @@ func resourceAwsDbInstanceCreate(d *schema.ResourceData, meta interface{}) error if attr, ok := d.GetOk("option_group_name"); ok { opts.OptionGroupName = aws.String(attr.(string)) + } if attr, ok := d.GetOk("port"); ok { @@ -492,6 +500,7 @@ func resourceAwsDbInstanceCreate(d *schema.ResourceData, meta interface{}) error opts.BackupRetentionPeriod = aws.Int64(int64(attr.(int))) if attr, ok := d.GetOk("multi_az"); ok { opts.MultiAZ = aws.Bool(attr.(bool)) + } if attr, ok := d.GetOk("maintenance_window"); ok { @@ -560,6 +569,10 @@ func resourceAwsDbInstanceCreate(d *schema.ResourceData, meta interface{}) error opts.OptionGroupName = aws.String(attr.(string)) } + if attr, ok := d.GetOk("kms_key_id"); ok { + opts.KmsKeyId = aws.String(attr.(string)) + } + log.Printf("[DEBUG] DB Instance create configuration: %#v", opts) var err error err = resource.Retry(5*time.Minute, func() *resource.RetryError { @@ -632,6 +645,7 @@ func resourceAwsDbInstanceRead(d *schema.ResourceData, meta interface{}) error { d.Set("maintenance_window", v.PreferredMaintenanceWindow) d.Set("publicly_accessible", v.PubliclyAccessible) d.Set("multi_az", v.MultiAZ) + d.Set("kms_key_id", v.KmsKeyId) if v.DBSubnetGroup != nil { d.Set("db_subnet_group_name", v.DBSubnetGroup.DBSubnetGroupName) } diff --git a/builtin/providers/aws/resource_aws_db_instance_test.go b/builtin/providers/aws/resource_aws_db_instance_test.go index f44d4df316..91a484f0e8 100644 --- a/builtin/providers/aws/resource_aws_db_instance_test.go +++ b/builtin/providers/aws/resource_aws_db_instance_test.go @@ -3,6 +3,7 @@ package aws import ( "fmt" "log" + "regexp" "strings" "math/rand" @@ -51,6 +52,31 @@ func TestAccAWSDBInstance_basic(t *testing.T) { }) } +func TestAccAWSDBInstance_kmsKey(t *testing.T) { + var v rds.DBInstance + keyRegex := regexp.MustCompile("^arn:aws:kms:") + + ri := rand.New(rand.NewSource(time.Now().UnixNano())).Int() + config := fmt.Sprintf(testAccAWSDBInstanceConfigKmsKeyId, ri) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSDBInstanceDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: config, + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSDBInstanceExists("aws_db_instance.bar", &v), + testAccCheckAWSDBInstanceAttributes(&v), + resource.TestMatchResourceAttr( + "aws_db_instance.bar", "kms_key_id", keyRegex), + ), + }, + }, + }) +} + func TestAccAWSDBInstance_optionGroup(t *testing.T) { var v rds.DBInstance @@ -404,6 +430,51 @@ resource "aws_db_instance" "bar" { parameter_group_name = "default.mysql5.6" }` +var testAccAWSDBInstanceConfigKmsKeyId = ` +resource "aws_kms_key" "foo" { + description = "Terraform acc test %s" + policy = < **NOTE:** Removing the `replicate_source_db` attribute from an existing RDS