github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/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 { 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 resource.TestCheckResourceAttrSet("aws_db_instance.bar", "hosted_zone_id"), 50 ), 51 }, 52 }, 53 }) 54 } 55 56 func TestAccAWSDBInstance_namePrefix(t *testing.T) { 57 var v rds.DBInstance 58 59 resource.Test(t, resource.TestCase{ 60 PreCheck: func() { testAccPreCheck(t) }, 61 Providers: testAccProviders, 62 CheckDestroy: testAccCheckAWSDBInstanceDestroy, 63 Steps: []resource.TestStep{ 64 { 65 Config: testAccAWSDBInstanceConfig_namePrefix, 66 Check: resource.ComposeTestCheckFunc( 67 testAccCheckAWSDBInstanceExists("aws_db_instance.test", &v), 68 testAccCheckAWSDBInstanceAttributes(&v), 69 resource.TestMatchResourceAttr( 70 "aws_db_instance.test", "identifier", regexp.MustCompile("^tf-test-")), 71 ), 72 }, 73 }, 74 }) 75 } 76 77 func TestAccAWSDBInstance_generatedName(t *testing.T) { 78 var v rds.DBInstance 79 80 resource.Test(t, resource.TestCase{ 81 PreCheck: func() { testAccPreCheck(t) }, 82 Providers: testAccProviders, 83 CheckDestroy: testAccCheckAWSDBInstanceDestroy, 84 Steps: []resource.TestStep{ 85 { 86 Config: testAccAWSDBInstanceConfig_generatedName, 87 Check: resource.ComposeTestCheckFunc( 88 testAccCheckAWSDBInstanceExists("aws_db_instance.test", &v), 89 testAccCheckAWSDBInstanceAttributes(&v), 90 ), 91 }, 92 }, 93 }) 94 } 95 96 func TestAccAWSDBInstance_kmsKey(t *testing.T) { 97 var v rds.DBInstance 98 keyRegex := regexp.MustCompile("^arn:aws:kms:") 99 100 ri := rand.New(rand.NewSource(time.Now().UnixNano())).Int() 101 config := fmt.Sprintf(testAccAWSDBInstanceConfigKmsKeyId, ri) 102 103 resource.Test(t, resource.TestCase{ 104 PreCheck: func() { testAccPreCheck(t) }, 105 Providers: testAccProviders, 106 CheckDestroy: testAccCheckAWSDBInstanceDestroy, 107 Steps: []resource.TestStep{ 108 { 109 Config: config, 110 Check: resource.ComposeTestCheckFunc( 111 testAccCheckAWSDBInstanceExists("aws_db_instance.bar", &v), 112 testAccCheckAWSDBInstanceAttributes(&v), 113 resource.TestMatchResourceAttr( 114 "aws_db_instance.bar", "kms_key_id", keyRegex), 115 ), 116 }, 117 }, 118 }) 119 } 120 121 func TestAccAWSDBInstance_subnetGroup(t *testing.T) { 122 var v rds.DBInstance 123 rName := acctest.RandString(10) 124 125 resource.Test(t, resource.TestCase{ 126 PreCheck: func() { testAccPreCheck(t) }, 127 Providers: testAccProviders, 128 CheckDestroy: testAccCheckAWSDBInstanceDestroy, 129 Steps: []resource.TestStep{ 130 { 131 Config: testAccAWSDBInstanceConfigWithSubnetGroup(rName), 132 Check: resource.ComposeTestCheckFunc( 133 testAccCheckAWSDBInstanceExists("aws_db_instance.bar", &v), 134 resource.TestCheckResourceAttr( 135 "aws_db_instance.bar", "db_subnet_group_name", "foo-"+rName), 136 ), 137 }, 138 { 139 Config: testAccAWSDBInstanceConfigWithSubnetGroupUpdated(rName), 140 Check: resource.ComposeTestCheckFunc( 141 testAccCheckAWSDBInstanceExists("aws_db_instance.bar", &v), 142 resource.TestCheckResourceAttr( 143 "aws_db_instance.bar", "db_subnet_group_name", "bar-"+rName), 144 ), 145 }, 146 }, 147 }) 148 } 149 150 func TestAccAWSDBInstance_optionGroup(t *testing.T) { 151 var v rds.DBInstance 152 153 rName := fmt.Sprintf("tf-option-test-%d", acctest.RandInt()) 154 155 resource.Test(t, resource.TestCase{ 156 PreCheck: func() { testAccPreCheck(t) }, 157 Providers: testAccProviders, 158 CheckDestroy: testAccCheckAWSDBInstanceDestroy, 159 Steps: []resource.TestStep{ 160 { 161 Config: testAccAWSDBInstanceConfigWithOptionGroup(rName), 162 Check: resource.ComposeTestCheckFunc( 163 testAccCheckAWSDBInstanceExists("aws_db_instance.bar", &v), 164 testAccCheckAWSDBInstanceAttributes(&v), 165 resource.TestCheckResourceAttr( 166 "aws_db_instance.bar", "option_group_name", rName), 167 ), 168 }, 169 }, 170 }) 171 } 172 173 func TestAccAWSDBInstanceReplica(t *testing.T) { 174 var s, r rds.DBInstance 175 176 resource.Test(t, resource.TestCase{ 177 PreCheck: func() { testAccPreCheck(t) }, 178 Providers: testAccProviders, 179 CheckDestroy: testAccCheckAWSDBInstanceDestroy, 180 Steps: []resource.TestStep{ 181 { 182 Config: testAccReplicaInstanceConfig(rand.New(rand.NewSource(time.Now().UnixNano())).Int()), 183 Check: resource.ComposeTestCheckFunc( 184 testAccCheckAWSDBInstanceExists("aws_db_instance.bar", &s), 185 testAccCheckAWSDBInstanceExists("aws_db_instance.replica", &r), 186 testAccCheckAWSDBInstanceReplicaAttributes(&s, &r), 187 ), 188 }, 189 }, 190 }) 191 } 192 193 func TestAccAWSDBInstanceNoSnapshot(t *testing.T) { 194 var snap rds.DBInstance 195 196 resource.Test(t, resource.TestCase{ 197 PreCheck: func() { testAccPreCheck(t) }, 198 Providers: testAccProviders, 199 CheckDestroy: testAccCheckAWSDBInstanceNoSnapshot, 200 Steps: []resource.TestStep{ 201 { 202 Config: testAccSnapshotInstanceConfig(), 203 Check: resource.ComposeTestCheckFunc( 204 testAccCheckAWSDBInstanceExists("aws_db_instance.snapshot", &snap), 205 ), 206 }, 207 }, 208 }) 209 } 210 211 func TestAccAWSDBInstanceSnapshot(t *testing.T) { 212 var snap rds.DBInstance 213 rInt := acctest.RandInt() 214 215 resource.Test(t, resource.TestCase{ 216 PreCheck: func() { testAccPreCheck(t) }, 217 Providers: testAccProviders, 218 // testAccCheckAWSDBInstanceSnapshot verifies a database snapshot is 219 // created, and subequently deletes it 220 CheckDestroy: testAccCheckAWSDBInstanceSnapshot(rInt), 221 Steps: []resource.TestStep{ 222 { 223 Config: testAccSnapshotInstanceConfigWithSnapshot(rInt), 224 Check: resource.ComposeTestCheckFunc( 225 testAccCheckAWSDBInstanceExists("aws_db_instance.snapshot", &snap), 226 ), 227 }, 228 }, 229 }) 230 } 231 232 func TestAccAWSDBInstance_enhancedMonitoring(t *testing.T) { 233 var dbInstance rds.DBInstance 234 rName := acctest.RandString(5) 235 236 resource.Test(t, resource.TestCase{ 237 PreCheck: func() { testAccPreCheck(t) }, 238 Providers: testAccProviders, 239 CheckDestroy: testAccCheckAWSDBInstanceNoSnapshot, 240 Steps: []resource.TestStep{ 241 { 242 Config: testAccSnapshotInstanceConfig_enhancedMonitoring(rName), 243 Check: resource.ComposeTestCheckFunc( 244 testAccCheckAWSDBInstanceExists("aws_db_instance.enhanced_monitoring", &dbInstance), 245 resource.TestCheckResourceAttr( 246 "aws_db_instance.enhanced_monitoring", "monitoring_interval", "5"), 247 ), 248 }, 249 }, 250 }) 251 } 252 253 // Regression test for https://github.com/hashicorp/terraform/issues/3760 . 254 // We apply a plan, then change just the iops. If the apply succeeds, we 255 // consider this a pass, as before in 3760 the request would fail 256 func TestAccAWS_separate_DBInstance_iops_update(t *testing.T) { 257 var v rds.DBInstance 258 259 rName := acctest.RandString(5) 260 261 resource.Test(t, resource.TestCase{ 262 PreCheck: func() { testAccPreCheck(t) }, 263 Providers: testAccProviders, 264 CheckDestroy: testAccCheckAWSDBInstanceDestroy, 265 Steps: []resource.TestStep{ 266 { 267 Config: testAccSnapshotInstanceConfig_iopsUpdate(rName, 1000), 268 Check: resource.ComposeTestCheckFunc( 269 testAccCheckAWSDBInstanceExists("aws_db_instance.bar", &v), 270 testAccCheckAWSDBInstanceAttributes(&v), 271 ), 272 }, 273 274 { 275 Config: testAccSnapshotInstanceConfig_iopsUpdate(rName, 2000), 276 Check: resource.ComposeTestCheckFunc( 277 testAccCheckAWSDBInstanceExists("aws_db_instance.bar", &v), 278 testAccCheckAWSDBInstanceAttributes(&v), 279 ), 280 }, 281 }, 282 }) 283 } 284 285 func TestAccAWSDBInstance_portUpdate(t *testing.T) { 286 var v rds.DBInstance 287 288 rName := acctest.RandString(5) 289 290 resource.Test(t, resource.TestCase{ 291 PreCheck: func() { testAccPreCheck(t) }, 292 Providers: testAccProviders, 293 CheckDestroy: testAccCheckAWSDBInstanceDestroy, 294 Steps: []resource.TestStep{ 295 { 296 Config: testAccSnapshotInstanceConfig_mysqlPort(rName), 297 Check: resource.ComposeTestCheckFunc( 298 testAccCheckAWSDBInstanceExists("aws_db_instance.bar", &v), 299 resource.TestCheckResourceAttr( 300 "aws_db_instance.bar", "port", "3306"), 301 ), 302 }, 303 304 { 305 Config: testAccSnapshotInstanceConfig_updateMysqlPort(rName), 306 Check: resource.ComposeTestCheckFunc( 307 testAccCheckAWSDBInstanceExists("aws_db_instance.bar", &v), 308 resource.TestCheckResourceAttr( 309 "aws_db_instance.bar", "port", "3305"), 310 ), 311 }, 312 }, 313 }) 314 } 315 316 func TestAccAWSDBInstance_MSSQL_TZ(t *testing.T) { 317 var v rds.DBInstance 318 319 resource.Test(t, resource.TestCase{ 320 PreCheck: func() { testAccPreCheck(t) }, 321 Providers: testAccProviders, 322 CheckDestroy: testAccCheckAWSDBInstanceDestroy, 323 Steps: []resource.TestStep{ 324 { 325 Config: testAccAWSDBMSSQL_timezone, 326 Check: resource.ComposeTestCheckFunc( 327 testAccCheckAWSDBInstanceExists("aws_db_instance.mssql", &v), 328 testAccCheckAWSDBInstanceAttributes_MSSQL(&v, ""), 329 resource.TestCheckResourceAttr( 330 "aws_db_instance.mssql", "allocated_storage", "20"), 331 resource.TestCheckResourceAttr( 332 "aws_db_instance.mssql", "engine", "sqlserver-ex"), 333 ), 334 }, 335 336 { 337 Config: testAccAWSDBMSSQL_timezone_AKST, 338 Check: resource.ComposeTestCheckFunc( 339 testAccCheckAWSDBInstanceExists("aws_db_instance.mssql", &v), 340 testAccCheckAWSDBInstanceAttributes_MSSQL(&v, "Alaskan Standard Time"), 341 resource.TestCheckResourceAttr( 342 "aws_db_instance.mssql", "allocated_storage", "20"), 343 resource.TestCheckResourceAttr( 344 "aws_db_instance.mssql", "engine", "sqlserver-ex"), 345 ), 346 }, 347 }, 348 }) 349 } 350 351 func TestAccAWSDBInstance_MinorVersion(t *testing.T) { 352 var v rds.DBInstance 353 354 resource.Test(t, resource.TestCase{ 355 PreCheck: func() { testAccPreCheck(t) }, 356 Providers: testAccProviders, 357 CheckDestroy: testAccCheckAWSDBInstanceDestroy, 358 Steps: []resource.TestStep{ 359 { 360 Config: testAccAWSDBInstanceConfigAutoMinorVersion, 361 Check: resource.ComposeTestCheckFunc( 362 testAccCheckAWSDBInstanceExists("aws_db_instance.bar", &v), 363 ), 364 }, 365 }, 366 }) 367 } 368 369 // See https://github.com/hashicorp/terraform/issues/11881 370 func TestAccAWSDBInstance_diffSuppressInitialState(t *testing.T) { 371 var v rds.DBInstance 372 rInt := acctest.RandInt() 373 374 resource.Test(t, resource.TestCase{ 375 PreCheck: func() { testAccPreCheck(t) }, 376 Providers: testAccProviders, 377 CheckDestroy: testAccCheckAWSDBInstanceDestroy, 378 Steps: []resource.TestStep{ 379 { 380 Config: testAccAWSDBInstanceConfigSuppressInitialState(rInt), 381 Check: resource.ComposeTestCheckFunc( 382 testAccCheckAWSDBInstanceExists("aws_db_instance.bar", &v), 383 ), 384 }, 385 }, 386 }) 387 } 388 389 func testAccCheckAWSDBInstanceDestroy(s *terraform.State) error { 390 conn := testAccProvider.Meta().(*AWSClient).rdsconn 391 392 for _, rs := range s.RootModule().Resources { 393 if rs.Type != "aws_db_instance" { 394 continue 395 } 396 397 // Try to find the Group 398 var err error 399 resp, err := conn.DescribeDBInstances( 400 &rds.DescribeDBInstancesInput{ 401 DBInstanceIdentifier: aws.String(rs.Primary.ID), 402 }) 403 404 if ae, ok := err.(awserr.Error); ok && ae.Code() == "DBInstanceNotFound" { 405 continue 406 } 407 408 if err == nil { 409 if len(resp.DBInstances) != 0 && 410 *resp.DBInstances[0].DBInstanceIdentifier == rs.Primary.ID { 411 return fmt.Errorf("DB Instance still exists") 412 } 413 } 414 415 // Verify the error 416 newerr, ok := err.(awserr.Error) 417 if !ok { 418 return err 419 } 420 if newerr.Code() != "DBInstanceNotFound" { 421 return err 422 } 423 } 424 425 return nil 426 } 427 428 func testAccCheckAWSDBInstanceAttributes(v *rds.DBInstance) resource.TestCheckFunc { 429 return func(s *terraform.State) error { 430 431 if *v.Engine != "mysql" { 432 return fmt.Errorf("bad engine: %#v", *v.Engine) 433 } 434 435 if *v.EngineVersion == "" { 436 return fmt.Errorf("bad engine_version: %#v", *v.EngineVersion) 437 } 438 439 if *v.BackupRetentionPeriod != 0 { 440 return fmt.Errorf("bad backup_retention_period: %#v", *v.BackupRetentionPeriod) 441 } 442 443 return nil 444 } 445 } 446 447 func testAccCheckAWSDBInstanceAttributes_MSSQL(v *rds.DBInstance, tz string) resource.TestCheckFunc { 448 return func(s *terraform.State) error { 449 450 if *v.Engine != "sqlserver-ex" { 451 return fmt.Errorf("bad engine: %#v", *v.Engine) 452 } 453 454 rtz := "" 455 if v.Timezone != nil { 456 rtz = *v.Timezone 457 } 458 459 if tz != rtz { 460 return fmt.Errorf("Expected (%s) Timezone for MSSQL test, got (%s)", tz, rtz) 461 } 462 463 return nil 464 } 465 } 466 467 func testAccCheckAWSDBInstanceReplicaAttributes(source, replica *rds.DBInstance) resource.TestCheckFunc { 468 return func(s *terraform.State) error { 469 470 if replica.ReadReplicaSourceDBInstanceIdentifier != nil && *replica.ReadReplicaSourceDBInstanceIdentifier != *source.DBInstanceIdentifier { 471 return fmt.Errorf("bad source identifier for replica, expected: '%s', got: '%s'", *source.DBInstanceIdentifier, *replica.ReadReplicaSourceDBInstanceIdentifier) 472 } 473 474 return nil 475 } 476 } 477 478 func testAccCheckAWSDBInstanceSnapshot(rInt int) resource.TestCheckFunc { 479 return func(s *terraform.State) error { 480 for _, rs := range s.RootModule().Resources { 481 if rs.Type != "aws_db_instance" { 482 continue 483 } 484 485 awsClient := testAccProvider.Meta().(*AWSClient) 486 conn := awsClient.rdsconn 487 488 var err error 489 log.Printf("[INFO] Trying to locate the DBInstance Final Snapshot") 490 snapshot_identifier := fmt.Sprintf("foobarbaz-test-terraform-final-snapshot-%d", rInt) 491 _, snapErr := conn.DescribeDBSnapshots( 492 &rds.DescribeDBSnapshotsInput{ 493 DBSnapshotIdentifier: aws.String(snapshot_identifier), 494 }) 495 496 if snapErr != nil { 497 newerr, _ := snapErr.(awserr.Error) 498 if newerr.Code() == "DBSnapshotNotFound" { 499 return fmt.Errorf("Snapshot %s not found", snapshot_identifier) 500 } 501 } else { // snapshot was found, 502 // verify we have the tags copied to the snapshot 503 instanceARN, err := buildRDSARN(snapshot_identifier, testAccProvider.Meta().(*AWSClient).partition, testAccProvider.Meta().(*AWSClient).accountid, testAccProvider.Meta().(*AWSClient).region) 504 // tags have a different ARN, just swapping :db: for :snapshot: 505 tagsARN := strings.Replace(instanceARN, ":db:", ":snapshot:", 1) 506 if err != nil { 507 return fmt.Errorf("Error building ARN for tags check with ARN (%s): %s", tagsARN, err) 508 } 509 resp, err := conn.ListTagsForResource(&rds.ListTagsForResourceInput{ 510 ResourceName: aws.String(tagsARN), 511 }) 512 if err != nil { 513 return fmt.Errorf("Error retrieving tags for ARN (%s): %s", tagsARN, err) 514 } 515 516 if resp.TagList == nil || len(resp.TagList) == 0 { 517 return fmt.Errorf("Tag list is nil or zero: %s", resp.TagList) 518 } 519 520 var found bool 521 for _, t := range resp.TagList { 522 if *t.Key == "Name" && *t.Value == "tf-tags-db" { 523 found = true 524 } 525 } 526 if !found { 527 return fmt.Errorf("Expected to find tag Name (%s), but wasn't found. Tags: %s", "tf-tags-db", resp.TagList) 528 } 529 // end tag search 530 531 log.Printf("[INFO] Deleting the Snapshot %s", snapshot_identifier) 532 _, snapDeleteErr := conn.DeleteDBSnapshot( 533 &rds.DeleteDBSnapshotInput{ 534 DBSnapshotIdentifier: aws.String(snapshot_identifier), 535 }) 536 if snapDeleteErr != nil { 537 return err 538 } 539 } // end snapshot was found 540 541 resp, err := conn.DescribeDBInstances( 542 &rds.DescribeDBInstancesInput{ 543 DBInstanceIdentifier: aws.String(rs.Primary.ID), 544 }) 545 546 if err != nil { 547 newerr, _ := err.(awserr.Error) 548 if newerr.Code() != "DBInstanceNotFound" { 549 return err 550 } 551 552 } else { 553 if len(resp.DBInstances) != 0 && 554 *resp.DBInstances[0].DBInstanceIdentifier == rs.Primary.ID { 555 return fmt.Errorf("DB Instance still exists") 556 } 557 } 558 } 559 560 return nil 561 } 562 } 563 564 func testAccCheckAWSDBInstanceNoSnapshot(s *terraform.State) error { 565 conn := testAccProvider.Meta().(*AWSClient).rdsconn 566 567 for _, rs := range s.RootModule().Resources { 568 if rs.Type != "aws_db_instance" { 569 continue 570 } 571 572 var err error 573 resp, err := conn.DescribeDBInstances( 574 &rds.DescribeDBInstancesInput{ 575 DBInstanceIdentifier: aws.String(rs.Primary.ID), 576 }) 577 578 if err != nil { 579 newerr, _ := err.(awserr.Error) 580 if newerr.Code() != "DBInstanceNotFound" { 581 return err 582 } 583 584 } else { 585 if len(resp.DBInstances) != 0 && 586 *resp.DBInstances[0].DBInstanceIdentifier == rs.Primary.ID { 587 return fmt.Errorf("DB Instance still exists") 588 } 589 } 590 591 snapshot_identifier := "foobarbaz-test-terraform-final-snapshot-2" 592 _, snapErr := conn.DescribeDBSnapshots( 593 &rds.DescribeDBSnapshotsInput{ 594 DBSnapshotIdentifier: aws.String(snapshot_identifier), 595 }) 596 597 if snapErr != nil { 598 newerr, _ := snapErr.(awserr.Error) 599 if newerr.Code() != "DBSnapshotNotFound" { 600 return fmt.Errorf("Snapshot %s found and it shouldn't have been", snapshot_identifier) 601 } 602 } 603 } 604 605 return nil 606 } 607 608 func testAccCheckAWSDBInstanceExists(n string, v *rds.DBInstance) resource.TestCheckFunc { 609 return func(s *terraform.State) error { 610 rs, ok := s.RootModule().Resources[n] 611 if !ok { 612 return fmt.Errorf("Not found: %s", n) 613 } 614 615 if rs.Primary.ID == "" { 616 return fmt.Errorf("No DB Instance ID is set") 617 } 618 619 conn := testAccProvider.Meta().(*AWSClient).rdsconn 620 621 opts := rds.DescribeDBInstancesInput{ 622 DBInstanceIdentifier: aws.String(rs.Primary.ID), 623 } 624 625 resp, err := conn.DescribeDBInstances(&opts) 626 627 if err != nil { 628 return err 629 } 630 631 if len(resp.DBInstances) != 1 || 632 *resp.DBInstances[0].DBInstanceIdentifier != rs.Primary.ID { 633 return fmt.Errorf("DB Instance not found") 634 } 635 636 *v = *resp.DBInstances[0] 637 638 return nil 639 } 640 } 641 642 // Database names cannot collide, and deletion takes so long, that making the 643 // name a bit random helps so able we can kill a test that's just waiting for a 644 // delete and not be blocked on kicking off another one. 645 var testAccAWSDBInstanceConfig = ` 646 resource "aws_db_instance" "bar" { 647 allocated_storage = 10 648 engine = "MySQL" 649 engine_version = "5.6.21" 650 instance_class = "db.t1.micro" 651 name = "baz" 652 password = "barbarbarbar" 653 username = "foo" 654 655 656 # Maintenance Window is stored in lower case in the API, though not strictly 657 # documented. Terraform will downcase this to match (as opposed to throw a 658 # validation error). 659 maintenance_window = "Fri:09:00-Fri:09:30" 660 skip_final_snapshot = true 661 662 backup_retention_period = 0 663 664 parameter_group_name = "default.mysql5.6" 665 666 timeouts { 667 create = "30m" 668 } 669 }` 670 671 const testAccAWSDBInstanceConfig_namePrefix = ` 672 resource "aws_db_instance" "test" { 673 allocated_storage = 10 674 engine = "MySQL" 675 identifier_prefix = "tf-test-" 676 instance_class = "db.t1.micro" 677 password = "password" 678 username = "root" 679 publicly_accessible = true 680 skip_final_snapshot = true 681 682 timeouts { 683 create = "30m" 684 } 685 }` 686 687 const testAccAWSDBInstanceConfig_generatedName = ` 688 resource "aws_db_instance" "test" { 689 allocated_storage = 10 690 engine = "MySQL" 691 instance_class = "db.t1.micro" 692 password = "password" 693 username = "root" 694 publicly_accessible = true 695 skip_final_snapshot = true 696 697 timeouts { 698 create = "30m" 699 } 700 }` 701 702 var testAccAWSDBInstanceConfigKmsKeyId = ` 703 resource "aws_kms_key" "foo" { 704 description = "Terraform acc test %s" 705 policy = <<POLICY 706 { 707 "Version": "2012-10-17", 708 "Id": "kms-tf-1", 709 "Statement": [ 710 { 711 "Sid": "Enable IAM User Permissions", 712 "Effect": "Allow", 713 "Principal": { 714 "AWS": "*" 715 }, 716 "Action": "kms:*", 717 "Resource": "*" 718 } 719 ] 720 } 721 POLICY 722 } 723 724 resource "aws_db_instance" "bar" { 725 allocated_storage = 10 726 engine = "MySQL" 727 engine_version = "5.6.21" 728 instance_class = "db.m3.medium" 729 name = "baz" 730 password = "barbarbarbar" 731 username = "foo" 732 733 734 # Maintenance Window is stored in lower case in the API, though not strictly 735 # documented. Terraform will downcase this to match (as opposed to throw a 736 # validation error). 737 maintenance_window = "Fri:09:00-Fri:09:30" 738 739 backup_retention_period = 0 740 storage_encrypted = true 741 kms_key_id = "${aws_kms_key.foo.arn}" 742 743 skip_final_snapshot = true 744 745 parameter_group_name = "default.mysql5.6" 746 } 747 ` 748 749 func testAccAWSDBInstanceConfigWithOptionGroup(rName string) string { 750 return fmt.Sprintf(` 751 resource "aws_db_option_group" "bar" { 752 name = "%s" 753 option_group_description = "Test option group for terraform" 754 engine_name = "mysql" 755 major_engine_version = "5.6" 756 } 757 758 resource "aws_db_instance" "bar" { 759 identifier = "foobarbaz-test-terraform-%d" 760 761 allocated_storage = 10 762 engine = "MySQL" 763 instance_class = "db.m1.small" 764 name = "baz" 765 password = "barbarbarbar" 766 username = "foo" 767 768 backup_retention_period = 0 769 skip_final_snapshot = true 770 771 parameter_group_name = "default.mysql5.6" 772 option_group_name = "${aws_db_option_group.bar.name}" 773 }`, rName, acctest.RandInt()) 774 } 775 776 func testAccReplicaInstanceConfig(val int) string { 777 return fmt.Sprintf(` 778 resource "aws_db_instance" "bar" { 779 identifier = "foobarbaz-test-terraform-%d" 780 781 allocated_storage = 5 782 engine = "mysql" 783 engine_version = "5.6.21" 784 instance_class = "db.t1.micro" 785 name = "baz" 786 password = "barbarbarbar" 787 username = "foo" 788 789 backup_retention_period = 1 790 skip_final_snapshot = true 791 792 parameter_group_name = "default.mysql5.6" 793 } 794 795 resource "aws_db_instance" "replica" { 796 identifier = "tf-replica-db-%d" 797 backup_retention_period = 0 798 replicate_source_db = "${aws_db_instance.bar.identifier}" 799 allocated_storage = "${aws_db_instance.bar.allocated_storage}" 800 engine = "${aws_db_instance.bar.engine}" 801 engine_version = "${aws_db_instance.bar.engine_version}" 802 instance_class = "${aws_db_instance.bar.instance_class}" 803 password = "${aws_db_instance.bar.password}" 804 username = "${aws_db_instance.bar.username}" 805 skip_final_snapshot = true 806 tags { 807 Name = "tf-replica-db" 808 } 809 } 810 `, val, val) 811 } 812 813 func testAccSnapshotInstanceConfig() string { 814 return fmt.Sprintf(` 815 provider "aws" { 816 region = "us-east-1" 817 } 818 resource "aws_db_instance" "snapshot" { 819 identifier = "tf-test-%d" 820 821 allocated_storage = 5 822 engine = "mysql" 823 engine_version = "5.6.21" 824 instance_class = "db.t1.micro" 825 name = "baz" 826 password = "barbarbarbar" 827 username = "foo" 828 security_group_names = ["default"] 829 backup_retention_period = 1 830 831 publicly_accessible = true 832 833 parameter_group_name = "default.mysql5.6" 834 835 skip_final_snapshot = true 836 final_snapshot_identifier = "foobarbaz-test-terraform-final-snapshot-1" 837 }`, acctest.RandInt()) 838 } 839 840 func testAccSnapshotInstanceConfigWithSnapshot(rInt int) string { 841 return fmt.Sprintf(` 842 provider "aws" { 843 region = "us-east-1" 844 } 845 resource "aws_db_instance" "snapshot" { 846 identifier = "tf-snapshot-%d" 847 848 allocated_storage = 5 849 engine = "mysql" 850 engine_version = "5.6.21" 851 instance_class = "db.t1.micro" 852 name = "baz" 853 password = "barbarbarbar" 854 publicly_accessible = true 855 username = "foo" 856 security_group_names = ["default"] 857 backup_retention_period = 1 858 859 parameter_group_name = "default.mysql5.6" 860 861 copy_tags_to_snapshot = true 862 final_snapshot_identifier = "foobarbaz-test-terraform-final-snapshot-%d" 863 tags { 864 Name = "tf-tags-db" 865 } 866 } 867 `, rInt, rInt) 868 } 869 870 func testAccSnapshotInstanceConfig_enhancedMonitoring(rName string) string { 871 return fmt.Sprintf(` 872 resource "aws_iam_role" "enhanced_policy_role" { 873 name = "enhanced-monitoring-role-%s" 874 assume_role_policy = <<EOF 875 { 876 "Version": "2012-10-17", 877 "Statement": [ 878 { 879 "Sid": "", 880 "Effect": "Allow", 881 "Principal": { 882 "Service": "monitoring.rds.amazonaws.com" 883 }, 884 "Action": "sts:AssumeRole" 885 } 886 ] 887 } 888 EOF 889 890 } 891 892 resource "aws_iam_policy_attachment" "test-attach" { 893 name = "enhanced-monitoring-attachment" 894 roles = [ 895 "${aws_iam_role.enhanced_policy_role.name}", 896 ] 897 898 policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonRDSEnhancedMonitoringRole" 899 } 900 901 resource "aws_db_instance" "enhanced_monitoring" { 902 identifier = "foobarbaz-enhanced-monitoring-%s" 903 depends_on = ["aws_iam_policy_attachment.test-attach"] 904 905 allocated_storage = 5 906 engine = "mysql" 907 engine_version = "5.6.21" 908 instance_class = "db.m3.medium" 909 name = "baz" 910 password = "barbarbarbar" 911 username = "foo" 912 backup_retention_period = 1 913 914 parameter_group_name = "default.mysql5.6" 915 916 monitoring_role_arn = "${aws_iam_role.enhanced_policy_role.arn}" 917 monitoring_interval = "5" 918 919 skip_final_snapshot = true 920 }`, rName, rName) 921 } 922 923 func testAccSnapshotInstanceConfig_iopsUpdate(rName string, iops int) string { 924 return fmt.Sprintf(` 925 resource "aws_db_instance" "bar" { 926 identifier = "mydb-rds-%s" 927 engine = "mysql" 928 engine_version = "5.6.23" 929 instance_class = "db.t2.micro" 930 name = "mydb" 931 username = "foo" 932 password = "barbarbar" 933 parameter_group_name = "default.mysql5.6" 934 skip_final_snapshot = true 935 936 apply_immediately = true 937 938 storage_type = "io1" 939 allocated_storage = 200 940 iops = %d 941 }`, rName, iops) 942 } 943 944 func testAccSnapshotInstanceConfig_mysqlPort(rName string) string { 945 return fmt.Sprintf(` 946 resource "aws_db_instance" "bar" { 947 identifier = "mydb-rds-%s" 948 engine = "mysql" 949 engine_version = "5.6.23" 950 instance_class = "db.t2.micro" 951 name = "mydb" 952 username = "foo" 953 password = "barbarbar" 954 parameter_group_name = "default.mysql5.6" 955 port = 3306 956 allocated_storage = 10 957 skip_final_snapshot = true 958 959 apply_immediately = true 960 }`, rName) 961 } 962 963 func testAccSnapshotInstanceConfig_updateMysqlPort(rName string) string { 964 return fmt.Sprintf(` 965 resource "aws_db_instance" "bar" { 966 identifier = "mydb-rds-%s" 967 engine = "mysql" 968 engine_version = "5.6.23" 969 instance_class = "db.t2.micro" 970 name = "mydb" 971 username = "foo" 972 password = "barbarbar" 973 parameter_group_name = "default.mysql5.6" 974 port = 3305 975 allocated_storage = 10 976 skip_final_snapshot = true 977 978 apply_immediately = true 979 }`, rName) 980 } 981 982 func testAccAWSDBInstanceConfigWithSubnetGroup(rName string) string { 983 return fmt.Sprintf(` 984 resource "aws_vpc" "foo" { 985 cidr_block = "10.1.0.0/16" 986 } 987 988 resource "aws_subnet" "foo" { 989 cidr_block = "10.1.1.0/24" 990 availability_zone = "us-west-2a" 991 vpc_id = "${aws_vpc.foo.id}" 992 tags { 993 Name = "tf-dbsubnet-test-1" 994 } 995 } 996 997 resource "aws_subnet" "bar" { 998 cidr_block = "10.1.2.0/24" 999 availability_zone = "us-west-2b" 1000 vpc_id = "${aws_vpc.foo.id}" 1001 tags { 1002 Name = "tf-dbsubnet-test-2" 1003 } 1004 } 1005 1006 resource "aws_db_subnet_group" "foo" { 1007 name = "foo-%s" 1008 subnet_ids = ["${aws_subnet.foo.id}", "${aws_subnet.bar.id}"] 1009 tags { 1010 Name = "tf-dbsubnet-group-test" 1011 } 1012 } 1013 1014 resource "aws_db_instance" "bar" { 1015 identifier = "mydb-rds-%s" 1016 engine = "mysql" 1017 engine_version = "5.6.23" 1018 instance_class = "db.t2.micro" 1019 name = "mydb" 1020 username = "foo" 1021 password = "barbarbar" 1022 parameter_group_name = "default.mysql5.6" 1023 db_subnet_group_name = "${aws_db_subnet_group.foo.name}" 1024 port = 3305 1025 allocated_storage = 10 1026 skip_final_snapshot = true 1027 1028 backup_retention_period = 0 1029 apply_immediately = true 1030 }`, rName, rName) 1031 } 1032 1033 func testAccAWSDBInstanceConfigWithSubnetGroupUpdated(rName string) string { 1034 return fmt.Sprintf(` 1035 resource "aws_vpc" "foo" { 1036 cidr_block = "10.1.0.0/16" 1037 } 1038 1039 resource "aws_vpc" "bar" { 1040 cidr_block = "10.10.0.0/16" 1041 } 1042 1043 resource "aws_subnet" "foo" { 1044 cidr_block = "10.1.1.0/24" 1045 availability_zone = "us-west-2a" 1046 vpc_id = "${aws_vpc.foo.id}" 1047 tags { 1048 Name = "tf-dbsubnet-test-1" 1049 } 1050 } 1051 1052 resource "aws_subnet" "bar" { 1053 cidr_block = "10.1.2.0/24" 1054 availability_zone = "us-west-2b" 1055 vpc_id = "${aws_vpc.foo.id}" 1056 tags { 1057 Name = "tf-dbsubnet-test-2" 1058 } 1059 } 1060 1061 resource "aws_subnet" "test" { 1062 cidr_block = "10.10.3.0/24" 1063 availability_zone = "us-west-2b" 1064 vpc_id = "${aws_vpc.bar.id}" 1065 tags { 1066 Name = "tf-dbsubnet-test-3" 1067 } 1068 } 1069 1070 resource "aws_subnet" "another_test" { 1071 cidr_block = "10.10.4.0/24" 1072 availability_zone = "us-west-2a" 1073 vpc_id = "${aws_vpc.bar.id}" 1074 tags { 1075 Name = "tf-dbsubnet-test-4" 1076 } 1077 } 1078 1079 resource "aws_db_subnet_group" "foo" { 1080 name = "foo-%s" 1081 subnet_ids = ["${aws_subnet.foo.id}", "${aws_subnet.bar.id}"] 1082 tags { 1083 Name = "tf-dbsubnet-group-test" 1084 } 1085 } 1086 1087 resource "aws_db_subnet_group" "bar" { 1088 name = "bar-%s" 1089 subnet_ids = ["${aws_subnet.test.id}", "${aws_subnet.another_test.id}"] 1090 tags { 1091 Name = "tf-dbsubnet-group-test-updated" 1092 } 1093 } 1094 1095 resource "aws_db_instance" "bar" { 1096 identifier = "mydb-rds-%s" 1097 engine = "mysql" 1098 engine_version = "5.6.23" 1099 instance_class = "db.t2.micro" 1100 name = "mydb" 1101 username = "foo" 1102 password = "barbarbar" 1103 parameter_group_name = "default.mysql5.6" 1104 db_subnet_group_name = "${aws_db_subnet_group.bar.name}" 1105 port = 3305 1106 allocated_storage = 10 1107 skip_final_snapshot = true 1108 1109 backup_retention_period = 0 1110 1111 apply_immediately = true 1112 }`, rName, rName, rName) 1113 } 1114 1115 const testAccAWSDBMSSQL_timezone = ` 1116 provider "aws" { 1117 region = "us-west-2" 1118 } 1119 1120 resource "aws_vpc" "foo" { 1121 cidr_block = "10.1.0.0/16" 1122 enable_dns_hostnames = true 1123 tags { 1124 Name = "tf-rds-mssql-timezone-test" 1125 } 1126 } 1127 1128 resource "aws_db_subnet_group" "rds_one" { 1129 name = "rds_one_db" 1130 description = "db subnets for rds_one" 1131 1132 subnet_ids = ["${aws_subnet.main.id}", "${aws_subnet.other.id}"] 1133 } 1134 1135 resource "aws_subnet" "main" { 1136 vpc_id = "${aws_vpc.foo.id}" 1137 availability_zone = "us-west-2a" 1138 cidr_block = "10.1.1.0/24" 1139 } 1140 1141 resource "aws_subnet" "other" { 1142 vpc_id = "${aws_vpc.foo.id}" 1143 availability_zone = "us-west-2b" 1144 cidr_block = "10.1.2.0/24" 1145 } 1146 1147 resource "aws_db_instance" "mssql" { 1148 #identifier = "tf-test-mssql" 1149 1150 db_subnet_group_name = "${aws_db_subnet_group.rds_one.name}" 1151 1152 instance_class = "db.t2.micro" 1153 allocated_storage = 20 1154 username = "somecrazyusername" 1155 password = "somecrazypassword" 1156 engine = "sqlserver-ex" 1157 backup_retention_period = 0 1158 skip_final_snapshot = true 1159 1160 #publicly_accessible = true 1161 1162 vpc_security_group_ids = ["${aws_security_group.rds-mssql.id}"] 1163 } 1164 1165 resource "aws_security_group" "rds-mssql" { 1166 name = "tf-rds-mssql-test" 1167 1168 description = "TF Testing" 1169 vpc_id = "${aws_vpc.foo.id}" 1170 } 1171 1172 resource "aws_security_group_rule" "rds-mssql-1" { 1173 type = "egress" 1174 from_port = 0 1175 to_port = 0 1176 protocol = "-1" 1177 cidr_blocks = ["0.0.0.0/0"] 1178 1179 security_group_id = "${aws_security_group.rds-mssql.id}" 1180 } 1181 ` 1182 1183 const testAccAWSDBMSSQL_timezone_AKST = ` 1184 provider "aws" { 1185 region = "us-west-2" 1186 } 1187 1188 resource "aws_vpc" "foo" { 1189 cidr_block = "10.1.0.0/16" 1190 enable_dns_hostnames = true 1191 tags { 1192 Name = "tf-rds-mssql-timezone-test" 1193 } 1194 } 1195 1196 resource "aws_db_subnet_group" "rds_one" { 1197 name = "rds_one_db" 1198 description = "db subnets for rds_one" 1199 1200 subnet_ids = ["${aws_subnet.main.id}", "${aws_subnet.other.id}"] 1201 } 1202 1203 resource "aws_subnet" "main" { 1204 vpc_id = "${aws_vpc.foo.id}" 1205 availability_zone = "us-west-2a" 1206 cidr_block = "10.1.1.0/24" 1207 } 1208 1209 resource "aws_subnet" "other" { 1210 vpc_id = "${aws_vpc.foo.id}" 1211 availability_zone = "us-west-2b" 1212 cidr_block = "10.1.2.0/24" 1213 } 1214 1215 resource "aws_db_instance" "mssql" { 1216 #identifier = "tf-test-mssql" 1217 1218 db_subnet_group_name = "${aws_db_subnet_group.rds_one.name}" 1219 1220 instance_class = "db.t2.micro" 1221 allocated_storage = 20 1222 username = "somecrazyusername" 1223 password = "somecrazypassword" 1224 engine = "sqlserver-ex" 1225 backup_retention_period = 0 1226 skip_final_snapshot = true 1227 1228 #publicly_accessible = true 1229 1230 vpc_security_group_ids = ["${aws_security_group.rds-mssql.id}"] 1231 timezone = "Alaskan Standard Time" 1232 } 1233 1234 resource "aws_security_group" "rds-mssql" { 1235 name = "tf-rds-mssql-test" 1236 1237 description = "TF Testing" 1238 vpc_id = "${aws_vpc.foo.id}" 1239 } 1240 1241 resource "aws_security_group_rule" "rds-mssql-1" { 1242 type = "egress" 1243 from_port = 0 1244 to_port = 0 1245 protocol = "-1" 1246 cidr_blocks = ["0.0.0.0/0"] 1247 1248 security_group_id = "${aws_security_group.rds-mssql.id}" 1249 } 1250 ` 1251 1252 var testAccAWSDBInstanceConfigAutoMinorVersion = fmt.Sprintf(` 1253 resource "aws_db_instance" "bar" { 1254 identifier = "foobarbaz-test-terraform-%d" 1255 allocated_storage = 10 1256 engine = "MySQL" 1257 engine_version = "5.6" 1258 instance_class = "db.t1.micro" 1259 name = "baz" 1260 password = "barbarbarbar" 1261 username = "foo" 1262 skip_final_snapshot = true 1263 } 1264 `, acctest.RandInt()) 1265 1266 func testAccAWSDBInstanceConfigSuppressInitialState(rInt int) string { 1267 return fmt.Sprintf(` 1268 resource "aws_db_instance" "bar" { 1269 identifier = "foobarbaz-test-terraform-%d" 1270 allocated_storage = 10 1271 engine = "MySQL" 1272 instance_class = "db.t1.micro" 1273 name = "baz" 1274 password = "barbarbarbar" 1275 username = "foo" 1276 skip_final_snapshot = true 1277 } 1278 1279 data "template_file" "test" { 1280 template = "" 1281 vars = { 1282 test_var = "${aws_db_instance.bar.engine_version}" 1283 } 1284 } 1285 `, rInt) 1286 }