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