Skip to content

Commit

Permalink
Merge pull request #9087 from terraform-providers/f-aws_db_instance-s…
Browse files Browse the repository at this point in the history
…torage-autoscaling

resource/aws_db_instance: Add max_allocated_storage argument (support Storage Autoscaling)
  • Loading branch information
bflad authored Jun 27, 2019
2 parents 560cb7b + 2a07cad commit f0ec2a7
Show file tree
Hide file tree
Showing 3 changed files with 245 additions and 2 deletions.
65 changes: 65 additions & 0 deletions aws/resource_aws_db_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"log"
"regexp"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -97,6 +98,29 @@ func resourceAwsDbInstance() *schema.Resource {
Type: schema.TypeInt,
Optional: true,
Computed: true,
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
mas := d.Get("max_allocated_storage").(int)

newInt, err := strconv.Atoi(new)

if err != nil {
return false
}

oldInt, err := strconv.Atoi(old)

if err != nil {
return false
}

// Allocated is higher than the configuration
// and autoscaling is enabled
if oldInt > newInt && mas > newInt {
return true
}

return false
},
},

"storage_type": {
Expand Down Expand Up @@ -171,6 +195,17 @@ func resourceAwsDbInstance() *schema.Resource {
ValidateFunc: validateOnceAWeekWindowFormat,
},

"max_allocated_storage": {
Type: schema.TypeInt,
Optional: true,
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
if old == "0" && new == fmt.Sprintf("%d", d.Get("allocated_storage").(int)) {
return true
}
return false
},
},

"multi_az": {
Type: schema.TypeBool,
Optional: true,
Expand Down Expand Up @@ -544,6 +579,11 @@ func resourceAwsDbInstanceCreate(d *schema.ResourceData, meta interface{}) error
requiresModifyDbInstance = true
}

if attr, ok := d.GetOk("max_allocated_storage"); ok {
modifyDbInstanceInput.MaxAllocatedStorage = aws.Int64(int64(attr.(int)))
requiresModifyDbInstance = true
}

if attr, ok := d.GetOk("monitoring_interval"); ok {
opts.MonitoringInterval = aws.Int64(int64(attr.(int)))
}
Expand Down Expand Up @@ -876,6 +916,11 @@ func resourceAwsDbInstanceCreate(d *schema.ResourceData, meta interface{}) error
requiresModifyDbInstance = true
}

if attr, ok := d.GetOk("max_allocated_storage"); ok {
modifyDbInstanceInput.MaxAllocatedStorage = aws.Int64(int64(attr.(int)))
requiresModifyDbInstance = true
}

