github.com/keshavdv/terraform@v0.7.0-rc2.0.20160711232630-d69256dcb425/builtin/providers/aws/resource_aws_db_instance_test.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"regexp"
     7  	"strings"
     8  
     9  	"math/rand"
    10  	"testing"
    11  	"time"
    12  
    13  	"github.com/hashicorp/terraform/helper/acctest"
    14  	"github.com/hashicorp/terraform/helper/resource"
    15  	"github.com/hashicorp/terraform/terraform"
    16  
    17  	"github.com/aws/aws-sdk-go/aws"
    18  	"github.com/aws/aws-sdk-go/aws/awserr"
    19  	"github.com/aws/aws-sdk-go/service/rds"
    20  )
    21  
    22  func TestAccAWSDBInstance_basic(t *testing.T) {
    23  	var v rds.DBInstance
    24  
    25  	resource.Test(t, resource.TestCase{
    26  		PreCheck:     func() { testAccPreCheck(t) },
    27  		Providers:    testAccProviders,
    28  		CheckDestroy: testAccCheckAWSDBInstanceDestroy,
    29  		Steps: []resource.TestStep{
    30  			resource.TestStep{
    31  				Config: testAccAWSDBInstanceConfig,
    32  				Check: resource.ComposeTestCheckFunc(
    33  					testAccCheckAWSDBInstanceExists("aws_db_instance.bar", &v),
    34  					testAccCheckAWSDBInstanceAttributes(&v),
    35  					resource.TestCheckResourceAttr(
    36  						"aws_db_instance.bar", "allocated_storage", "10"),
    37  					resource.TestCheckResourceAttr(
    38  						"aws_db_instance.bar", "engine", "mysql"),
    39  					resource.TestCheckResourceAttr(
    40  						"aws_db_instance.bar", "license_model", "general-public-license"),
    41  					resource.TestCheckResourceAttr(
    42  						"aws_db_instance.bar", "instance_class", "db.t1.micro"),
    43  					resource.TestCheckResourceAttr(
    44  						"aws_db_instance.bar", "name", "baz"),
    45  					resource.TestCheckResourceAttr(
    46  						"aws_db_instance.bar", "username", "foo"),
    47  					resource.TestCheckResourceAttr(
    48  						"aws_db_instance.bar", "parameter_group_name", "default.mysql5.6"),
    49  				),
    50  			},
    51  		},
    52  	})
    53  }
    54  
    55  func TestAccAWSDBInstance_kmsKey(t *testing.T) {
    56  	var v rds.DBInstance
    57  	keyRegex := regexp.MustCompile("^arn:aws:kms:")
    58  
    59  	ri := rand.New(rand.NewSource(time.Now().UnixNano())).Int()
    60  	config := fmt.Sprintf(testAccAWSDBInstanceConfigKmsKeyId, ri)
    61  
    62  	resource.Test(t, resource.TestCase{
    63  		PreCheck:     func() { testAccPreCheck(t) },
    64  		Providers:    testAccProviders,
    65  		CheckDestroy: testAccCheckAWSDBInstanceDestroy,
    66  		Steps: []resource.TestStep{
    67  			resource.TestStep{
    68  				Config: config,
    69  				Check: resource.ComposeTestCheckFunc(
    70  					testAccCheckAWSDBInstanceExists("aws_db_instance.bar", &v),
    71  					testAccCheckAWSDBInstanceAttributes(&v),
    72  					resource.TestMatchResourceAttr(
    73  						"aws_db_instance.bar", "kms_key_id", keyRegex),
    74  				),
    75  			},
    76  		},
    77  	})
    78  }
    79  
    80  func TestAccAWSDBInstance_optionGroup(t *testing.T) {
    81  	var v rds.DBInstance
    82  
    83  	resource.Test(t, resource.TestCase{
    84  		PreCheck:     func() { testAccPreCheck(t) },
    85  		Providers:    testAccProviders,
    86  		CheckDestroy: testAccCheckAWSDBInstanceDestroy,
    87  		Steps: []resource.TestStep{
    88  			resource.TestStep{
    89  				Config: testAccAWSDBInstanceConfigWithOptionGroup,
    90  				Check: resource.ComposeTestCheckFunc(
    91  					testAccCheckAWSDBInstanceExists("aws_db_instance.bar", &v),
    92  					testAccCheckAWSDBInstanceAttributes(&v),
    93  					resource.TestCheckResourceAttr(
    94  						"aws_db_instance.bar", "option_group_name", "option-group-test-terraform"),
    95  				),
    96  			},
    97  		},
    98  	})
    99  }
   100  
   101  func TestAccAWSDBInstanceReplica(t *testing.T) {
   102  	var s, r rds.DBInstance
   103  
   104  	resource.Test(t, resource.TestCase{
   105  		PreCheck:     func() { testAccPreCheck(t) },
   106  		Providers:    testAccProviders,
   107  		CheckDestroy: testAccCheckAWSDBInstanceDestroy,
   108  		Steps: []resource.TestStep{
   109  			resource.TestStep{
   110  				Config: testAccReplicaInstanceConfig(rand.New(rand.NewSource(time.Now().UnixNano())).Int()),
   111  				Check: resource.ComposeTestCheckFunc(
   112  					testAccCheckAWSDBInstanceExists("aws_db_instance.bar", &s),
   113  					testAccCheckAWSDBInstanceExists("aws_db_instance.replica", &r),
   114  					testAccCheckAWSDBInstanceReplicaAttributes(&s, &r),
   115  				),
   116  			},
   117  		},
   118  	})
   119  }
   120  
   121  func TestAccAWSDBInstanceSnapshot(t *testing.T) {
   122  	var snap rds.DBInstance
   123  
   124  	resource.Test(t, resource.TestCase{
   125  		PreCheck:  func() { testAccPreCheck(t) },
   126  		Providers: testAccProviders,
   127  		// testAccCheckAWSDBInstanceSnapshot verifies a database snapshot is
   128  		// created, and subequently deletes it
   129  		CheckDestroy: testAccCheckAWSDBInstanceSnapshot,
   130  		Steps: []resource.TestStep{
   131  			resource.TestStep{
   132  				Config: testAccSnapshotInstanceConfig(),
   133  				Check: resource.ComposeTestCheckFunc(
   134  					testAccCheckAWSDBInstanceExists("aws_db_instance.snapshot", &snap),
   135  				),
   136  			},
   137  		},
   138  	})
   139  }
   140  
   141  func TestAccAWSDBInstanceNoSnapshot(t *testing.T) {
   142  	var nosnap rds.DBInstance
   143  
   144  	resource.Test(t, resource.TestCase{
   145  		PreCheck:     func() { testAccPreCheck(t) },
   146  		Providers:    testAccProviders,
   147  		CheckDestroy: testAccCheckAWSDBInstanceNoSnapshot,
   148  		Steps: []resource.TestStep{
   149  			resource.TestStep{
   150  				Config: testAccNoSnapshotInstanceConfig(),
   151  				Check: resource.ComposeTestCheckFunc(
   152  					testAccCheckAWSDBInstanceExists("aws_db_instance.no_snapshot", &nosnap),
   153  				),
   154  			},
   155  		},
   156  	})
   157  }
   158  
   159  func TestAccAWSDBInstance_enhancedMonitoring(t *testing.T) {
   160  	var dbInstance rds.DBInstance
   161  
   162  	resource.Test(t, resource.TestCase{
   163  		PreCheck:     func() { testAccPreCheck(t) },
   164  		Providers:    testAccProviders,
   165  		CheckDestroy: testAccCheckAWSDBInstanceNoSnapshot,
   166  		Steps: []resource.TestStep{
   167  			resource.TestStep{
   168  				Config: testAccSnapshotInstanceConfig_enhancedMonitoring,
   169  				Check: resource.ComposeTestCheckFunc(
   170  					testAccCheckAWSDBInstanceExists("aws_db_instance.enhanced_monitoring", &dbInstance),
   171  					resource.TestCheckResourceAttr(
   172  						"aws_db_instance.enhanced_monitoring", "monitoring_interval", "5"),
   173  				),
   174  			},
   175  		},
   176  	})
   177  }
   178  
   179  // Regression test for https://github.com/hashicorp/terraform/issues/3760 .
   180  // We apply a plan, then change just the iops. If the apply succeeds, we
   181  // consider this a pass, as before in 3760 the request would fail
   182  func TestAccAWSDBInstance_iops_update(t *testing.T) {
   183  	var v rds.DBInstance
   184  
   185  	rName := acctest.RandString(5)
   186  
   187  	resource.Test(t, resource.TestCase{
   188  		PreCheck:     func() { testAccPreCheck(t) },
   189  		Providers:    testAccProviders,
   190  		CheckDestroy: testAccCheckAWSDBInstanceDestroy,
   191  		Steps: []resource.TestStep{
   192  			resource.TestStep{
   193  				Config: testAccSnapshotInstanceConfig_iopsUpdate(rName, 1000),
   194  				Check: resource.ComposeTestCheckFunc(
   195  					testAccCheckAWSDBInstanceExists("aws_db_instance.bar", &v),
   196  					testAccCheckAWSDBInstanceAttributes(&v),
   197  				),
   198  			},
   199  
   200  			resource.TestStep{
   201  				Config: testAccSnapshotInstanceConfig_iopsUpdate(rName, 2000),
   202  				Check: resource.ComposeTestCheckFunc(
   203  					testAccCheckAWSDBInstanceExists("aws_db_instance.bar", &v),
   204  					testAccCheckAWSDBInstanceAttributes(&v),
   205  				),
   206  				// The plan will be non-empty because even with apply_immediatley, the
   207  				// instance has to apply the change via reboot, so follow up plans will
   208  				// show a non empty plan. The test is considered "successful" if the
   209  				// follow up change is applied at all.
   210  				ExpectNonEmptyPlan: true,
   211  			},
   212  		},
   213  	})
   214  }
   215  
   216  func testAccCheckAWSDBInstanceDestroy(s *terraform.State) error {
   217  	conn := testAccProvider.Meta().(*AWSClient).rdsconn
   218  
   219  	for _, rs := range s.RootModule().Resources {
   220  		if rs.Type != "aws_db_instance" {
   221  			continue
   222  		}
   223  
   224  		// Try to find the Group
   225  		var err error
   226  		resp, err := conn.DescribeDBInstances(
   227  			&rds.DescribeDBInstancesInput{
   228  				DBInstanceIdentifier: aws.String(rs.Primary.ID),
   229  			})
   230  
   231  		if ae, ok := err.(awserr.Error); ok && ae.Code() == "DBInstanceNotFound" {
   232  			continue
   233  		}
   234  
   235  		if err == nil {
   236  			if len(resp.DBInstances) != 0 &&
   237  				*resp.DBInstances[0].DBInstanceIdentifier == rs.Primary.ID {
   238  				return fmt.Errorf("DB Instance still exists")
   239  			}
   240  		}
   241  
   242  		// Verify the error
   243  		newerr, ok := err.(awserr.Error)
   244  		if !ok {
   245  			return err
   246  		}
   247  		if newerr.Code() != "DBInstanceNotFound" {
   248  			return err
   249  		}
   250  	}
   251  
   252  	return nil
   253  }
   254  
   255  func testAccCheckAWSDBInstanceAttributes(v *rds.DBInstance) resource.TestCheckFunc {
   256  	return func(s *terraform.State) error {
   257  
   258  		if *v.Engine != "mysql" {
   259  			return fmt.Errorf("bad engine: %#v", *v.Engine)
   260  		}
   261  
   262  		if *v.EngineVersion == "" {
   263  			return fmt.Errorf("bad engine_version: %#v", *v.EngineVersion)
   264  		}
   265  
   266  		if *v.BackupRetentionPeriod != 0 {
   267  			return fmt.Errorf("bad backup_retention_period: %#v", *v.BackupRetentionPeriod)
   268  		}
   269  
   270  		return nil
   271  	}
   272  }
   273  
   274  func testAccCheckAWSDBInstanceReplicaAttributes(source, replica *rds.DBInstance) resource.TestCheckFunc {
   275  	return func(s *terraform.State) error {
   276  
   277  		if replica.ReadReplicaSourceDBInstanceIdentifier != nil && *replica.ReadReplicaSourceDBInstanceIdentifier != *source.DBInstanceIdentifier {
   278  			return fmt.Errorf("bad source identifier for replica, expected: '%s', got: '%s'", *source.DBInstanceIdentifier, *replica.ReadReplicaSourceDBInstanceIdentifier)
   279  		}
   280  
   281  		return nil
   282  	}
   283  }
   284  
   285  func testAccCheckAWSDBInstanceSnapshot(s *terraform.State) error {
   286  	conn := testAccProvider.Meta().(*AWSClient).rdsconn
   287  
   288  	for _, rs := range s.RootModule().Resources {
   289  		if rs.Type != "aws_db_instance" {
   290  			continue
   291  		}
   292  
   293  		var err error
   294  		resp, err := conn.DescribeDBInstances(
   295  			&rds.DescribeDBInstancesInput{
   296  				DBInstanceIdentifier: aws.String(rs.Primary.ID),
   297  			})
   298  
   299  		if err != nil {
   300  			newerr, _ := err.(awserr.Error)
   301  			if newerr.Code() != "DBInstanceNotFound" {
   302  				return err
   303  			}
   304  
   305  		} else {
   306  			if len(resp.DBInstances) != 0 &&
   307  				*resp.DBInstances[0].DBInstanceIdentifier == rs.Primary.ID {
   308  				return fmt.Errorf("DB Instance still exists")
   309  			}
   310  		}
   311  
   312  		log.Printf("[INFO] Trying to locate the DBInstance Final Snapshot")
   313  		snapshot_identifier := "foobarbaz-test-terraform-final-snapshot-1"
   314  		_, snapErr := conn.DescribeDBSnapshots(
   315  			&rds.DescribeDBSnapshotsInput{
   316  				DBSnapshotIdentifier: aws.String(snapshot_identifier),
   317  			})
   318  
   319  		if snapErr != nil {
   320  			newerr, _ := snapErr.(awserr.Error)
   321  			if newerr.Code() == "DBSnapshotNotFound" {
   322  				return fmt.Errorf("Snapshot %s not found", snapshot_identifier)
   323  			}
   324  		} else { // snapshot was found
   325  			// verify we have the tags copied to the snapshot
   326  			instanceARN, err := buildRDSARN(snapshot_identifier, testAccProvider.Meta())
   327  			// tags have a different ARN, just swapping :db: for :snapshot:
   328  			tagsARN := strings.Replace(instanceARN, ":db:", ":snapshot:", 1)
   329  			if err != nil {
   330  				return fmt.Errorf("Error building ARN for tags check with ARN (%s): %s", tagsARN, err)
   331  			}
   332  			resp, err := conn.ListTagsForResource(&rds.ListTagsForResourceInput{
   333  				ResourceName: aws.String(tagsARN),
   334  			})
   335  			if err != nil {
   336  				return fmt.Errorf("Error retrieving tags for ARN (%s): %s", tagsARN, err)
   337  			}
   338  
   339  			if resp.TagList == nil || len(resp.TagList) == 0 {
   340  				return fmt.Errorf("Tag list is nil or zero: %s", resp.TagList)
   341  			}
   342  
   343  			var found bool
   344  			for _, t := range resp.TagList {
   345  				if *t.Key == "Name" && *t.Value == "tf-tags-db" {
   346  					found = true
   347  				}
   348  			}
   349  			if !found {
   350  				return fmt.Errorf("Expected to find tag Name (%s), but wasn't found. Tags: %s", "tf-tags-db", resp.TagList)
   351  			}
   352  			// end tag search
   353  
   354  			log.Printf("[INFO] Deleting the Snapshot %s", snapshot_identifier)
   355  			_, snapDeleteErr := conn.DeleteDBSnapshot(
   356  				&rds.DeleteDBSnapshotInput{
   357  					DBSnapshotIdentifier: aws.String(snapshot_identifier),
   358  				})
   359  			if snapDeleteErr != nil {
   360  				return err
   361  			}
   362  		} // end snapshot was found
   363  	}
   364  
   365  	return nil
   366  }
   367  
   368  func testAccCheckAWSDBInstanceNoSnapshot(s *terraform.State) error {
   369  	conn := testAccProvider.Meta().(*AWSClient).rdsconn
   370  
   371  	for _, rs := range s.RootModule().Resources {
   372  		if rs.Type != "aws_db_instance" {
   373  			continue
   374  		}
   375  
   376  		var err error
   377  		resp, err := conn.DescribeDBInstances(
   378  			&rds.DescribeDBInstancesInput{
   379  				DBInstanceIdentifier: aws.String(rs.Primary.ID),
   380  			})
   381  
   382  		if err != nil {
   383  			newerr, _ := err.(awserr.Error)
   384  			if newerr.Code() != "DBInstanceNotFound" {
   385  				return err
   386  			}
   387  
   388  		} else {
   389  			if len(resp.DBInstances) != 0 &&
   390  				*resp.DBInstances[0].DBInstanceIdentifier == rs.Primary.ID {
   391  				return fmt.Errorf("DB Instance still exists")
   392  			}
   393  		}
   394  
   395  		snapshot_identifier := "foobarbaz-test-terraform-final-snapshot-2"
   396  		_, snapErr := conn.DescribeDBSnapshots(
   397  			&rds.DescribeDBSnapshotsInput{
   398  				DBSnapshotIdentifier: aws.String(snapshot_identifier),
   399  			})
   400  
   401  		if snapErr != nil {
   402  			newerr, _ := snapErr.(awserr.Error)
   403  			if newerr.Code() != "DBSnapshotNotFound" {
   404  				return fmt.Errorf("Snapshot %s found and it shouldn't have been", snapshot_identifier)
   405  			}
   406  		}
   407  	}
   408  
   409  	return nil
   410  }
   411  
   412  func testAccCheckAWSDBInstanceExists(n string, v *rds.DBInstance) resource.TestCheckFunc {
   413  	return func(s *terraform.State) error {
   414  		rs, ok := s.RootModule().Resources[n]
   415  		if !ok {
   416  			return fmt.Errorf("Not found: %s", n)
   417  		}
   418  
   419  		if rs.Primary.ID == "" {
   420  			return fmt.Errorf("No DB Instance ID is set")
   421  		}
   422  
   423  		conn := testAccProvider.Meta().(*AWSClient).rdsconn
   424  
   425  		opts := rds.DescribeDBInstancesInput{
   426  			DBInstanceIdentifier: aws.String(rs.Primary.ID),
   427  		}
   428  
   429  		resp, err := conn.DescribeDBInstances(&opts)
   430  
   431  		if err != nil {
   432  			return err
   433  		}
   434  
   435  		if len(resp.DBInstances) != 1 ||
   436  			*resp.DBInstances[0].DBInstanceIdentifier != rs.Primary.ID {
   437  			return fmt.Errorf("DB Instance not found")
   438  		}
   439  
   440  		*v = *resp.DBInstances[0]
   441  
   442  		return nil
   443  	}
   444  }
   445  
   446  // Database names cannot collide, and deletion takes so long, that making the
   447  // name a bit random helps so able we can kill a test that's just waiting for a
   448  // delete and not be blocked on kicking off another one.
   449  var testAccAWSDBInstanceConfig = `
   450  resource "aws_db_instance" "bar" {
   451  	allocated_storage = 10
   452  	engine = "MySQL"
   453  	engine_version = "5.6.21"
   454  	instance_class = "db.t1.micro"
   455  	name = "baz"
   456  	password = "barbarbarbar"
   457  	username = "foo"
   458  
   459  
   460  	# Maintenance Window is stored in lower case in the API, though not strictly 
   461  	# documented. Terraform will downcase this to match (as opposed to throw a 
   462  	# validation error).
   463  	maintenance_window = "Fri:09:00-Fri:09:30"
   464  
   465  	backup_retention_period = 0
   466  
   467  	parameter_group_name = "default.mysql5.6"
   468  }`
   469  
   470  var testAccAWSDBInstanceConfigKmsKeyId = `
   471  resource "aws_kms_key" "foo" {
   472      description = "Terraform acc test %s"
   473      policy = <<POLICY
   474  {
   475    "Version": "2012-10-17",
   476    "Id": "kms-tf-1",
   477    "Statement": [
   478      {
   479        "Sid": "Enable IAM User Permissions",
   480        "Effect": "Allow",
   481        "Principal": {
   482          "AWS": "*"
   483        },
   484        "Action": "kms:*",
   485        "Resource": "*"
   486      }
   487    ]
   488  }
   489  POLICY
   490  }
   491  
   492  resource "aws_db_instance" "bar" {
   493  	allocated_storage = 10
   494  	engine = "MySQL"
   495  	engine_version = "5.6.21"
   496  	instance_class = "db.m3.medium"
   497  	name = "baz"
   498  	password = "barbarbarbar"
   499  	username = "foo"
   500  
   501  
   502  	# Maintenance Window is stored in lower case in the API, though not strictly
   503  	# documented. Terraform will downcase this to match (as opposed to throw a
   504  	# validation error).
   505  	maintenance_window = "Fri:09:00-Fri:09:30"
   506  
   507  	backup_retention_period = 0
   508  	storage_encrypted = true
   509  	kms_key_id = "${aws_kms_key.foo.arn}"
   510  
   511  	parameter_group_name = "default.mysql5.6"
   512  }
   513  `
   514  
   515  var testAccAWSDBInstanceConfigWithOptionGroup = fmt.Sprintf(`
   516  
   517  resource "aws_db_option_group" "bar" {
   518  	name = "option-group-test-terraform"
   519  	option_group_description = "Test option group for terraform"
   520  	engine_name = "mysql"
   521  	major_engine_version = "5.6"
   522  }
   523  
   524  resource "aws_db_instance" "bar" {
   525  	identifier = "foobarbaz-test-terraform-%d"
   526  
   527  	allocated_storage = 10
   528  	engine = "MySQL"
   529  	instance_class = "db.m1.small"
   530  	name = "baz"
   531  	password = "barbarbarbar"
   532  	username = "foo"
   533  
   534  	backup_retention_period = 0
   535  
   536  	parameter_group_name = "default.mysql5.6"
   537  	option_group_name = "${aws_db_option_group.bar.name}"
   538  }`, acctest.RandInt())
   539  
   540  func testAccReplicaInstanceConfig(val int) string {
   541  	return fmt.Sprintf(`
   542  	resource "aws_db_instance" "bar" {
   543  		identifier = "foobarbaz-test-terraform-%d"
   544  
   545  		allocated_storage = 5
   546  		engine = "mysql"
   547  		engine_version = "5.6.21"
   548  		instance_class = "db.t1.micro"
   549  		name = "baz"
   550  		password = "barbarbarbar"
   551  		username = "foo"
   552  
   553  		backup_retention_period = 1
   554  
   555  		parameter_group_name = "default.mysql5.6"
   556  	}
   557  	
   558  	resource "aws_db_instance" "replica" {
   559  		identifier = "tf-replica-db-%d"
   560  		backup_retention_period = 0
   561  		replicate_source_db = "${aws_db_instance.bar.identifier}"
   562  		allocated_storage = "${aws_db_instance.bar.allocated_storage}"
   563  		engine = "${aws_db_instance.bar.engine}"
   564  		engine_version = "${aws_db_instance.bar.engine_version}"
   565  		instance_class = "${aws_db_instance.bar.instance_class}"
   566  		password = "${aws_db_instance.bar.password}"
   567  		username = "${aws_db_instance.bar.username}"
   568  		tags {
   569  			Name = "tf-replica-db"
   570  		}
   571  	}
   572  	`, val, val)
   573  }
   574  
   575  func testAccSnapshotInstanceConfig() string {
   576  	return fmt.Sprintf(`
   577  provider "aws" {
   578    region = "us-east-1"
   579  }
   580  resource "aws_db_instance" "snapshot" {
   581  	identifier = "tf-snapshot-%d"
   582  
   583  	allocated_storage = 5
   584  	engine = "mysql"
   585  	engine_version = "5.6.21"
   586  	instance_class = "db.t1.micro"
   587  	name = "baz"
   588  	password = "barbarbarbar"
   589  	username = "foo"
   590  	security_group_names = ["default"]
   591  	backup_retention_period = 1
   592  
   593  	publicly_accessible = true
   594  
   595  	parameter_group_name = "default.mysql5.6"
   596  
   597  	skip_final_snapshot = false
   598  	copy_tags_to_snapshot = true
   599  	final_snapshot_identifier = "foobarbaz-test-terraform-final-snapshot-1"
   600  	tags {
   601  		Name = "tf-tags-db"
   602  	}
   603  }`, acctest.RandInt())
   604  }
   605  
   606  func testAccNoSnapshotInstanceConfig() string {
   607  	return fmt.Sprintf(`
   608  provider "aws" {
   609    region = "us-east-1"
   610  }
   611  resource "aws_db_instance" "no_snapshot" {
   612  	identifier = "tf-test-%s"
   613  
   614  	allocated_storage = 5
   615  	engine = "mysql"
   616  	engine_version = "5.6.21"
   617  	instance_class = "db.t1.micro"
   618  	name = "baz"
   619  	password = "barbarbarbar"
   620  	publicly_accessible = true
   621  	username = "foo"
   622      security_group_names = ["default"]
   623  	backup_retention_period = 1
   624  
   625  	parameter_group_name = "default.mysql5.6"
   626  
   627  	skip_final_snapshot = true
   628  	final_snapshot_identifier = "foobarbaz-test-terraform-final-snapshot-2"
   629  }
   630  `, acctest.RandString(5))
   631  }
   632  
   633  var testAccSnapshotInstanceConfig_enhancedMonitoring = `
   634  resource "aws_iam_role" "enhanced_policy_role" {
   635      name = "enhanced-monitoring-role"
   636      assume_role_policy = <<EOF
   637  {
   638    "Version": "2012-10-17",
   639    "Statement": [
   640      {
   641        "Sid": "",
   642        "Effect": "Allow",
   643        "Principal": {
   644          "Service": "monitoring.rds.amazonaws.com"
   645        },
   646        "Action": "sts:AssumeRole"
   647      }
   648    ]
   649  }
   650  EOF
   651  
   652  }
   653  
   654  resource "aws_iam_policy_attachment" "test-attach" {
   655      name = "enhanced-monitoring-attachment"
   656      roles = [
   657          "${aws_iam_role.enhanced_policy_role.name}",
   658      ]
   659  
   660      policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonRDSEnhancedMonitoringRole"
   661  }
   662  
   663  resource "aws_db_instance" "enhanced_monitoring" {
   664  	identifier = "foobarbaz-test-terraform-enhanced-monitoring"
   665  	depends_on = ["aws_iam_policy_attachment.test-attach"]
   666  
   667  	allocated_storage = 5
   668  	engine = "mysql"
   669  	engine_version = "5.6.21"
   670  	instance_class = "db.m3.medium"
   671  	name = "baz"
   672  	password = "barbarbarbar"
   673  	username = "foo"
   674  	backup_retention_period = 1
   675  
   676  	parameter_group_name = "default.mysql5.6"
   677  
   678  	monitoring_role_arn = "${aws_iam_role.enhanced_policy_role.arn}"
   679  	monitoring_interval = "5"
   680  
   681  	skip_final_snapshot = true
   682  }
   683  `
   684  
   685  func testAccSnapshotInstanceConfig_iopsUpdate(rName string, iops int) string {
   686  	return fmt.Sprintf(`
   687  resource "aws_db_instance" "bar" {
   688    identifier           = "mydb-rds-%s"
   689    engine               = "mysql"
   690    engine_version       = "5.6.23"
   691    instance_class       = "db.t2.micro"
   692    name                 = "mydb"
   693    username             = "foo"
   694    password             = "barbarbar"
   695    parameter_group_name = "default.mysql5.6"
   696  
   697    apply_immediately = true
   698  
   699    storage_type      = "io1"
   700    allocated_storage = 200
   701    iops              = %d
   702  }`, rName, iops)
   703  }