github.com/acm1/terraform@v0.6.2-0.20150729164239-1f314444f45c/builtin/providers/aws/resource_aws_instance_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "reflect" 6 "testing" 7 8 "github.com/aws/aws-sdk-go/aws" 9 "github.com/aws/aws-sdk-go/aws/awserr" 10 "github.com/aws/aws-sdk-go/service/ec2" 11 "github.com/hashicorp/terraform/helper/resource" 12 "github.com/hashicorp/terraform/helper/schema" 13 "github.com/hashicorp/terraform/terraform" 14 ) 15 16 func TestAccAWSInstance_basic(t *testing.T) { 17 var v ec2.Instance 18 var vol *ec2.Volume 19 20 testCheck := func(*terraform.State) error { 21 if *v.Placement.AvailabilityZone != "us-west-2a" { 22 return fmt.Errorf("bad availability zone: %#v", *v.Placement.AvailabilityZone) 23 } 24 25 if len(v.SecurityGroups) == 0 { 26 return fmt.Errorf("no security groups: %#v", v.SecurityGroups) 27 } 28 if *v.SecurityGroups[0].GroupName != "tf_test_foo" { 29 return fmt.Errorf("no security groups: %#v", v.SecurityGroups) 30 } 31 32 return nil 33 } 34 35 resource.Test(t, resource.TestCase{ 36 PreCheck: func() { testAccPreCheck(t) }, 37 Providers: testAccProviders, 38 CheckDestroy: testAccCheckInstanceDestroy, 39 Steps: []resource.TestStep{ 40 // Create a volume to cover #1249 41 resource.TestStep{ 42 // Need a resource in this config so the provisioner will be available 43 Config: testAccInstanceConfig_pre, 44 Check: func(*terraform.State) error { 45 conn := testAccProvider.Meta().(*AWSClient).ec2conn 46 var err error 47 vol, err = conn.CreateVolume(&ec2.CreateVolumeInput{ 48 AvailabilityZone: aws.String("us-west-2a"), 49 Size: aws.Int64(int64(5)), 50 }) 51 return err 52 }, 53 }, 54 55 resource.TestStep{ 56 Config: testAccInstanceConfig, 57 Check: resource.ComposeTestCheckFunc( 58 testAccCheckInstanceExists( 59 "aws_instance.foo", &v), 60 testCheck, 61 resource.TestCheckResourceAttr( 62 "aws_instance.foo", 63 "user_data", 64 "3dc39dda39be1205215e776bad998da361a5955d"), 65 resource.TestCheckResourceAttr( 66 "aws_instance.foo", "ebs_block_device.#", "0"), 67 ), 68 }, 69 70 // We repeat the exact same test so that we can be sure 71 // that the user data hash stuff is working without generating 72 // an incorrect diff. 73 resource.TestStep{ 74 Config: testAccInstanceConfig, 75 Check: resource.ComposeTestCheckFunc( 76 testAccCheckInstanceExists( 77 "aws_instance.foo", &v), 78 testCheck, 79 resource.TestCheckResourceAttr( 80 "aws_instance.foo", 81 "user_data", 82 "3dc39dda39be1205215e776bad998da361a5955d"), 83 resource.TestCheckResourceAttr( 84 "aws_instance.foo", "ebs_block_device.#", "0"), 85 ), 86 }, 87 88 // Clean up volume created above 89 resource.TestStep{ 90 Config: testAccInstanceConfig, 91 Check: func(*terraform.State) error { 92 conn := testAccProvider.Meta().(*AWSClient).ec2conn 93 _, err := conn.DeleteVolume(&ec2.DeleteVolumeInput{VolumeID: vol.VolumeID}) 94 return err 95 }, 96 }, 97 }, 98 }) 99 } 100 101 func TestAccAWSInstance_blockDevices(t *testing.T) { 102 var v ec2.Instance 103 104 testCheck := func() resource.TestCheckFunc { 105 return func(*terraform.State) error { 106 107 // Map out the block devices by name, which should be unique. 108 blockDevices := make(map[string]*ec2.InstanceBlockDeviceMapping) 109 for _, blockDevice := range v.BlockDeviceMappings { 110 blockDevices[*blockDevice.DeviceName] = blockDevice 111 } 112 113 // Check if the root block device exists. 114 if _, ok := blockDevices["/dev/sda1"]; !ok { 115 fmt.Errorf("block device doesn't exist: /dev/sda1") 116 } 117 118 // Check if the secondary block device exists. 119 if _, ok := blockDevices["/dev/sdb"]; !ok { 120 fmt.Errorf("block device doesn't exist: /dev/sdb") 121 } 122 123 // Check if the third block device exists. 124 if _, ok := blockDevices["/dev/sdc"]; !ok { 125 fmt.Errorf("block device doesn't exist: /dev/sdc") 126 } 127 128 // Check if the encrypted block device exists 129 if _, ok := blockDevices["/dev/sdd"]; !ok { 130 fmt.Errorf("block device doesn't exist: /dev/sdd") 131 } 132 133 return nil 134 } 135 } 136 137 resource.Test(t, resource.TestCase{ 138 PreCheck: func() { testAccPreCheck(t) }, 139 Providers: testAccProviders, 140 CheckDestroy: testAccCheckInstanceDestroy, 141 Steps: []resource.TestStep{ 142 resource.TestStep{ 143 Config: testAccInstanceConfigBlockDevices, 144 Check: resource.ComposeTestCheckFunc( 145 testAccCheckInstanceExists( 146 "aws_instance.foo", &v), 147 resource.TestCheckResourceAttr( 148 "aws_instance.foo", "root_block_device.#", "1"), 149 resource.TestCheckResourceAttr( 150 "aws_instance.foo", "root_block_device.0.volume_size", "11"), 151 resource.TestCheckResourceAttr( 152 "aws_instance.foo", "root_block_device.0.volume_type", "gp2"), 153 resource.TestCheckResourceAttr( 154 "aws_instance.foo", "ebs_block_device.#", "3"), 155 resource.TestCheckResourceAttr( 156 "aws_instance.foo", "ebs_block_device.2576023345.device_name", "/dev/sdb"), 157 resource.TestCheckResourceAttr( 158 "aws_instance.foo", "ebs_block_device.2576023345.volume_size", "9"), 159 resource.TestCheckResourceAttr( 160 "aws_instance.foo", "ebs_block_device.2576023345.volume_type", "standard"), 161 resource.TestCheckResourceAttr( 162 "aws_instance.foo", "ebs_block_device.2554893574.device_name", "/dev/sdc"), 163 resource.TestCheckResourceAttr( 164 "aws_instance.foo", "ebs_block_device.2554893574.volume_size", "10"), 165 resource.TestCheckResourceAttr( 166 "aws_instance.foo", "ebs_block_device.2554893574.volume_type", "io1"), 167 resource.TestCheckResourceAttr( 168 "aws_instance.foo", "ebs_block_device.2554893574.iops", "100"), 169 resource.TestCheckResourceAttr( 170 "aws_instance.foo", "ebs_block_device.2634515331.device_name", "/dev/sdd"), 171 resource.TestCheckResourceAttr( 172 "aws_instance.foo", "ebs_block_device.2634515331.encrypted", "true"), 173 resource.TestCheckResourceAttr( 174 "aws_instance.foo", "ebs_block_device.2634515331.volume_size", "12"), 175 resource.TestCheckResourceAttr( 176 "aws_instance.foo", "ephemeral_block_device.#", "1"), 177 resource.TestCheckResourceAttr( 178 "aws_instance.foo", "ephemeral_block_device.1692014856.device_name", "/dev/sde"), 179 resource.TestCheckResourceAttr( 180 "aws_instance.foo", "ephemeral_block_device.1692014856.virtual_name", "ephemeral0"), 181 testCheck(), 182 ), 183 }, 184 }, 185 }) 186 } 187 188 func TestAccAWSInstance_sourceDestCheck(t *testing.T) { 189 var v ec2.Instance 190 191 testCheck := func(enabled bool) resource.TestCheckFunc { 192 return func(*terraform.State) error { 193 if *v.SourceDestCheck != enabled { 194 return fmt.Errorf("bad source_dest_check: %#v", *v.SourceDestCheck) 195 } 196 197 return nil 198 } 199 } 200 201 resource.Test(t, resource.TestCase{ 202 PreCheck: func() { testAccPreCheck(t) }, 203 Providers: testAccProviders, 204 CheckDestroy: testAccCheckInstanceDestroy, 205 Steps: []resource.TestStep{ 206 resource.TestStep{ 207 Config: testAccInstanceConfigSourceDestDisable, 208 Check: resource.ComposeTestCheckFunc( 209 testAccCheckInstanceExists("aws_instance.foo", &v), 210 testCheck(false), 211 ), 212 }, 213 214 resource.TestStep{ 215 Config: testAccInstanceConfigSourceDestEnable, 216 Check: resource.ComposeTestCheckFunc( 217 testAccCheckInstanceExists("aws_instance.foo", &v), 218 testCheck(true), 219 ), 220 }, 221 222 resource.TestStep{ 223 Config: testAccInstanceConfigSourceDestDisable, 224 Check: resource.ComposeTestCheckFunc( 225 testAccCheckInstanceExists("aws_instance.foo", &v), 226 testCheck(false), 227 ), 228 }, 229 }, 230 }) 231 } 232 233 func TestAccAWSInstance_disableApiTermination(t *testing.T) { 234 var v ec2.Instance 235 236 checkDisableApiTermination := func(expected bool) resource.TestCheckFunc { 237 return func(*terraform.State) error { 238 conn := testAccProvider.Meta().(*AWSClient).ec2conn 239 r, err := conn.DescribeInstanceAttribute(&ec2.DescribeInstanceAttributeInput{ 240 InstanceID: v.InstanceID, 241 Attribute: aws.String("disableApiTermination"), 242 }) 243 if err != nil { 244 return err 245 } 246 got := *r.DisableAPITermination.Value 247 if got != expected { 248 return fmt.Errorf("expected: %t, got: %t", expected, got) 249 } 250 return nil 251 } 252 } 253 254 resource.Test(t, resource.TestCase{ 255 PreCheck: func() { testAccPreCheck(t) }, 256 Providers: testAccProviders, 257 CheckDestroy: testAccCheckInstanceDestroy, 258 Steps: []resource.TestStep{ 259 resource.TestStep{ 260 Config: testAccInstanceConfigDisableAPITermination(true), 261 Check: resource.ComposeTestCheckFunc( 262 testAccCheckInstanceExists("aws_instance.foo", &v), 263 checkDisableApiTermination(true), 264 ), 265 }, 266 267 resource.TestStep{ 268 Config: testAccInstanceConfigDisableAPITermination(false), 269 Check: resource.ComposeTestCheckFunc( 270 testAccCheckInstanceExists("aws_instance.foo", &v), 271 checkDisableApiTermination(false), 272 ), 273 }, 274 }, 275 }) 276 } 277 278 func TestAccAWSInstance_vpc(t *testing.T) { 279 var v ec2.Instance 280 281 resource.Test(t, resource.TestCase{ 282 PreCheck: func() { testAccPreCheck(t) }, 283 Providers: testAccProviders, 284 CheckDestroy: testAccCheckInstanceDestroy, 285 Steps: []resource.TestStep{ 286 resource.TestStep{ 287 Config: testAccInstanceConfigVPC, 288 Check: resource.ComposeTestCheckFunc( 289 testAccCheckInstanceExists( 290 "aws_instance.foo", &v), 291 ), 292 }, 293 }, 294 }) 295 } 296 297 func TestAccAWSInstance_multipleRegions(t *testing.T) { 298 var v ec2.Instance 299 300 // record the initialized providers so that we can use them to 301 // check for the instances in each region 302 var providers []*schema.Provider 303 providerFactories := map[string]terraform.ResourceProviderFactory{ 304 "aws": func() (terraform.ResourceProvider, error) { 305 p := Provider() 306 providers = append(providers, p.(*schema.Provider)) 307 return p, nil 308 }, 309 } 310 311 resource.Test(t, resource.TestCase{ 312 PreCheck: func() { testAccPreCheck(t) }, 313 ProviderFactories: providerFactories, 314 CheckDestroy: testAccCheckInstanceDestroyWithProviders(&providers), 315 Steps: []resource.TestStep{ 316 resource.TestStep{ 317 Config: testAccInstanceConfigMultipleRegions, 318 Check: resource.ComposeTestCheckFunc( 319 testAccCheckInstanceExistsWithProviders( 320 "aws_instance.foo", &v, &providers), 321 testAccCheckInstanceExistsWithProviders( 322 "aws_instance.bar", &v, &providers), 323 ), 324 }, 325 }, 326 }) 327 } 328 329 func TestAccAWSInstance_NetworkInstanceSecurityGroups(t *testing.T) { 330 var v ec2.Instance 331 332 resource.Test(t, resource.TestCase{ 333 PreCheck: func() { testAccPreCheck(t) }, 334 Providers: testAccProviders, 335 CheckDestroy: testAccCheckInstanceDestroy, 336 Steps: []resource.TestStep{ 337 resource.TestStep{ 338 Config: testAccInstanceNetworkInstanceSecurityGroups, 339 Check: resource.ComposeTestCheckFunc( 340 testAccCheckInstanceExists( 341 "aws_instance.foo_instance", &v), 342 ), 343 }, 344 }, 345 }) 346 } 347 348 func TestAccAWSInstance_NetworkInstanceVPCSecurityGroupIDs(t *testing.T) { 349 var v ec2.Instance 350 351 resource.Test(t, resource.TestCase{ 352 PreCheck: func() { testAccPreCheck(t) }, 353 Providers: testAccProviders, 354 CheckDestroy: testAccCheckInstanceDestroy, 355 Steps: []resource.TestStep{ 356 resource.TestStep{ 357 Config: testAccInstanceNetworkInstanceVPCSecurityGroupIDs, 358 Check: resource.ComposeTestCheckFunc( 359 testAccCheckInstanceExists( 360 "aws_instance.foo_instance", &v), 361 resource.TestCheckResourceAttr( 362 "aws_instance.foo_instance", "security_groups.#", "0"), 363 resource.TestCheckResourceAttr( 364 "aws_instance.foo_instance", "vpc_security_group_ids.#", "1"), 365 ), 366 }, 367 }, 368 }) 369 } 370 371 func TestAccAWSInstance_tags(t *testing.T) { 372 var v ec2.Instance 373 374 resource.Test(t, resource.TestCase{ 375 PreCheck: func() { testAccPreCheck(t) }, 376 Providers: testAccProviders, 377 CheckDestroy: testAccCheckInstanceDestroy, 378 Steps: []resource.TestStep{ 379 resource.TestStep{ 380 Config: testAccCheckInstanceConfigTags, 381 Check: resource.ComposeTestCheckFunc( 382 testAccCheckInstanceExists("aws_instance.foo", &v), 383 testAccCheckTags(&v.Tags, "foo", "bar"), 384 // Guard against regression of https://github.com/hashicorp/terraform/issues/914 385 testAccCheckTags(&v.Tags, "#", ""), 386 ), 387 }, 388 389 resource.TestStep{ 390 Config: testAccCheckInstanceConfigTagsUpdate, 391 Check: resource.ComposeTestCheckFunc( 392 testAccCheckInstanceExists("aws_instance.foo", &v), 393 testAccCheckTags(&v.Tags, "foo", ""), 394 testAccCheckTags(&v.Tags, "bar", "baz"), 395 ), 396 }, 397 }, 398 }) 399 } 400 401 func TestAccAWSInstance_privateIP(t *testing.T) { 402 var v ec2.Instance 403 404 testCheckPrivateIP := func() resource.TestCheckFunc { 405 return func(*terraform.State) error { 406 if *v.PrivateIPAddress != "10.1.1.42" { 407 return fmt.Errorf("bad private IP: %s", *v.PrivateIPAddress) 408 } 409 410 return nil 411 } 412 } 413 414 resource.Test(t, resource.TestCase{ 415 PreCheck: func() { testAccPreCheck(t) }, 416 Providers: testAccProviders, 417 CheckDestroy: testAccCheckInstanceDestroy, 418 Steps: []resource.TestStep{ 419 resource.TestStep{ 420 Config: testAccInstanceConfigPrivateIP, 421 Check: resource.ComposeTestCheckFunc( 422 testAccCheckInstanceExists("aws_instance.foo", &v), 423 testCheckPrivateIP(), 424 ), 425 }, 426 }, 427 }) 428 } 429 430 func TestAccAWSInstance_associatePublicIPAndPrivateIP(t *testing.T) { 431 var v ec2.Instance 432 433 testCheckPrivateIP := func() resource.TestCheckFunc { 434 return func(*terraform.State) error { 435 if *v.PrivateIPAddress != "10.1.1.42" { 436 return fmt.Errorf("bad private IP: %s", *v.PrivateIPAddress) 437 } 438 439 return nil 440 } 441 } 442 443 resource.Test(t, resource.TestCase{ 444 PreCheck: func() { testAccPreCheck(t) }, 445 Providers: testAccProviders, 446 CheckDestroy: testAccCheckInstanceDestroy, 447 Steps: []resource.TestStep{ 448 resource.TestStep{ 449 Config: testAccInstanceConfigAssociatePublicIPAndPrivateIP, 450 Check: resource.ComposeTestCheckFunc( 451 testAccCheckInstanceExists("aws_instance.foo", &v), 452 testCheckPrivateIP(), 453 ), 454 }, 455 }, 456 }) 457 } 458 459 // Guard against regression with KeyPairs 460 // https://github.com/hashicorp/terraform/issues/2302 461 func TestAccAWSInstance_keyPairCheck(t *testing.T) { 462 var v ec2.Instance 463 464 testCheckKeyPair := func(keyName string) resource.TestCheckFunc { 465 return func(*terraform.State) error { 466 if v.KeyName == nil { 467 return fmt.Errorf("No Key Pair found, expected(%s)", keyName) 468 } 469 if v.KeyName != nil && *v.KeyName != keyName { 470 return fmt.Errorf("Bad key name, expected (%s), got (%s)", keyName, *v.KeyName) 471 } 472 473 return nil 474 } 475 } 476 477 resource.Test(t, resource.TestCase{ 478 PreCheck: func() { testAccPreCheck(t) }, 479 Providers: testAccProviders, 480 CheckDestroy: testAccCheckInstanceDestroy, 481 Steps: []resource.TestStep{ 482 resource.TestStep{ 483 Config: testAccInstanceConfigKeyPair, 484 Check: resource.ComposeTestCheckFunc( 485 testAccCheckInstanceExists("aws_instance.foo", &v), 486 testCheckKeyPair("tmp-key"), 487 ), 488 }, 489 }, 490 }) 491 } 492 493 func TestAccAWSInstance_rootBlockDeviceMismatch(t *testing.T) { 494 var v ec2.Instance 495 496 resource.Test(t, resource.TestCase{ 497 PreCheck: func() { testAccPreCheck(t) }, 498 Providers: testAccProviders, 499 CheckDestroy: testAccCheckInstanceDestroy, 500 Steps: []resource.TestStep{ 501 resource.TestStep{ 502 Config: testAccInstanceConfigRootBlockDeviceMismatch, 503 Check: resource.ComposeTestCheckFunc( 504 testAccCheckInstanceExists("aws_instance.foo", &v), 505 resource.TestCheckResourceAttr( 506 "aws_instance.foo", "root_block_device.0.volume_size", "13"), 507 ), 508 }, 509 }, 510 }) 511 } 512 513 func testAccCheckInstanceDestroy(s *terraform.State) error { 514 return testAccCheckInstanceDestroyWithProvider(s, testAccProvider) 515 } 516 517 func testAccCheckInstanceDestroyWithProviders(providers *[]*schema.Provider) resource.TestCheckFunc { 518 return func(s *terraform.State) error { 519 for _, provider := range *providers { 520 if provider.Meta() == nil { 521 continue 522 } 523 if err := testAccCheckInstanceDestroyWithProvider(s, provider); err != nil { 524 return err 525 } 526 } 527 return nil 528 } 529 } 530 531 func testAccCheckInstanceDestroyWithProvider(s *terraform.State, provider *schema.Provider) error { 532 conn := provider.Meta().(*AWSClient).ec2conn 533 534 for _, rs := range s.RootModule().Resources { 535 if rs.Type != "aws_instance" { 536 continue 537 } 538 539 // Try to find the resource 540 var err error 541 resp, err := conn.DescribeInstances(&ec2.DescribeInstancesInput{ 542 InstanceIDs: []*string{aws.String(rs.Primary.ID)}, 543 }) 544 if err == nil { 545 if len(resp.Reservations) > 0 { 546 return fmt.Errorf("still exist.") 547 } 548 549 return nil 550 } 551 552 // Verify the error is what we want 553 ec2err, ok := err.(awserr.Error) 554 if !ok { 555 return err 556 } 557 if ec2err.Code() != "InvalidInstanceID.NotFound" { 558 return err 559 } 560 } 561 562 return nil 563 } 564 565 func testAccCheckInstanceExists(n string, i *ec2.Instance) resource.TestCheckFunc { 566 providers := []*schema.Provider{testAccProvider} 567 return testAccCheckInstanceExistsWithProviders(n, i, &providers) 568 } 569 570 func testAccCheckInstanceExistsWithProviders(n string, i *ec2.Instance, providers *[]*schema.Provider) resource.TestCheckFunc { 571 return func(s *terraform.State) error { 572 rs, ok := s.RootModule().Resources[n] 573 if !ok { 574 return fmt.Errorf("Not found: %s", n) 575 } 576 577 if rs.Primary.ID == "" { 578 return fmt.Errorf("No ID is set") 579 } 580 for _, provider := range *providers { 581 // Ignore if Meta is empty, this can happen for validation providers 582 if provider.Meta() == nil { 583 continue 584 } 585 586 conn := provider.Meta().(*AWSClient).ec2conn 587 resp, err := conn.DescribeInstances(&ec2.DescribeInstancesInput{ 588 InstanceIDs: []*string{aws.String(rs.Primary.ID)}, 589 }) 590 if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "InvalidInstanceID.NotFound" { 591 continue 592 } 593 if err != nil { 594 return err 595 } 596 597 if len(resp.Reservations) > 0 { 598 *i = *resp.Reservations[0].Instances[0] 599 return nil 600 } 601 } 602 603 return fmt.Errorf("Instance not found") 604 } 605 } 606 607 func TestInstanceTenancySchema(t *testing.T) { 608 actualSchema := resourceAwsInstance().Schema["tenancy"] 609 expectedSchema := &schema.Schema{ 610 Type: schema.TypeString, 611 Optional: true, 612 Computed: true, 613 ForceNew: true, 614 } 615 if !reflect.DeepEqual(actualSchema, expectedSchema) { 616 t.Fatalf( 617 "Got:\n\n%#v\n\nExpected:\n\n%#v\n", 618 actualSchema, 619 expectedSchema) 620 } 621 } 622 623 const testAccInstanceConfig_pre = ` 624 resource "aws_security_group" "tf_test_foo" { 625 name = "tf_test_foo" 626 description = "foo" 627 628 ingress { 629 protocol = "icmp" 630 from_port = -1 631 to_port = -1 632 cidr_blocks = ["0.0.0.0/0"] 633 } 634 } 635 ` 636 637 const testAccInstanceConfig = ` 638 resource "aws_security_group" "tf_test_foo" { 639 name = "tf_test_foo" 640 description = "foo" 641 642 ingress { 643 protocol = "icmp" 644 from_port = -1 645 to_port = -1 646 cidr_blocks = ["0.0.0.0/0"] 647 } 648 } 649 650 resource "aws_instance" "foo" { 651 # us-west-2 652 ami = "ami-4fccb37f" 653 availability_zone = "us-west-2a" 654 655 instance_type = "m1.small" 656 security_groups = ["${aws_security_group.tf_test_foo.name}"] 657 user_data = "foo:-with-character's" 658 } 659 ` 660 661 const testAccInstanceConfigBlockDevices = ` 662 resource "aws_instance" "foo" { 663 # us-west-2 664 ami = "ami-55a7ea65" 665 666 # In order to attach an encrypted volume to an instance you need to have an 667 # m3.medium or larger. See "Supported Instance Types" in: 668 # http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EBSEncryption.html 669 instance_type = "m3.medium" 670 671 root_block_device { 672 volume_type = "gp2" 673 volume_size = 11 674 } 675 ebs_block_device { 676 device_name = "/dev/sdb" 677 volume_size = 9 678 } 679 ebs_block_device { 680 device_name = "/dev/sdc" 681 volume_size = 10 682 volume_type = "io1" 683 iops = 100 684 } 685 686 # Encrypted ebs block device 687 ebs_block_device { 688 device_name = "/dev/sdd" 689 volume_size = 12 690 encrypted = true 691 } 692 693 ephemeral_block_device { 694 device_name = "/dev/sde" 695 virtual_name = "ephemeral0" 696 } 697 } 698 ` 699 700 const testAccInstanceConfigSourceDestEnable = ` 701 resource "aws_vpc" "foo" { 702 cidr_block = "10.1.0.0/16" 703 } 704 705 resource "aws_subnet" "foo" { 706 cidr_block = "10.1.1.0/24" 707 vpc_id = "${aws_vpc.foo.id}" 708 } 709 710 resource "aws_instance" "foo" { 711 # us-west-2 712 ami = "ami-4fccb37f" 713 instance_type = "m1.small" 714 subnet_id = "${aws_subnet.foo.id}" 715 } 716 ` 717 718 const testAccInstanceConfigSourceDestDisable = ` 719 resource "aws_vpc" "foo" { 720 cidr_block = "10.1.0.0/16" 721 } 722 723 resource "aws_subnet" "foo" { 724 cidr_block = "10.1.1.0/24" 725 vpc_id = "${aws_vpc.foo.id}" 726 } 727 728 resource "aws_instance" "foo" { 729 # us-west-2 730 ami = "ami-4fccb37f" 731 instance_type = "m1.small" 732 subnet_id = "${aws_subnet.foo.id}" 733 source_dest_check = false 734 } 735 ` 736 737 func testAccInstanceConfigDisableAPITermination(val bool) string { 738 return fmt.Sprintf(` 739 resource "aws_vpc" "foo" { 740 cidr_block = "10.1.0.0/16" 741 } 742 743 resource "aws_subnet" "foo" { 744 cidr_block = "10.1.1.0/24" 745 vpc_id = "${aws_vpc.foo.id}" 746 } 747 748 resource "aws_instance" "foo" { 749 # us-west-2 750 ami = "ami-4fccb37f" 751 instance_type = "m1.small" 752 subnet_id = "${aws_subnet.foo.id}" 753 disable_api_termination = %t 754 } 755 `, val) 756 } 757 758 const testAccInstanceConfigVPC = ` 759 resource "aws_vpc" "foo" { 760 cidr_block = "10.1.0.0/16" 761 } 762 763 resource "aws_subnet" "foo" { 764 cidr_block = "10.1.1.0/24" 765 vpc_id = "${aws_vpc.foo.id}" 766 } 767 768 resource "aws_instance" "foo" { 769 # us-west-2 770 ami = "ami-4fccb37f" 771 instance_type = "m1.small" 772 subnet_id = "${aws_subnet.foo.id}" 773 associate_public_ip_address = true 774 tenancy = "dedicated" 775 } 776 ` 777 778 const testAccInstanceConfigMultipleRegions = ` 779 provider "aws" { 780 alias = "west" 781 region = "us-west-2" 782 } 783 784 provider "aws" { 785 alias = "east" 786 region = "us-east-1" 787 } 788 789 resource "aws_instance" "foo" { 790 # us-west-2 791 provider = "aws.west" 792 ami = "ami-4fccb37f" 793 instance_type = "m1.small" 794 } 795 796 resource "aws_instance" "bar" { 797 # us-east-1 798 provider = "aws.east" 799 ami = "ami-8c6ea9e4" 800 instance_type = "m1.small" 801 } 802 ` 803 804 const testAccCheckInstanceConfigTags = ` 805 resource "aws_instance" "foo" { 806 ami = "ami-4fccb37f" 807 instance_type = "m1.small" 808 tags { 809 foo = "bar" 810 } 811 } 812 ` 813 814 const testAccCheckInstanceConfigTagsUpdate = ` 815 resource "aws_instance" "foo" { 816 ami = "ami-4fccb37f" 817 instance_type = "m1.small" 818 tags { 819 bar = "baz" 820 } 821 } 822 ` 823 824 const testAccInstanceConfigPrivateIP = ` 825 resource "aws_vpc" "foo" { 826 cidr_block = "10.1.0.0/16" 827 } 828 829 resource "aws_subnet" "foo" { 830 cidr_block = "10.1.1.0/24" 831 vpc_id = "${aws_vpc.foo.id}" 832 } 833 834 resource "aws_instance" "foo" { 835 ami = "ami-c5eabbf5" 836 instance_type = "t2.micro" 837 subnet_id = "${aws_subnet.foo.id}" 838 private_ip = "10.1.1.42" 839 } 840 ` 841 842 const testAccInstanceConfigAssociatePublicIPAndPrivateIP = ` 843 resource "aws_vpc" "foo" { 844 cidr_block = "10.1.0.0/16" 845 } 846 847 resource "aws_subnet" "foo" { 848 cidr_block = "10.1.1.0/24" 849 vpc_id = "${aws_vpc.foo.id}" 850 } 851 852 resource "aws_instance" "foo" { 853 ami = "ami-c5eabbf5" 854 instance_type = "t2.micro" 855 subnet_id = "${aws_subnet.foo.id}" 856 associate_public_ip_address = true 857 private_ip = "10.1.1.42" 858 } 859 ` 860 861 const testAccInstanceNetworkInstanceSecurityGroups = ` 862 resource "aws_internet_gateway" "gw" { 863 vpc_id = "${aws_vpc.foo.id}" 864 } 865 866 resource "aws_vpc" "foo" { 867 cidr_block = "10.1.0.0/16" 868 tags { 869 Name = "tf-network-test" 870 } 871 } 872 873 resource "aws_security_group" "tf_test_foo" { 874 name = "tf_test_foo" 875 description = "foo" 876 vpc_id="${aws_vpc.foo.id}" 877 878 ingress { 879 protocol = "icmp" 880 from_port = -1 881 to_port = -1 882 cidr_blocks = ["0.0.0.0/0"] 883 } 884 } 885 886 resource "aws_subnet" "foo" { 887 cidr_block = "10.1.1.0/24" 888 vpc_id = "${aws_vpc.foo.id}" 889 } 890 891 resource "aws_instance" "foo_instance" { 892 ami = "ami-21f78e11" 893 instance_type = "t1.micro" 894 security_groups = ["${aws_security_group.tf_test_foo.id}"] 895 subnet_id = "${aws_subnet.foo.id}" 896 associate_public_ip_address = true 897 depends_on = ["aws_internet_gateway.gw"] 898 } 899 900 resource "aws_eip" "foo_eip" { 901 instance = "${aws_instance.foo_instance.id}" 902 vpc = true 903 depends_on = ["aws_internet_gateway.gw"] 904 } 905 ` 906 907 const testAccInstanceNetworkInstanceVPCSecurityGroupIDs = ` 908 resource "aws_internet_gateway" "gw" { 909 vpc_id = "${aws_vpc.foo.id}" 910 } 911 912 resource "aws_vpc" "foo" { 913 cidr_block = "10.1.0.0/16" 914 tags { 915 Name = "tf-network-test" 916 } 917 } 918 919 resource "aws_security_group" "tf_test_foo" { 920 name = "tf_test_foo" 921 description = "foo" 922 vpc_id="${aws_vpc.foo.id}" 923 924 ingress { 925 protocol = "icmp" 926 from_port = -1 927 to_port = -1 928 cidr_blocks = ["0.0.0.0/0"] 929 } 930 } 931 932 resource "aws_subnet" "foo" { 933 cidr_block = "10.1.1.0/24" 934 vpc_id = "${aws_vpc.foo.id}" 935 } 936 937 resource "aws_instance" "foo_instance" { 938 ami = "ami-21f78e11" 939 instance_type = "t1.micro" 940 vpc_security_group_ids = ["${aws_security_group.tf_test_foo.id}"] 941 subnet_id = "${aws_subnet.foo.id}" 942 depends_on = ["aws_internet_gateway.gw"] 943 } 944 945 resource "aws_eip" "foo_eip" { 946 instance = "${aws_instance.foo_instance.id}" 947 vpc = true 948 depends_on = ["aws_internet_gateway.gw"] 949 } 950 ` 951 952 const testAccInstanceConfigKeyPair = ` 953 provider "aws" { 954 region = "us-east-1" 955 } 956 957 resource "aws_key_pair" "debugging" { 958 key_name = "tmp-key" 959 public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD3F6tyPEFEzV0LX3X8BsXdMsQz1x2cEikKDEY0aIj41qgxMCP/iteneqXSIFZBp5vizPvaoIR3Um9xK7PGoW8giupGn+EPuxIA4cDM4vzOqOkiMPhz5XK0whEjkVzTo4+S0puvDZuwIsdiW9mxhJc7tgBNL0cYlWSYVkz4G/fslNfRPW5mYAM49f4fhtxPb5ok4Q2Lg9dPKVHO/Bgeu5woMc7RY0p1ej6D4CKFE6lymSDJpW0YHX/wqE9+cfEauh7xZcG0q9t2ta6F6fmX0agvpFyZo8aFbXeUBr7osSCJNgvavWbM/06niWrOvYX2xwWdhXmXSrbX8ZbabVohBK41 phodgson@thoughtworks.com" 960 } 961 962 resource "aws_instance" "foo" { 963 ami = "ami-408c7f28" 964 instance_type = "t1.micro" 965 key_name = "${aws_key_pair.debugging.key_name}" 966 } 967 ` 968 969 const testAccInstanceConfigRootBlockDeviceMismatch = ` 970 resource "aws_vpc" "foo" { 971 cidr_block = "10.1.0.0/16" 972 } 973 974 resource "aws_subnet" "foo" { 975 cidr_block = "10.1.1.0/24" 976 vpc_id = "${aws_vpc.foo.id}" 977 } 978 979 resource "aws_instance" "foo" { 980 // This is an AMI with RootDeviceName: "/dev/sda1"; actual root: "/dev/sda" 981 ami = "ami-ef5b69df" 982 instance_type = "t1.micro" 983 subnet_id = "${aws_subnet.foo.id}" 984 root_block_device { 985 volume_size = 13 986 } 987 } 988 `