github.com/alexissmirnov/terraform@v0.4.3-0.20150423153700-1ef9731a2f14/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/service/ec2" 10 "github.com/hashicorp/terraform/helper/resource" 11 "github.com/hashicorp/terraform/helper/schema" 12 "github.com/hashicorp/terraform/terraform" 13 ) 14 15 func TestAccAWSInstance_normal(t *testing.T) { 16 var v ec2.Instance 17 var vol *ec2.Volume 18 19 testCheck := func(*terraform.State) error { 20 if *v.Placement.AvailabilityZone != "us-west-2a" { 21 return fmt.Errorf("bad availability zone: %#v", *v.Placement.AvailabilityZone) 22 } 23 24 if *v.Placement.GroupName != "terraform-placement-group" { 25 return fmt.Errorf("bad placement group name: %#v", *v.Placement.GroupName) 26 } 27 28 if len(v.SecurityGroups) == 0 { 29 return fmt.Errorf("no security groups: %#v", v.SecurityGroups) 30 } 31 if *v.SecurityGroups[0].GroupName != "tf_test_foo" { 32 return fmt.Errorf("no security groups: %#v", v.SecurityGroups) 33 } 34 35 return nil 36 } 37 38 resource.Test(t, resource.TestCase{ 39 PreCheck: func() { testAccPreCheck(t) }, 40 Providers: testAccProviders, 41 CheckDestroy: testAccCheckInstanceDestroy, 42 Steps: []resource.TestStep{ 43 // Create a volume to cover #1249 44 resource.TestStep{ 45 // Need a resource in this config so the provisioner will be available 46 Config: testAccInstanceConfig_pre, 47 Check: func(*terraform.State) error { 48 conn := testAccProvider.Meta().(*AWSClient).ec2conn 49 var err error 50 vol, err = conn.CreateVolume(&ec2.CreateVolumeInput{ 51 AvailabilityZone: aws.String("us-west-2a"), 52 Size: aws.Long(int64(5)), 53 }) 54 return err 55 }, 56 }, 57 58 resource.TestStep{ 59 Config: testAccInstanceConfig, 60 Check: resource.ComposeTestCheckFunc( 61 testAccCheckInstanceExists( 62 "aws_instance.foo", &v), 63 testCheck, 64 resource.TestCheckResourceAttr( 65 "aws_instance.foo", 66 "user_data", 67 "3dc39dda39be1205215e776bad998da361a5955d"), 68 resource.TestCheckResourceAttr( 69 "aws_instance.foo", "ebs_block_device.#", "0"), 70 ), 71 }, 72 73 // We repeat the exact same test so that we can be sure 74 // that the user data hash stuff is working without generating 75 // an incorrect diff. 76 resource.TestStep{ 77 Config: testAccInstanceConfig, 78 Check: resource.ComposeTestCheckFunc( 79 testAccCheckInstanceExists( 80 "aws_instance.foo", &v), 81 testCheck, 82 resource.TestCheckResourceAttr( 83 "aws_instance.foo", 84 "user_data", 85 "3dc39dda39be1205215e776bad998da361a5955d"), 86 resource.TestCheckResourceAttr( 87 "aws_instance.foo", "ebs_block_device.#", "0"), 88 ), 89 }, 90 91 // Clean up volume created above 92 resource.TestStep{ 93 Config: testAccInstanceConfig, 94 Check: func(*terraform.State) error { 95 conn := testAccProvider.Meta().(*AWSClient).ec2conn 96 _, err := conn.DeleteVolume(&ec2.DeleteVolumeInput{VolumeID: vol.VolumeID}) 97 return err 98 }, 99 }, 100 }, 101 }) 102 } 103 104 func TestAccAWSInstance_blockDevices(t *testing.T) { 105 var v ec2.Instance 106 107 testCheck := func() resource.TestCheckFunc { 108 return func(*terraform.State) error { 109 110 // Map out the block devices by name, which should be unique. 111 blockDevices := make(map[string]*ec2.InstanceBlockDeviceMapping) 112 for _, blockDevice := range v.BlockDeviceMappings { 113 blockDevices[*blockDevice.DeviceName] = blockDevice 114 } 115 116 // Check if the root block device exists. 117 if _, ok := blockDevices["/dev/sda1"]; !ok { 118 fmt.Errorf("block device doesn't exist: /dev/sda1") 119 } 120 121 // Check if the secondary block device exists. 122 if _, ok := blockDevices["/dev/sdb"]; !ok { 123 fmt.Errorf("block device doesn't exist: /dev/sdb") 124 } 125 126 // Check if the third block device exists. 127 if _, ok := blockDevices["/dev/sdc"]; !ok { 128 fmt.Errorf("block device doesn't exist: /dev/sdc") 129 } 130 131 return nil 132 } 133 } 134 135 resource.Test(t, resource.TestCase{ 136 PreCheck: func() { testAccPreCheck(t) }, 137 Providers: testAccProviders, 138 CheckDestroy: testAccCheckInstanceDestroy, 139 Steps: []resource.TestStep{ 140 resource.TestStep{ 141 Config: testAccInstanceConfigBlockDevices, 142 Check: resource.ComposeTestCheckFunc( 143 testAccCheckInstanceExists( 144 "aws_instance.foo", &v), 145 resource.TestCheckResourceAttr( 146 "aws_instance.foo", "root_block_device.#", "1"), 147 resource.TestCheckResourceAttr( 148 "aws_instance.foo", "root_block_device.0.volume_size", "11"), 149 resource.TestCheckResourceAttr( 150 "aws_instance.foo", "root_block_device.0.volume_type", "gp2"), 151 resource.TestCheckResourceAttr( 152 "aws_instance.foo", "ebs_block_device.#", "2"), 153 resource.TestCheckResourceAttr( 154 "aws_instance.foo", "ebs_block_device.2576023345.device_name", "/dev/sdb"), 155 resource.TestCheckResourceAttr( 156 "aws_instance.foo", "ebs_block_device.2576023345.volume_size", "9"), 157 resource.TestCheckResourceAttr( 158 "aws_instance.foo", "ebs_block_device.2576023345.volume_type", "standard"), 159 resource.TestCheckResourceAttr( 160 "aws_instance.foo", "ebs_block_device.2554893574.device_name", "/dev/sdc"), 161 resource.TestCheckResourceAttr( 162 "aws_instance.foo", "ebs_block_device.2554893574.volume_size", "10"), 163 resource.TestCheckResourceAttr( 164 "aws_instance.foo", "ebs_block_device.2554893574.volume_type", "io1"), 165 resource.TestCheckResourceAttr( 166 "aws_instance.foo", "ebs_block_device.2554893574.iops", "100"), 167 resource.TestCheckResourceAttr( 168 "aws_instance.foo", "ephemeral_block_device.#", "1"), 169 resource.TestCheckResourceAttr( 170 "aws_instance.foo", "ephemeral_block_device.1692014856.device_name", "/dev/sde"), 171 resource.TestCheckResourceAttr( 172 "aws_instance.foo", "ephemeral_block_device.1692014856.virtual_name", "ephemeral0"), 173 testCheck(), 174 ), 175 }, 176 }, 177 }) 178 } 179 180 func TestAccAWSInstance_sourceDestCheck(t *testing.T) { 181 var v ec2.Instance 182 183 testCheck := func(enabled bool) resource.TestCheckFunc { 184 return func(*terraform.State) error { 185 if *v.SourceDestCheck != enabled { 186 return fmt.Errorf("bad source_dest_check: %#v", *v.SourceDestCheck) 187 } 188 189 return nil 190 } 191 } 192 193 resource.Test(t, resource.TestCase{ 194 PreCheck: func() { testAccPreCheck(t) }, 195 Providers: testAccProviders, 196 CheckDestroy: testAccCheckInstanceDestroy, 197 Steps: []resource.TestStep{ 198 resource.TestStep{ 199 Config: testAccInstanceConfigSourceDestDisable, 200 Check: resource.ComposeTestCheckFunc( 201 testAccCheckInstanceExists("aws_instance.foo", &v), 202 testCheck(false), 203 ), 204 }, 205 206 resource.TestStep{ 207 Config: testAccInstanceConfigSourceDestEnable, 208 Check: resource.ComposeTestCheckFunc( 209 testAccCheckInstanceExists("aws_instance.foo", &v), 210 testCheck(true), 211 ), 212 }, 213 214 resource.TestStep{ 215 Config: testAccInstanceConfigSourceDestDisable, 216 Check: resource.ComposeTestCheckFunc( 217 testAccCheckInstanceExists("aws_instance.foo", &v), 218 testCheck(false), 219 ), 220 }, 221 }, 222 }) 223 } 224 225 func TestAccAWSInstance_vpc(t *testing.T) { 226 var v ec2.Instance 227 228 resource.Test(t, resource.TestCase{ 229 PreCheck: func() { testAccPreCheck(t) }, 230 Providers: testAccProviders, 231 CheckDestroy: testAccCheckInstanceDestroy, 232 Steps: []resource.TestStep{ 233 resource.TestStep{ 234 Config: testAccInstanceConfigVPC, 235 Check: resource.ComposeTestCheckFunc( 236 testAccCheckInstanceExists( 237 "aws_instance.foo", &v), 238 ), 239 }, 240 }, 241 }) 242 } 243 244 func TestAccAWSInstance_multipleRegions(t *testing.T) { 245 var v ec2.Instance 246 247 // record the initialized providers so that we can use them to 248 // check for the instances in each region 249 var providers []*schema.Provider 250 providerFactories := map[string]terraform.ResourceProviderFactory{ 251 "aws": func() (terraform.ResourceProvider, error) { 252 p := Provider() 253 providers = append(providers, p.(*schema.Provider)) 254 return p, nil 255 }, 256 } 257 258 resource.Test(t, resource.TestCase{ 259 PreCheck: func() { testAccPreCheck(t) }, 260 ProviderFactories: providerFactories, 261 CheckDestroy: testAccCheckInstanceDestroyWithProviders(&providers), 262 Steps: []resource.TestStep{ 263 resource.TestStep{ 264 Config: testAccInstanceConfigMultipleRegions, 265 Check: resource.ComposeTestCheckFunc( 266 testAccCheckInstanceExistsWithProviders( 267 "aws_instance.foo", &v, &providers), 268 testAccCheckInstanceExistsWithProviders( 269 "aws_instance.bar", &v, &providers), 270 ), 271 }, 272 }, 273 }) 274 } 275 276 func TestAccAWSInstance_NetworkInstanceSecurityGroups(t *testing.T) { 277 var v ec2.Instance 278 279 resource.Test(t, resource.TestCase{ 280 PreCheck: func() { testAccPreCheck(t) }, 281 Providers: testAccProviders, 282 CheckDestroy: testAccCheckInstanceDestroy, 283 Steps: []resource.TestStep{ 284 resource.TestStep{ 285 Config: testAccInstanceNetworkInstanceSecurityGroups, 286 Check: resource.ComposeTestCheckFunc( 287 testAccCheckInstanceExists( 288 "aws_instance.foo_instance", &v), 289 ), 290 }, 291 }, 292 }) 293 } 294 295 func TestAccAWSInstance_NetworkInstanceVPCSecurityGroupIDs(t *testing.T) { 296 var v ec2.Instance 297 298 resource.Test(t, resource.TestCase{ 299 PreCheck: func() { testAccPreCheck(t) }, 300 Providers: testAccProviders, 301 CheckDestroy: testAccCheckInstanceDestroy, 302 Steps: []resource.TestStep{ 303 resource.TestStep{ 304 Config: testAccInstanceNetworkInstanceVPCSecurityGroupIDs, 305 Check: resource.ComposeTestCheckFunc( 306 testAccCheckInstanceExists( 307 "aws_instance.foo_instance", &v), 308 ), 309 }, 310 }, 311 }) 312 } 313 314 func TestAccAWSInstance_tags(t *testing.T) { 315 var v ec2.Instance 316 317 resource.Test(t, resource.TestCase{ 318 PreCheck: func() { testAccPreCheck(t) }, 319 Providers: testAccProviders, 320 CheckDestroy: testAccCheckInstanceDestroy, 321 Steps: []resource.TestStep{ 322 resource.TestStep{ 323 Config: testAccCheckInstanceConfigTags, 324 Check: resource.ComposeTestCheckFunc( 325 testAccCheckInstanceExists("aws_instance.foo", &v), 326 testAccCheckTagsSDK(&v.Tags, "foo", "bar"), 327 // Guard against regression of https://github.com/hashicorp/terraform/issues/914 328 testAccCheckTagsSDK(&v.Tags, "#", ""), 329 ), 330 }, 331 332 resource.TestStep{ 333 Config: testAccCheckInstanceConfigTagsUpdate, 334 Check: resource.ComposeTestCheckFunc( 335 testAccCheckInstanceExists("aws_instance.foo", &v), 336 testAccCheckTagsSDK(&v.Tags, "foo", ""), 337 testAccCheckTagsSDK(&v.Tags, "bar", "baz"), 338 ), 339 }, 340 }, 341 }) 342 } 343 344 func TestAccAWSInstance_privateIP(t *testing.T) { 345 var v ec2.Instance 346 347 testCheckPrivateIP := func() resource.TestCheckFunc { 348 return func(*terraform.State) error { 349 if *v.PrivateIPAddress != "10.1.1.42" { 350 return fmt.Errorf("bad private IP: %s", *v.PrivateIPAddress) 351 } 352 353 return nil 354 } 355 } 356 357 resource.Test(t, resource.TestCase{ 358 PreCheck: func() { testAccPreCheck(t) }, 359 Providers: testAccProviders, 360 CheckDestroy: testAccCheckInstanceDestroy, 361 Steps: []resource.TestStep{ 362 resource.TestStep{ 363 Config: testAccInstanceConfigPrivateIP, 364 Check: resource.ComposeTestCheckFunc( 365 testAccCheckInstanceExists("aws_instance.foo", &v), 366 testCheckPrivateIP(), 367 ), 368 }, 369 }, 370 }) 371 } 372 373 func TestAccAWSInstance_associatePublicIPAndPrivateIP(t *testing.T) { 374 var v ec2.Instance 375 376 testCheckPrivateIP := func() resource.TestCheckFunc { 377 return func(*terraform.State) error { 378 if *v.PrivateIPAddress != "10.1.1.42" { 379 return fmt.Errorf("bad private IP: %s", *v.PrivateIPAddress) 380 } 381 382 return nil 383 } 384 } 385 386 resource.Test(t, resource.TestCase{ 387 PreCheck: func() { testAccPreCheck(t) }, 388 Providers: testAccProviders, 389 CheckDestroy: testAccCheckInstanceDestroy, 390 Steps: []resource.TestStep{ 391 resource.TestStep{ 392 Config: testAccInstanceConfigAssociatePublicIPAndPrivateIP, 393 Check: resource.ComposeTestCheckFunc( 394 testAccCheckInstanceExists("aws_instance.foo", &v), 395 testCheckPrivateIP(), 396 ), 397 }, 398 }, 399 }) 400 } 401 402 func testAccCheckInstanceDestroy(s *terraform.State) error { 403 return testAccCheckInstanceDestroyWithProvider(s, testAccProvider) 404 } 405 406 func testAccCheckInstanceDestroyWithProviders(providers *[]*schema.Provider) resource.TestCheckFunc { 407 return func(s *terraform.State) error { 408 for _, provider := range *providers { 409 if provider.Meta() == nil { 410 continue 411 } 412 if err := testAccCheckInstanceDestroyWithProvider(s, provider); err != nil { 413 return err 414 } 415 } 416 return nil 417 } 418 } 419 420 func testAccCheckInstanceDestroyWithProvider(s *terraform.State, provider *schema.Provider) error { 421 conn := provider.Meta().(*AWSClient).ec2conn 422 423 for _, rs := range s.RootModule().Resources { 424 if rs.Type != "aws_instance" { 425 continue 426 } 427 428 // Try to find the resource 429 resp, err := conn.DescribeInstances(&ec2.DescribeInstancesInput{ 430 InstanceIDs: []*string{aws.String(rs.Primary.ID)}, 431 }) 432 if err == nil { 433 if len(resp.Reservations) > 0 { 434 return fmt.Errorf("still exist.") 435 } 436 437 return nil 438 } 439 440 // Verify the error is what we want 441 ec2err, ok := err.(aws.APIError) 442 if !ok { 443 return err 444 } 445 if ec2err.Code != "InvalidInstanceID.NotFound" { 446 return err 447 } 448 } 449 450 return nil 451 } 452 453 func testAccCheckInstanceExists(n string, i *ec2.Instance) resource.TestCheckFunc { 454 providers := []*schema.Provider{testAccProvider} 455 return testAccCheckInstanceExistsWithProviders(n, i, &providers) 456 } 457 458 func testAccCheckInstanceExistsWithProviders(n string, i *ec2.Instance, providers *[]*schema.Provider) resource.TestCheckFunc { 459 return func(s *terraform.State) error { 460 rs, ok := s.RootModule().Resources[n] 461 if !ok { 462 return fmt.Errorf("Not found: %s", n) 463 } 464 465 if rs.Primary.ID == "" { 466 return fmt.Errorf("No ID is set") 467 } 468 for _, provider := range *providers { 469 conn := provider.Meta().(*AWSClient).ec2conn 470 resp, err := conn.DescribeInstances(&ec2.DescribeInstancesInput{ 471 InstanceIDs: []*string{aws.String(rs.Primary.ID)}, 472 }) 473 if ec2err, ok := err.(aws.APIError); ok && ec2err.Code == "InvalidInstanceID.NotFound" { 474 continue 475 } 476 if err != nil { 477 return err 478 } 479 480 if len(resp.Reservations) > 0 { 481 *i = *resp.Reservations[0].Instances[0] 482 return nil 483 } 484 } 485 486 return fmt.Errorf("Instance not found") 487 } 488 } 489 490 func TestInstanceTenancySchema(t *testing.T) { 491 actualSchema := resourceAwsInstance().Schema["tenancy"] 492 expectedSchema := &schema.Schema{ 493 Type: schema.TypeString, 494 Optional: true, 495 Computed: true, 496 ForceNew: true, 497 } 498 if !reflect.DeepEqual(actualSchema, expectedSchema) { 499 t.Fatalf( 500 "Got:\n\n%#v\n\nExpected:\n\n%#v\n", 501 actualSchema, 502 expectedSchema) 503 } 504 } 505 506 const testAccInstanceConfig_pre = ` 507 resource "aws_security_group" "tf_test_foo" { 508 name = "tf_test_foo" 509 description = "foo" 510 511 ingress { 512 protocol = "icmp" 513 from_port = -1 514 to_port = -1 515 cidr_blocks = ["0.0.0.0/0"] 516 } 517 } 518 ` 519 520 const testAccInstanceConfig = ` 521 resource "aws_security_group" "tf_test_foo" { 522 name = "tf_test_foo" 523 description = "foo" 524 525 ingress { 526 protocol = "icmp" 527 from_port = -1 528 to_port = -1 529 cidr_blocks = ["0.0.0.0/0"] 530 } 531 } 532 533 resource "aws_instance" "foo" { 534 # us-west-2 535 ami = "ami-4fccb37f" 536 availability_zone = "us-west-2a" 537 placement_group = "terraform-placement-group" 538 539 instance_type = "m1.small" 540 security_groups = ["${aws_security_group.tf_test_foo.name}"] 541 user_data = "foo:-with-character's" 542 } 543 ` 544 545 const testAccInstanceConfigBlockDevices = ` 546 resource "aws_instance" "foo" { 547 # us-west-2 548 ami = "ami-55a7ea65" 549 instance_type = "m1.small" 550 551 root_block_device { 552 volume_type = "gp2" 553 volume_size = 11 554 } 555 ebs_block_device { 556 device_name = "/dev/sdb" 557 volume_size = 9 558 } 559 ebs_block_device { 560 device_name = "/dev/sdc" 561 volume_size = 10 562 volume_type = "io1" 563 iops = 100 564 } 565 ephemeral_block_device { 566 device_name = "/dev/sde" 567 virtual_name = "ephemeral0" 568 } 569 } 570 ` 571 572 const testAccInstanceConfigSourceDestEnable = ` 573 resource "aws_vpc" "foo" { 574 cidr_block = "10.1.0.0/16" 575 } 576 577 resource "aws_subnet" "foo" { 578 cidr_block = "10.1.1.0/24" 579 vpc_id = "${aws_vpc.foo.id}" 580 } 581 582 resource "aws_instance" "foo" { 583 # us-west-2 584 ami = "ami-4fccb37f" 585 instance_type = "m1.small" 586 subnet_id = "${aws_subnet.foo.id}" 587 source_dest_check = true 588 } 589 ` 590 591 const testAccInstanceConfigSourceDestDisable = ` 592 resource "aws_vpc" "foo" { 593 cidr_block = "10.1.0.0/16" 594 } 595 596 resource "aws_subnet" "foo" { 597 cidr_block = "10.1.1.0/24" 598 vpc_id = "${aws_vpc.foo.id}" 599 } 600 601 resource "aws_instance" "foo" { 602 # us-west-2 603 ami = "ami-4fccb37f" 604 instance_type = "m1.small" 605 subnet_id = "${aws_subnet.foo.id}" 606 source_dest_check = false 607 } 608 ` 609 610 const testAccInstanceConfigVPC = ` 611 resource "aws_vpc" "foo" { 612 cidr_block = "10.1.0.0/16" 613 } 614 615 resource "aws_subnet" "foo" { 616 cidr_block = "10.1.1.0/24" 617 vpc_id = "${aws_vpc.foo.id}" 618 } 619 620 resource "aws_instance" "foo" { 621 # us-west-2 622 ami = "ami-4fccb37f" 623 instance_type = "m1.small" 624 subnet_id = "${aws_subnet.foo.id}" 625 associate_public_ip_address = true 626 tenancy = "dedicated" 627 } 628 ` 629 630 const testAccInstanceConfigMultipleRegions = ` 631 provider "aws" { 632 alias = "west" 633 region = "us-west-2" 634 } 635 636 provider "aws" { 637 alias = "east" 638 region = "us-east-1" 639 } 640 641 resource "aws_instance" "foo" { 642 # us-west-2 643 provider = "aws.west" 644 ami = "ami-4fccb37f" 645 instance_type = "m1.small" 646 } 647 648 resource "aws_instance" "bar" { 649 # us-east-1 650 provider = "aws.east" 651 ami = "ami-8c6ea9e4" 652 instance_type = "m1.small" 653 } 654 ` 655 656 const testAccCheckInstanceConfigTags = ` 657 resource "aws_instance" "foo" { 658 ami = "ami-4fccb37f" 659 instance_type = "m1.small" 660 tags { 661 foo = "bar" 662 } 663 } 664 ` 665 666 const testAccCheckInstanceConfigTagsUpdate = ` 667 resource "aws_instance" "foo" { 668 ami = "ami-4fccb37f" 669 instance_type = "m1.small" 670 tags { 671 bar = "baz" 672 } 673 } 674 ` 675 676 const testAccInstanceConfigPrivateIP = ` 677 resource "aws_vpc" "foo" { 678 cidr_block = "10.1.0.0/16" 679 } 680 681 resource "aws_subnet" "foo" { 682 cidr_block = "10.1.1.0/24" 683 vpc_id = "${aws_vpc.foo.id}" 684 } 685 686 resource "aws_instance" "foo" { 687 ami = "ami-c5eabbf5" 688 instance_type = "t2.micro" 689 subnet_id = "${aws_subnet.foo.id}" 690 private_ip = "10.1.1.42" 691 } 692 ` 693 694 const testAccInstanceConfigAssociatePublicIPAndPrivateIP = ` 695 resource "aws_vpc" "foo" { 696 cidr_block = "10.1.0.0/16" 697 } 698 699 resource "aws_subnet" "foo" { 700 cidr_block = "10.1.1.0/24" 701 vpc_id = "${aws_vpc.foo.id}" 702 } 703 704 resource "aws_instance" "foo" { 705 ami = "ami-c5eabbf5" 706 instance_type = "t2.micro" 707 subnet_id = "${aws_subnet.foo.id}" 708 associate_public_ip_address = true 709 private_ip = "10.1.1.42" 710 } 711 ` 712 713 const testAccInstanceNetworkInstanceSecurityGroups = ` 714 resource "aws_internet_gateway" "gw" { 715 vpc_id = "${aws_vpc.foo.id}" 716 } 717 718 resource "aws_vpc" "foo" { 719 cidr_block = "10.1.0.0/16" 720 tags { 721 Name = "tf-network-test" 722 } 723 } 724 725 resource "aws_security_group" "tf_test_foo" { 726 name = "tf_test_foo" 727 description = "foo" 728 vpc_id="${aws_vpc.foo.id}" 729 730 ingress { 731 protocol = "icmp" 732 from_port = -1 733 to_port = -1 734 cidr_blocks = ["0.0.0.0/0"] 735 } 736 } 737 738 resource "aws_subnet" "foo" { 739 cidr_block = "10.1.1.0/24" 740 vpc_id = "${aws_vpc.foo.id}" 741 } 742 743 resource "aws_instance" "foo_instance" { 744 ami = "ami-21f78e11" 745 instance_type = "t1.micro" 746 security_groups = ["${aws_security_group.tf_test_foo.id}"] 747 subnet_id = "${aws_subnet.foo.id}" 748 associate_public_ip_address = true 749 depends_on = ["aws_internet_gateway.gw"] 750 } 751 752 resource "aws_eip" "foo_eip" { 753 instance = "${aws_instance.foo_instance.id}" 754 vpc = true 755 depends_on = ["aws_internet_gateway.gw"] 756 } 757 ` 758 759 const testAccInstanceNetworkInstanceVPCSecurityGroupIDs = ` 760 resource "aws_internet_gateway" "gw" { 761 vpc_id = "${aws_vpc.foo.id}" 762 } 763 764 resource "aws_vpc" "foo" { 765 cidr_block = "10.1.0.0/16" 766 tags { 767 Name = "tf-network-test" 768 } 769 } 770 771 resource "aws_security_group" "tf_test_foo" { 772 name = "tf_test_foo" 773 description = "foo" 774 vpc_id="${aws_vpc.foo.id}" 775 776 ingress { 777 protocol = "icmp" 778 from_port = -1 779 to_port = -1 780 cidr_blocks = ["0.0.0.0/0"] 781 } 782 } 783 784 resource "aws_subnet" "foo" { 785 cidr_block = "10.1.1.0/24" 786 vpc_id = "${aws_vpc.foo.id}" 787 } 788 789 resource "aws_instance" "foo_instance" { 790 ami = "ami-21f78e11" 791 instance_type = "t1.micro" 792 vpc_security_group_ids = ["${aws_security_group.tf_test_foo.id}"] 793 subnet_id = "${aws_subnet.foo.id}" 794 depends_on = ["aws_internet_gateway.gw"] 795 } 796 797 resource "aws_eip" "foo_eip" { 798 instance = "${aws_instance.foo_instance.id}" 799 vpc = true 800 depends_on = ["aws_internet_gateway.gw"] 801 } 802 `