github.com/armen/terraform@v0.5.2-0.20150529052519-caa8117a08f1/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/awslabs/aws-sdk-go/aws" 9 "github.com/awslabs/aws-sdk-go/aws/awserr" 10 "github.com/awslabs/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_normal(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.Long(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 func testAccCheckInstanceDestroy(s *terraform.State) error { 460 return testAccCheckInstanceDestroyWithProvider(s, testAccProvider) 461 } 462 463 func testAccCheckInstanceDestroyWithProviders(providers *[]*schema.Provider) resource.TestCheckFunc { 464 return func(s *terraform.State) error { 465 for _, provider := range *providers { 466 if provider.Meta() == nil { 467 continue 468 } 469 if err := testAccCheckInstanceDestroyWithProvider(s, provider); err != nil { 470 return err 471 } 472 } 473 return nil 474 } 475 } 476 477 func testAccCheckInstanceDestroyWithProvider(s *terraform.State, provider *schema.Provider) error { 478 conn := provider.Meta().(*AWSClient).ec2conn 479 480 for _, rs := range s.RootModule().Resources { 481 if rs.Type != "aws_instance" { 482 continue 483 } 484 485 // Try to find the resource 486 var err error 487 resp, err := conn.DescribeInstances(&ec2.DescribeInstancesInput{ 488 InstanceIDs: []*string{aws.String(rs.Primary.ID)}, 489 }) 490 if err == nil { 491 if len(resp.Reservations) > 0 { 492 return fmt.Errorf("still exist.") 493 } 494 495 return nil 496 } 497 498 // Verify the error is what we want 499 ec2err, ok := err.(awserr.Error) 500 if !ok { 501 return err 502 } 503 if ec2err.Code() != "InvalidInstanceID.NotFound" { 504 return err 505 } 506 } 507 508 return nil 509 } 510 511 func testAccCheckInstanceExists(n string, i *ec2.Instance) resource.TestCheckFunc { 512 providers := []*schema.Provider{testAccProvider} 513 return testAccCheckInstanceExistsWithProviders(n, i, &providers) 514 } 515 516 func testAccCheckInstanceExistsWithProviders(n string, i *ec2.Instance, providers *[]*schema.Provider) resource.TestCheckFunc { 517 return func(s *terraform.State) error { 518 rs, ok := s.RootModule().Resources[n] 519 if !ok { 520 return fmt.Errorf("Not found: %s", n) 521 } 522 523 if rs.Primary.ID == "" { 524 return fmt.Errorf("No ID is set") 525 } 526 for _, provider := range *providers { 527 conn := provider.Meta().(*AWSClient).ec2conn 528 resp, err := conn.DescribeInstances(&ec2.DescribeInstancesInput{ 529 InstanceIDs: []*string{aws.String(rs.Primary.ID)}, 530 }) 531 if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "InvalidInstanceID.NotFound" { 532 continue 533 } 534 if err != nil { 535 return err 536 } 537 538 if len(resp.Reservations) > 0 { 539 *i = *resp.Reservations[0].Instances[0] 540 return nil 541 } 542 } 543 544 return fmt.Errorf("Instance not found") 545 } 546 } 547 548 func TestInstanceTenancySchema(t *testing.T) { 549 actualSchema := resourceAwsInstance().Schema["tenancy"] 550 expectedSchema := &schema.Schema{ 551 Type: schema.TypeString, 552 Optional: true, 553 Computed: true, 554 ForceNew: true, 555 } 556 if !reflect.DeepEqual(actualSchema, expectedSchema) { 557 t.Fatalf( 558 "Got:\n\n%#v\n\nExpected:\n\n%#v\n", 559 actualSchema, 560 expectedSchema) 561 } 562 } 563 564 const testAccInstanceConfig_pre = ` 565 resource "aws_security_group" "tf_test_foo" { 566 name = "tf_test_foo" 567 description = "foo" 568 569 ingress { 570 protocol = "icmp" 571 from_port = -1 572 to_port = -1 573 cidr_blocks = ["0.0.0.0/0"] 574 } 575 } 576 ` 577 578 const testAccInstanceConfig = ` 579 resource "aws_security_group" "tf_test_foo" { 580 name = "tf_test_foo" 581 description = "foo" 582 583 ingress { 584 protocol = "icmp" 585 from_port = -1 586 to_port = -1 587 cidr_blocks = ["0.0.0.0/0"] 588 } 589 } 590 591 resource "aws_instance" "foo" { 592 # us-west-2 593 ami = "ami-4fccb37f" 594 availability_zone = "us-west-2a" 595 596 instance_type = "m1.small" 597 security_groups = ["${aws_security_group.tf_test_foo.name}"] 598 user_data = "foo:-with-character's" 599 } 600 ` 601 602 const testAccInstanceConfigBlockDevices = ` 603 resource "aws_instance" "foo" { 604 # us-west-2 605 ami = "ami-55a7ea65" 606 607 # In order to attach an encrypted volume to an instance you need to have an 608 # m3.medium or larger. See "Supported Instance Types" in: 609 # http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EBSEncryption.html 610 instance_type = "m3.medium" 611 612 root_block_device { 613 volume_type = "gp2" 614 volume_size = 11 615 } 616 ebs_block_device { 617 device_name = "/dev/sdb" 618 volume_size = 9 619 } 620 ebs_block_device { 621 device_name = "/dev/sdc" 622 volume_size = 10 623 volume_type = "io1" 624 iops = 100 625 } 626 627 # Encrypted ebs block device 628 ebs_block_device { 629 device_name = "/dev/sdd" 630 volume_size = 12 631 encrypted = true 632 } 633 634 ephemeral_block_device { 635 device_name = "/dev/sde" 636 virtual_name = "ephemeral0" 637 } 638 } 639 ` 640 641 const testAccInstanceConfigSourceDestEnable = ` 642 resource "aws_vpc" "foo" { 643 cidr_block = "10.1.0.0/16" 644 } 645 646 resource "aws_subnet" "foo" { 647 cidr_block = "10.1.1.0/24" 648 vpc_id = "${aws_vpc.foo.id}" 649 } 650 651 resource "aws_instance" "foo" { 652 # us-west-2 653 ami = "ami-4fccb37f" 654 instance_type = "m1.small" 655 subnet_id = "${aws_subnet.foo.id}" 656 source_dest_check = true 657 } 658 ` 659 660 const testAccInstanceConfigSourceDestDisable = ` 661 resource "aws_vpc" "foo" { 662 cidr_block = "10.1.0.0/16" 663 } 664 665 resource "aws_subnet" "foo" { 666 cidr_block = "10.1.1.0/24" 667 vpc_id = "${aws_vpc.foo.id}" 668 } 669 670 resource "aws_instance" "foo" { 671 # us-west-2 672 ami = "ami-4fccb37f" 673 instance_type = "m1.small" 674 subnet_id = "${aws_subnet.foo.id}" 675 source_dest_check = false 676 } 677 ` 678 679 func testAccInstanceConfigDisableAPITermination(val bool) string { 680 return fmt.Sprintf(` 681 resource "aws_vpc" "foo" { 682 cidr_block = "10.1.0.0/16" 683 } 684 685 resource "aws_subnet" "foo" { 686 cidr_block = "10.1.1.0/24" 687 vpc_id = "${aws_vpc.foo.id}" 688 } 689 690 resource "aws_instance" "foo" { 691 # us-west-2 692 ami = "ami-4fccb37f" 693 instance_type = "m1.small" 694 subnet_id = "${aws_subnet.foo.id}" 695 disable_api_termination = %t 696 } 697 `, val) 698 } 699 700 const testAccInstanceConfigVPC = ` 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 associate_public_ip_address = true 716 tenancy = "dedicated" 717 } 718 ` 719 720 const testAccInstanceConfigMultipleRegions = ` 721 provider "aws" { 722 alias = "west" 723 region = "us-west-2" 724 } 725 726 provider "aws" { 727 alias = "east" 728 region = "us-east-1" 729 } 730 731 resource "aws_instance" "foo" { 732 # us-west-2 733 provider = "aws.west" 734 ami = "ami-4fccb37f" 735 instance_type = "m1.small" 736 } 737 738 resource "aws_instance" "bar" { 739 # us-east-1 740 provider = "aws.east" 741 ami = "ami-8c6ea9e4" 742 instance_type = "m1.small" 743 } 744 ` 745 746 const testAccCheckInstanceConfigTags = ` 747 resource "aws_instance" "foo" { 748 ami = "ami-4fccb37f" 749 instance_type = "m1.small" 750 tags { 751 foo = "bar" 752 } 753 } 754 ` 755 756 const testAccCheckInstanceConfigTagsUpdate = ` 757 resource "aws_instance" "foo" { 758 ami = "ami-4fccb37f" 759 instance_type = "m1.small" 760 tags { 761 bar = "baz" 762 } 763 } 764 ` 765 766 const testAccInstanceConfigPrivateIP = ` 767 resource "aws_vpc" "foo" { 768 cidr_block = "10.1.0.0/16" 769 } 770 771 resource "aws_subnet" "foo" { 772 cidr_block = "10.1.1.0/24" 773 vpc_id = "${aws_vpc.foo.id}" 774 } 775 776 resource "aws_instance" "foo" { 777 ami = "ami-c5eabbf5" 778 instance_type = "t2.micro" 779 subnet_id = "${aws_subnet.foo.id}" 780 private_ip = "10.1.1.42" 781 } 782 ` 783 784 const testAccInstanceConfigAssociatePublicIPAndPrivateIP = ` 785 resource "aws_vpc" "foo" { 786 cidr_block = "10.1.0.0/16" 787 } 788 789 resource "aws_subnet" "foo" { 790 cidr_block = "10.1.1.0/24" 791 vpc_id = "${aws_vpc.foo.id}" 792 } 793 794 resource "aws_instance" "foo" { 795 ami = "ami-c5eabbf5" 796 instance_type = "t2.micro" 797 subnet_id = "${aws_subnet.foo.id}" 798 associate_public_ip_address = true 799 private_ip = "10.1.1.42" 800 } 801 ` 802 803 const testAccInstanceNetworkInstanceSecurityGroups = ` 804 resource "aws_internet_gateway" "gw" { 805 vpc_id = "${aws_vpc.foo.id}" 806 } 807 808 resource "aws_vpc" "foo" { 809 cidr_block = "10.1.0.0/16" 810 tags { 811 Name = "tf-network-test" 812 } 813 } 814 815 resource "aws_security_group" "tf_test_foo" { 816 name = "tf_test_foo" 817 description = "foo" 818 vpc_id="${aws_vpc.foo.id}" 819 820 ingress { 821 protocol = "icmp" 822 from_port = -1 823 to_port = -1 824 cidr_blocks = ["0.0.0.0/0"] 825 } 826 } 827 828 resource "aws_subnet" "foo" { 829 cidr_block = "10.1.1.0/24" 830 vpc_id = "${aws_vpc.foo.id}" 831 } 832 833 resource "aws_instance" "foo_instance" { 834 ami = "ami-21f78e11" 835 instance_type = "t1.micro" 836 security_groups = ["${aws_security_group.tf_test_foo.id}"] 837 subnet_id = "${aws_subnet.foo.id}" 838 associate_public_ip_address = true 839 depends_on = ["aws_internet_gateway.gw"] 840 } 841 842 resource "aws_eip" "foo_eip" { 843 instance = "${aws_instance.foo_instance.id}" 844 vpc = true 845 depends_on = ["aws_internet_gateway.gw"] 846 } 847 ` 848 849 const testAccInstanceNetworkInstanceVPCSecurityGroupIDs = ` 850 resource "aws_internet_gateway" "gw" { 851 vpc_id = "${aws_vpc.foo.id}" 852 } 853 854 resource "aws_vpc" "foo" { 855 cidr_block = "10.1.0.0/16" 856 tags { 857 Name = "tf-network-test" 858 } 859 } 860 861 resource "aws_security_group" "tf_test_foo" { 862 name = "tf_test_foo" 863 description = "foo" 864 vpc_id="${aws_vpc.foo.id}" 865 866 ingress { 867 protocol = "icmp" 868 from_port = -1 869 to_port = -1 870 cidr_blocks = ["0.0.0.0/0"] 871 } 872 } 873 874 resource "aws_subnet" "foo" { 875 cidr_block = "10.1.1.0/24" 876 vpc_id = "${aws_vpc.foo.id}" 877 } 878 879 resource "aws_instance" "foo_instance" { 880 ami = "ami-21f78e11" 881 instance_type = "t1.micro" 882 vpc_security_group_ids = ["${aws_security_group.tf_test_foo.id}"] 883 subnet_id = "${aws_subnet.foo.id}" 884 depends_on = ["aws_internet_gateway.gw"] 885 } 886 887 resource "aws_eip" "foo_eip" { 888 instance = "${aws_instance.foo_instance.id}" 889 vpc = true 890 depends_on = ["aws_internet_gateway.gw"] 891 } 892 `