github.com/ggiamarchi/terraform@v0.3.7-0.20150607194748-ed2a66a46a71/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_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 } 657 ` 658 659 const testAccInstanceConfigSourceDestDisable = ` 660 resource "aws_vpc" "foo" { 661 cidr_block = "10.1.0.0/16" 662 } 663 664 resource "aws_subnet" "foo" { 665 cidr_block = "10.1.1.0/24" 666 vpc_id = "${aws_vpc.foo.id}" 667 } 668 669 resource "aws_instance" "foo" { 670 # us-west-2 671 ami = "ami-4fccb37f" 672 instance_type = "m1.small" 673 subnet_id = "${aws_subnet.foo.id}" 674 source_dest_check = false 675 } 676 ` 677 678 func testAccInstanceConfigDisableAPITermination(val bool) string { 679 return fmt.Sprintf(` 680 resource "aws_vpc" "foo" { 681 cidr_block = "10.1.0.0/16" 682 } 683 684 resource "aws_subnet" "foo" { 685 cidr_block = "10.1.1.0/24" 686 vpc_id = "${aws_vpc.foo.id}" 687 } 688 689 resource "aws_instance" "foo" { 690 # us-west-2 691 ami = "ami-4fccb37f" 692 instance_type = "m1.small" 693 subnet_id = "${aws_subnet.foo.id}" 694 disable_api_termination = %t 695 } 696 `, val) 697 } 698 699 const testAccInstanceConfigVPC = ` 700 resource "aws_vpc" "foo" { 701 cidr_block = "10.1.0.0/16" 702 } 703 704 resource "aws_subnet" "foo" { 705 cidr_block = "10.1.1.0/24" 706 vpc_id = "${aws_vpc.foo.id}" 707 } 708 709 resource "aws_instance" "foo" { 710 # us-west-2 711 ami = "ami-4fccb37f" 712 instance_type = "m1.small" 713 subnet_id = "${aws_subnet.foo.id}" 714 associate_public_ip_address = true 715 tenancy = "dedicated" 716 } 717 ` 718 719 const testAccInstanceConfigMultipleRegions = ` 720 provider "aws" { 721 alias = "west" 722 region = "us-west-2" 723 } 724 725 provider "aws" { 726 alias = "east" 727 region = "us-east-1" 728 } 729 730 resource "aws_instance" "foo" { 731 # us-west-2 732 provider = "aws.west" 733 ami = "ami-4fccb37f" 734 instance_type = "m1.small" 735 } 736 737 resource "aws_instance" "bar" { 738 # us-east-1 739 provider = "aws.east" 740 ami = "ami-8c6ea9e4" 741 instance_type = "m1.small" 742 } 743 ` 744 745 const testAccCheckInstanceConfigTags = ` 746 resource "aws_instance" "foo" { 747 ami = "ami-4fccb37f" 748 instance_type = "m1.small" 749 tags { 750 foo = "bar" 751 } 752 } 753 ` 754 755 const testAccCheckInstanceConfigTagsUpdate = ` 756 resource "aws_instance" "foo" { 757 ami = "ami-4fccb37f" 758 instance_type = "m1.small" 759 tags { 760 bar = "baz" 761 } 762 } 763 ` 764 765 const testAccInstanceConfigPrivateIP = ` 766 resource "aws_vpc" "foo" { 767 cidr_block = "10.1.0.0/16" 768 } 769 770 resource "aws_subnet" "foo" { 771 cidr_block = "10.1.1.0/24" 772 vpc_id = "${aws_vpc.foo.id}" 773 } 774 775 resource "aws_instance" "foo" { 776 ami = "ami-c5eabbf5" 777 instance_type = "t2.micro" 778 subnet_id = "${aws_subnet.foo.id}" 779 private_ip = "10.1.1.42" 780 } 781 ` 782 783 const testAccInstanceConfigAssociatePublicIPAndPrivateIP = ` 784 resource "aws_vpc" "foo" { 785 cidr_block = "10.1.0.0/16" 786 } 787 788 resource "aws_subnet" "foo" { 789 cidr_block = "10.1.1.0/24" 790 vpc_id = "${aws_vpc.foo.id}" 791 } 792 793 resource "aws_instance" "foo" { 794 ami = "ami-c5eabbf5" 795 instance_type = "t2.micro" 796 subnet_id = "${aws_subnet.foo.id}" 797 associate_public_ip_address = true 798 private_ip = "10.1.1.42" 799 } 800 ` 801 802 const testAccInstanceNetworkInstanceSecurityGroups = ` 803 resource "aws_internet_gateway" "gw" { 804 vpc_id = "${aws_vpc.foo.id}" 805 } 806 807 resource "aws_vpc" "foo" { 808 cidr_block = "10.1.0.0/16" 809 tags { 810 Name = "tf-network-test" 811 } 812 } 813 814 resource "aws_security_group" "tf_test_foo" { 815 name = "tf_test_foo" 816 description = "foo" 817 vpc_id="${aws_vpc.foo.id}" 818 819 ingress { 820 protocol = "icmp" 821 from_port = -1 822 to_port = -1 823 cidr_blocks = ["0.0.0.0/0"] 824 } 825 } 826 827 resource "aws_subnet" "foo" { 828 cidr_block = "10.1.1.0/24" 829 vpc_id = "${aws_vpc.foo.id}" 830 } 831 832 resource "aws_instance" "foo_instance" { 833 ami = "ami-21f78e11" 834 instance_type = "t1.micro" 835 security_groups = ["${aws_security_group.tf_test_foo.id}"] 836 subnet_id = "${aws_subnet.foo.id}" 837 associate_public_ip_address = true 838 depends_on = ["aws_internet_gateway.gw"] 839 } 840 841 resource "aws_eip" "foo_eip" { 842 instance = "${aws_instance.foo_instance.id}" 843 vpc = true 844 depends_on = ["aws_internet_gateway.gw"] 845 } 846 ` 847 848 const testAccInstanceNetworkInstanceVPCSecurityGroupIDs = ` 849 resource "aws_internet_gateway" "gw" { 850 vpc_id = "${aws_vpc.foo.id}" 851 } 852 853 resource "aws_vpc" "foo" { 854 cidr_block = "10.1.0.0/16" 855 tags { 856 Name = "tf-network-test" 857 } 858 } 859 860 resource "aws_security_group" "tf_test_foo" { 861 name = "tf_test_foo" 862 description = "foo" 863 vpc_id="${aws_vpc.foo.id}" 864 865 ingress { 866 protocol = "icmp" 867 from_port = -1 868 to_port = -1 869 cidr_blocks = ["0.0.0.0/0"] 870 } 871 } 872 873 resource "aws_subnet" "foo" { 874 cidr_block = "10.1.1.0/24" 875 vpc_id = "${aws_vpc.foo.id}" 876 } 877 878 resource "aws_instance" "foo_instance" { 879 ami = "ami-21f78e11" 880 instance_type = "t1.micro" 881 vpc_security_group_ids = ["${aws_security_group.tf_test_foo.id}"] 882 subnet_id = "${aws_subnet.foo.id}" 883 depends_on = ["aws_internet_gateway.gw"] 884 } 885 886 resource "aws_eip" "foo_eip" { 887 instance = "${aws_instance.foo_instance.id}" 888 vpc = true 889 depends_on = ["aws_internet_gateway.gw"] 890 } 891 `