Skip to content

Commit

Permalink
resource/aws_db_instance: Add max_allocated_storage argument (support…
Browse files Browse the repository at this point in the history
… Storage Autoscaling)

Reference: #9076

Output from acceptance testing (test failures present in master):

```
--- FAIL: TestAccAWSDBInstance_optionGroup (9.60s)
--- PASS: TestAccAWSDBInstance_IsAlreadyBeingDeleted (407.82s)
--- PASS: TestAccAWSDBInstance_kmsKey (447.28s)
--- PASS: TestAccAWSDBInstance_iamAuth (468.51s)
--- PASS: TestAccAWSDBInstance_basic (469.82s)
--- PASS: TestAccAWSDBInstance_generatedName (509.47s)
--- PASS: TestAccAWSDBInstance_namePrefix (509.57s)
--- PASS: TestAccAWSDBInstance_DeletionProtection (534.93s)
--- PASS: TestAccAWSDBInstance_MaxAllocatedStorage (554.00s)
--- PASS: TestAccAWSDBInstance_FinalSnapshotIdentifier_SkipFinalSnapshot (640.05s)
--- FAIL: TestAccAWSDBInstance_ReplicateSourceDb_AutoMinorVersionUpgrade (839.74s)
--- PASS: TestAccAWSDBInstance_subnetGroup (864.75s)
--- PASS: TestAccAWSDBInstance_FinalSnapshotIdentifier (975.11s)
--- FAIL: TestAccAWSDBInstance_S3Import (661.46s)
--- PASS: TestAccAWSDBInstance_ReplicateSourceDb_BackupWindow (1464.99s)
--- PASS: TestAccAWSDBInstance_ReplicateSourceDb_MaxAllocatedStorage (1520.22s)
--- PASS: TestAccAWSDBInstance_ReplicateSourceDb_AvailabilityZone (1536.83s)
--- PASS: TestAccAWSDBInstance_ReplicateSourceDb_IamDatabaseAuthenticationEnabled (1586.30s)
--- PASS: TestAccAWSDBInstance_ReplicateSourceDb_MaintenanceWindow (1635.73s)
--- PASS: TestAccAWSDBInstance_ReplicateSourceDb (1697.74s)
--- PASS: TestAccAWSDBInstance_SnapshotIdentifier (1263.54s)
--- PASS: TestAccAWSDBInstance_ReplicateSourceDb_VpcSecurityGroupIds (1349.56s)
--- PASS: TestAccAWSDBInstance_ReplicateSourceDb_BackupRetentionPeriod (1940.63s)
--- PASS: TestAccAWSDBInstance_ReplicateSourceDb_AllocatedStorage (1949.99s)
--- PASS: TestAccAWSDBInstance_ReplicateSourceDb_Port (1519.48s)
--- PASS: TestAccAWSDBInstance_SnapshotIdentifier_AllocatedStorage (1475.69s)
--- PASS: TestAccAWSDBInstance_SnapshotIdentifier_Io1Storage (1389.89s)
--- PASS: TestAccAWSDBInstance_ReplicateSourceDb_Monitoring (1623.10s)
--- PASS: TestAccAWSDBInstance_SnapshotIdentifier_AutoMinorVersionUpgrade (1233.00s)
--- PASS: TestAccAWSDBInstance_SnapshotIdentifier_AvailabilityZone (1245.29s)
--- PASS: TestAccAWSDBInstance_ReplicateSourceDb_ParameterGroupName (1661.06s)
--- PASS: TestAccAWSDBInstance_SnapshotIdentifier_BackupRetentionPeriod (1285.34s)
--- PASS: TestAccAWSDBInstance_ReplicateSourceDb_MultiAZ (2146.49s)
--- PASS: TestAccAWSDBInstance_SnapshotIdentifier_IamDatabaseAuthenticationEnabled (1083.14s)
--- PASS: TestAccAWSDBInstance_SnapshotIdentifier_DeletionProtection (1149.70s)
--- PASS: TestAccAWSDBInstance_portUpdate (570.32s)
--- PASS: TestAccAWSDBInstance_SnapshotIdentifier_BackupWindow (1234.86s)
--- PASS: TestAccAWSDBInstance_enhancedMonitoring (670.62s)
--- PASS: TestAccAWSDBInstance_separate_iops_update (632.53s)
--- PASS: TestAccAWSDBInstance_SnapshotIdentifier_MaintenanceWindow (1335.83s)
--- PASS: TestAccAWSDBInstance_SnapshotIdentifier_Monitoring (1225.51s)
--- PASS: TestAccAWSDBInstance_SnapshotIdentifier_MaxAllocatedStorage (1295.29s)
--- PASS: TestAccAWSDBInstance_SnapshotIdentifier_BackupRetentionPeriod_Unset (1799.33s)
--- PASS: TestAccAWSDBInstance_MinorVersion (343.49s)
--- PASS: TestAccAWSDBInstance_ec2Classic (395.42s)
--- PASS: TestAccAWSDBInstance_SnapshotIdentifier_Port (1173.81s)
--- PASS: TestAccAWSDBInstance_SnapshotIdentifier_VpcSecurityGroupIds_Tags (1094.16s)
--- PASS: TestAccAWSDBInstance_diffSuppressInitialState (443.64s)
--- PASS: TestAccAWSDBInstance_SnapshotIdentifier_Tags (1223.46s)
--- PASS: TestAccAWSDBInstance_cloudwatchLogsExportConfiguration (511.70s)
--- PASS: TestAccAWSDBInstance_SnapshotIdentifier_ParameterGroupName (1284.52s)
--- PASS: TestAccAWSDBInstance_SnapshotIdentifier_VpcSecurityGroupIds (1196.15s)
--- PASS: TestAccAWSDBInstance_EnabledCloudwatchLogsExports_Postgresql (495.06s)
--- PASS: TestAccAWSDBInstance_SnapshotIdentifier_MultiAZ (1638.76s)
--- PASS: TestAccAWSDBInstance_cloudwatchLogsExportConfigurationUpdate (759.27s)
--- PASS: TestAccAWSDBInstance_EnabledCloudwatchLogsExports_Oracle (686.93s)
--- PASS: TestAccAWSDBInstance_MSSQL_TZ (1926.59s)
--- PASS: TestAccAWSDBInstance_MySQL_SnapshotRestoreWithEngineVersion (1864.61s)
--- PASS: TestAccAWSDBInstance_SnapshotIdentifier_PerformanceInsightsEnabled (1527.89s)
--- PASS: TestAccAWSDBInstance_ReplicateSourceDb_PerformanceInsightsEnabled (1632.72s)
--- PASS: TestAccAWSDBInstance_MSSQL_Domain (3296.64s)
--- PASS: TestAccAWSDBInstance_MSSQL_DomainSnapshotRestore (3228.09s)
--- PASS: TestAccAWSDBInstance_SnapshotIdentifier_MultiAZ_SQLServer (4095.19s)
```
  • Loading branch information
bflad committed Jun 22, 2019
1 parent 471278e commit 6695352
Show file tree
Hide file tree
Showing 3 changed files with 237 additions and 2 deletions.
68 changes: 68 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,18 @@ func resourceAwsDbInstance() *schema.Resource {
ValidateFunc: validateOnceAWeekWindowFormat,
},

"max_allocated_storage": {
Type: schema.TypeInt,
Optional: true,
Computed: 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 +580,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 @@ -709,6 +750,11 @@ func resourceAwsDbInstanceCreate(d *schema.ResourceData, meta interface{}) error
opts.AvailabilityZone = aws.String(attr.(string))
}

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

if attr, ok := d.GetOk("monitoring_role_arn"); ok {
opts.MonitoringRoleArn = aws.String(attr.(string))
}
Expand Down Expand Up @@ -876,6 +922,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 +1078,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 +1270,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 +1508,17 @@ 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)

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
8 changes: 6 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,16 @@ resource "aws_db_instance" "default" {
}
```

### Storage Autoscaling

## 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 +127,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 configured to perform drift detection and its value 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 6695352

Please sign in to comment.