if attr, ok := d.GetOk("monitoring_interval"); ok {
modifyDbInstanceInput.MonitoringInterval = aws.Int64(int64(attr.(int)))
requiresModifyDbInstance = true
Expand Down Expand Up @@ -1027,6 +1072,11 @@ func resourceAwsDbInstanceCreate(d *schema.ResourceData, meta interface{}) error
if attr, ok := d.GetOk("license_model"); ok {
opts.LicenseModel = aws.String(attr.(string))
}

if attr, ok := d.GetOk("max_allocated_storage"); ok {
opts.MaxAllocatedStorage = aws.Int64(int64(attr.(int)))
}

if attr, ok := d.GetOk("parameter_group_name"); ok {
opts.DBParameterGroupName = aws.String(attr.(string))
}
Expand Down Expand Up @@ -1214,6 +1264,7 @@ func resourceAwsDbInstanceRead(d *schema.ResourceData, meta interface{}) error {
d.Set("backup_window", v.PreferredBackupWindow)
d.Set("license_model", v.LicenseModel)
d.Set("maintenance_window", v.PreferredMaintenanceWindow)
d.Set("max_allocated_storage", v.MaxAllocatedStorage)
d.Set("publicly_accessible", v.PubliclyAccessible)
d.Set("multi_az", v.MultiAZ)
d.Set("kms_key_id", v.KmsKeyId)
Expand Down Expand Up @@ -1451,6 +1502,20 @@ func resourceAwsDbInstanceUpdate(d *schema.ResourceData, meta interface{}) error
req.PreferredMaintenanceWindow = aws.String(d.Get("maintenance_window").(string))
requestUpdate = true
}
if d.HasChange("max_allocated_storage") {
d.SetPartial("max_allocated_storage")
mas := d.Get("max_allocated_storage").(int)

// The API expects the max allocated storage value to be set to the allocated storage
// value when disabling autoscaling. This check ensures that value is set correctly
// if the update to the Terraform configuration was removing the argument completely.
if mas == 0 {
mas = d.Get("allocated_storage").(int)
}

req.MaxAllocatedStorage = aws.Int64(int64(mas))
requestUpdate = true
}
if d.HasChange("password") {
d.SetPartial("password")
req.MasterUserPassword = aws.String(d.Get("password").(string))
Expand Down
163 changes: 163 additions & 0 deletions aws/resource_aws_db_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ func TestAccAWSDBInstance_basic(t *testing.T) {
resource.TestCheckResourceAttr(resourceName, "instance_class", "db.t2.micro"),
resource.TestCheckResourceAttr(resourceName, "license_model", "general-public-license"),
resource.TestCheckResourceAttrSet(resourceName, "maintenance_window"),
resource.TestCheckResourceAttr(resourceName, "max_allocated_storage", "0"),
resource.TestCheckResourceAttr(resourceName, "name", "baz"),
resource.TestCheckResourceAttr(resourceName, "option_group_name", "default:mysql-5-6"),
resource.TestCheckResourceAttr(resourceName, "parameter_group_name", "default.mysql5.6"),
Expand Down Expand Up @@ -377,6 +378,49 @@ func TestAccAWSDBInstance_IsAlreadyBeingDeleted(t *testing.T) {
})
}

func TestAccAWSDBInstance_MaxAllocatedStorage(t *testing.T) {
var dbInstance rds.DBInstance

rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_db_instance.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSDBInstanceDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSDBInstanceConfig_MaxAllocatedStorage(rName, 10),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSDBInstanceExists(resourceName, &dbInstance),
resource.TestCheckResourceAttr(resourceName, "max_allocated_storage", "10"),
),
},
{
Config: testAccAWSDBInstanceConfig_MaxAllocatedStorage(rName, 5),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSDBInstanceExists(resourceName, &dbInstance),
resource.TestCheckResourceAttr(resourceName, "max_allocated_storage", "0"),
),
},
{
Config: testAccAWSDBInstanceConfig_MaxAllocatedStorage(rName, 15),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSDBInstanceExists(resourceName, &dbInstance),
resource.TestCheckResourceAttr(resourceName, "max_allocated_storage", "15"),
),
},
{
Config: testAccAWSDBInstanceConfig_MaxAllocatedStorage(rName, 0),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSDBInstanceExists(resourceName, &dbInstance),
resource.TestCheckResourceAttr(resourceName, "max_allocated_storage", "0"),
),
},
},
})
}

func TestAccAWSDBInstance_ReplicateSourceDb(t *testing.T) {
var dbInstance, sourceDbInstance rds.DBInstance

Expand Down Expand Up @@ -622,6 +666,31 @@ func TestAccAWSDBInstance_ReplicateSourceDb_MaintenanceWindow(t *testing.T) {
})
}

func TestAccAWSDBInstance_ReplicateSourceDb_MaxAllocatedStorage(t *testing.T) {
var dbInstance, sourceDbInstance rds.DBInstance

rName := acctest.RandomWithPrefix("tf-acc-test")
sourceResourceName := "aws_db_instance.source"
resourceName := "aws_db_instance.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSDBInstanceDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSDBInstanceConfig_ReplicateSourceDb_MaxAllocatedStorage(rName, 10),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSDBInstanceExists(sourceResourceName, &sourceDbInstance),
testAccCheckAWSDBInstanceExists(resourceName, &dbInstance),
testAccCheckAWSDBInstanceReplicaAttributes(&sourceDbInstance, &dbInstance),
resource.TestCheckResourceAttr(resourceName, "max_allocated_storage", "10"),
),
},
},
})
}

func TestAccAWSDBInstance_ReplicateSourceDb_Monitoring(t *testing.T) {
var dbInstance, sourceDbInstance rds.DBInstance

Expand Down Expand Up @@ -1074,6 +1143,33 @@ func TestAccAWSDBInstance_SnapshotIdentifier_MaintenanceWindow(t *testing.T) {
})
}

func TestAccAWSDBInstance_SnapshotIdentifier_MaxAllocatedStorage(t *testing.T) {
var dbInstance, sourceDbInstance rds.DBInstance
var dbSnapshot rds.DBSnapshot

rName := acctest.RandomWithPrefix("tf-acc-test")
sourceDbResourceName := "aws_db_instance.source"
snapshotResourceName := "aws_db_snapshot.test"
resourceName := "aws_db_instance.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSDBInstanceDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSDBInstanceConfig_SnapshotIdentifier_MaxAllocatedStorage(rName, 10),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSDBInstanceExists(sourceDbResourceName, &sourceDbInstance),
testAccCheckDbSnapshotExists(snapshotResourceName, &dbSnapshot),
testAccCheckAWSDBInstanceExists(resourceName, &dbInstance),
resource.TestCheckResourceAttr(resourceName, "max_allocated_storage", "10"),
),
},
},
})
}

func TestAccAWSDBInstance_SnapshotIdentifier_Monitoring(t *testing.T) {
var dbInstance, sourceDbInstance rds.DBInstance
var dbSnapshot rds.DBSnapshot
Expand Down Expand Up @@ -3749,6 +3845,21 @@ resource "aws_db_instance" "test" {
`, rName)
}

func testAccAWSDBInstanceConfig_MaxAllocatedStorage(rName string, maxAllocatedStorage int) string {
return fmt.Sprintf(`
resource "aws_db_instance" "test" {
allocated_storage = 5
engine = "mysql"
identifier = %[1]q
instance_class = "db.t2.micro"
max_allocated_storage = %[2]d
password = "avoid-plaintext-passwords"
username = "tfacctest"
skip_final_snapshot = true
}
`, rName, maxAllocatedStorage)
}

func testAccAWSDBInstanceConfig_ReplicateSourceDb(rName string) string {
return fmt.Sprintf(`
resource "aws_db_instance" "source" {
Expand Down Expand Up @@ -3963,6 +4074,30 @@ resource "aws_db_instance" "test" {
`, rName, backupWindow, rName, maintenanceWindow)
}

func testAccAWSDBInstanceConfig_ReplicateSourceDb_MaxAllocatedStorage(rName string, maxAllocatedStorage int) string {
return fmt.Sprintf(`
resource "aws_db_instance" "source" {
allocated_storage = 5
backup_retention_period = 1
engine = "mysql"
identifier = "%[1]s-source"
instance_class = "db.t2.micro"
password = "avoid-plaintext-passwords"
username = "tfacctest"
skip_final_snapshot = true
}
resource "aws_db_instance" "test" {
allocated_storage = "${aws_db_instance.source.allocated_storage}"
identifier = %[1]q
instance_class = "${aws_db_instance.source.instance_class}"
max_allocated_storage = %[2]d
replicate_source_db = "${aws_db_instance.source.id}"
skip_final_snapshot = true
}
`, rName, maxAllocatedStorage)
}

func testAccAWSDBInstanceConfig_ReplicateSourceDb_Monitoring(rName string, monitoringInterval int) string {
return fmt.Sprintf(`
data "aws_partition" "current" {}
Expand Down Expand Up @@ -4433,6 +4568,34 @@ resource "aws_db_instance" "test" {
`, rName, rName, backupWindow, rName, maintenanceWindow)
}

func testAccAWSDBInstanceConfig_SnapshotIdentifier_MaxAllocatedStorage(rName string, maxAllocatedStorage int) string {
return fmt.Sprintf(`
resource "aws_db_instance" "source" {
allocated_storage = 5
engine = "mariadb"
identifier = "%[1]s-source"
instance_class = "db.t2.micro"
password = "avoid-plaintext-passwords"
username = "tfacctest"
skip_final_snapshot = true
}
resource "aws_db_snapshot" "test" {
db_instance_identifier = "${aws_db_instance.source.id}"
db_snapshot_identifier = %[1]q
}
resource "aws_db_instance" "test" {
allocated_storage = "${aws_db_instance.source.allocated_storage}"
identifier = %[1]q
instance_class = "${aws_db_instance.source.instance_class}"
max_allocated_storage = %[2]d
snapshot_identifier = "${aws_db_snapshot.test.id}"
skip_final_snapshot = true
}
`, rName, maxAllocatedStorage)
}

func testAccAWSDBInstanceConfig_SnapshotIdentifier_Monitoring(rName string, monitoringInterval int) string {
return fmt.Sprintf(`
data "aws_partition" "current" {}
Expand Down
19 changes: 17 additions & 2 deletions website/docs/r/db_instance.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ about [DB Instance Class Types](https://docs.aws.amazon.com/AmazonRDS/latest/Use

## Example Usage

### Basic Usage

```hcl
resource "aws_db_instance" "default" {
allocated_storage = 20
Expand All @@ -50,15 +52,27 @@ resource "aws_db_instance" "default" {
}
```

### Storage Autoscaling

To enable Storage Autoscaling with instances that support the feature, define the `max_allocated_storage` argument higher than the `allocated_storage` argument. Terraform will automatically hide differences with the `allocated_storage` argument value if autoscaling occurs.

```hcl
resource "aws_db_instance" "example {
# ... other configuration ...
allocated_storage = 50
max_allocated_storage = 100
}
```

## Argument Reference

For more detailed documentation about each argument, refer to the [AWS official
documentation](http://docs.aws.amazon.com/AmazonRDS/latest/APIReference/API_CreateDBInstance.html).

The following arguments are supported:

* `allocated_storage` - (Required unless a `snapshot_identifier` or
`replicate_source_db` is provided) The allocated storage in gibibytes.
* `allocated_storage` - (Required unless a `snapshot_identifier` or `replicate_source_db` is provided) The allocated storage in gibibytes. If `max_allocated_storage` is configured, this argument represents the initial storage allocation and differences from the configuration will be ignored automatically when Storage Autoscaling occurs.
* `allow_major_version_upgrade` - (Optional) Indicates that major version
upgrades are allowed. Changing this parameter does not result in an outage and
the change is asynchronously applied as soon as possible.
Expand Down Expand Up @@ -124,6 +138,7 @@ Syntax: "ddd:hh24:mi-ddd:hh24:mi". Eg: "Mon:00:00-Mon:03:00". See [RDS
Maintenance Window
docs](http://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/USER_UpgradeDBInstance.Maintenance.html#AdjustingTheMaintenanceWindow)
for more information.
* `max_allocated_storage` - (Optional) When configured, the upper limit to which Amazon RDS can automatically scale the storage of the DB instance. Configuring this will automatically ignore differences to `allocated_storage`. Must be greater than or equal to `allocated_storage` or `0` to disable Storage Autoscaling.
* `monitoring_interval` - (Optional) The interval, in seconds, between points
when Enhanced Monitoring metrics are collected for the DB instance. To disable
collecting Enhanced Monitoring metrics, specify 0. The default is 0. Valid
Expand Down

0 comments on commit f0ec2a7

Please sign in to comment.