github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/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/acctest" 12 "github.com/hashicorp/terraform/helper/resource" 13 "github.com/hashicorp/terraform/helper/schema" 14 "github.com/hashicorp/terraform/terraform" 15 ) 16 17 func TestAccAWSInstance_basic(t *testing.T) { 18 var v ec2.Instance 19 var vol *ec2.Volume 20 21 testCheck := func(*terraform.State) error { 22 if *v.Placement.AvailabilityZone != "us-west-2a" { 23 return fmt.Errorf("bad availability zone: %#v", *v.Placement.AvailabilityZone) 24 } 25 26 if len(v.SecurityGroups) == 0 { 27 return fmt.Errorf("no security groups: %#v", v.SecurityGroups) 28 } 29 if *v.SecurityGroups[0].GroupName != "tf_test_foo" { 30 return fmt.Errorf("no security groups: %#v", v.SecurityGroups) 31 } 32 33 return nil 34 } 35 36 resource.Test(t, resource.TestCase{ 37 PreCheck: func() { testAccPreCheck(t) }, 38 39 // We ignore security groups because even with EC2 classic 40 // we'll import as VPC security groups, which is fine. We verify 41 // VPC security group import in other tests 42 IDRefreshName: "aws_instance.foo", 43 IDRefreshIgnore: []string{"security_groups", "vpc_security_group_ids"}, 44 45 Providers: testAccProviders, 46 CheckDestroy: testAccCheckInstanceDestroy, 47 Steps: []resource.TestStep{ 48 // Create a volume to cover #1249 49 { 50 // Need a resource in this config so the provisioner will be available 51 Config: testAccInstanceConfig_pre, 52 Check: func(*terraform.State) error { 53 conn := testAccProvider.Meta().(*AWSClient).ec2conn 54 var err error 55 vol, err = conn.CreateVolume(&ec2.CreateVolumeInput{ 56 AvailabilityZone: aws.String("us-west-2a"), 57 Size: aws.Int64(int64(5)), 58 }) 59 return err 60 }, 61 }, 62 63 { 64 Config: testAccInstanceConfig, 65 Check: resource.ComposeTestCheckFunc( 66 testAccCheckInstanceExists( 67 "aws_instance.foo", &v), 68 testCheck, 69 resource.TestCheckResourceAttr( 70 "aws_instance.foo", 71 "user_data", 72 "3dc39dda39be1205215e776bad998da361a5955d"), 73 resource.TestCheckResourceAttr( 74 "aws_instance.foo", "ebs_block_device.#", "0"), 75 ), 76 }, 77 78 // We repeat the exact same test so that we can be sure 79 // that the user data hash stuff is working without generating 80 // an incorrect diff. 81 { 82 Config: testAccInstanceConfig, 83 Check: resource.ComposeTestCheckFunc( 84 testAccCheckInstanceExists( 85 "aws_instance.foo", &v), 86 testCheck, 87 resource.TestCheckResourceAttr( 88 "aws_instance.foo", 89 "user_data", 90 "3dc39dda39be1205215e776bad998da361a5955d"), 91 resource.TestCheckResourceAttr( 92 "aws_instance.foo", "ebs_block_device.#", "0"), 93 ), 94 }, 95 96 // Clean up volume created above 97 { 98 Config: testAccInstanceConfig, 99 Check: func(*terraform.State) error { 100 conn := testAccProvider.Meta().(*AWSClient).ec2conn 101 _, err := conn.DeleteVolume(&ec2.DeleteVolumeInput{VolumeId: vol.VolumeId}) 102 return err 103 }, 104 }, 105 }, 106 }) 107 } 108 109 func TestAccAWSInstance_GP2IopsDevice(t *testing.T) { 110 var v ec2.Instance 111 112 testCheck := func() resource.TestCheckFunc { 113 return func(*terraform.State) error { 114 115 // Map out the block devices by name, which should be unique. 116 blockDevices := make(map[string]*ec2.InstanceBlockDeviceMapping) 117 for _, blockDevice := range v.BlockDeviceMappings { 118 blockDevices[*blockDevice.DeviceName] = blockDevice 119 } 120 121 // Check if the root block device exists. 122 if _, ok := blockDevices["/dev/sda1"]; !ok { 123 return fmt.Errorf("block device doesn't exist: /dev/sda1") 124 } 125 126 return nil 127 } 128 } 129 130 resource.Test(t, resource.TestCase{ 131 PreCheck: func() { testAccPreCheck(t) }, 132 IDRefreshName: "aws_instance.foo", 133 IDRefreshIgnore: []string{ 134 "ephemeral_block_device", "user_data", "security_groups", "vpc_security_groups"}, 135 Providers: testAccProviders, 136 CheckDestroy: testAccCheckInstanceDestroy, 137 Steps: []resource.TestStep{ 138 { 139 Config: testAccInstanceGP2IopsDevice, 140 //Config: testAccInstanceConfigBlockDevices, 141 Check: resource.ComposeTestCheckFunc( 142 testAccCheckInstanceExists( 143 "aws_instance.foo", &v), 144 resource.TestCheckResourceAttr( 145 "aws_instance.foo", "root_block_device.#", "1"), 146 resource.TestCheckResourceAttr( 147 "aws_instance.foo", "root_block_device.0.volume_size", "11"), 148 resource.TestCheckResourceAttr( 149 "aws_instance.foo", "root_block_device.0.volume_type", "gp2"), 150 resource.TestCheckResourceAttr( 151 "aws_instance.foo", "root_block_device.0.iops", "100"), 152 testCheck(), 153 ), 154 }, 155 }, 156 }) 157 } 158 159 func TestAccAWSInstance_blockDevices(t *testing.T) { 160 var v ec2.Instance 161 162 testCheck := func() resource.TestCheckFunc { 163 return func(*terraform.State) error { 164 165 // Map out the block devices by name, which should be unique. 166 blockDevices := make(map[string]*ec2.InstanceBlockDeviceMapping) 167 for _, blockDevice := range v.BlockDeviceMappings { 168 blockDevices[*blockDevice.DeviceName] = blockDevice 169 } 170 171 // Check if the root block device exists. 172 if _, ok := blockDevices["/dev/sda1"]; !ok { 173 return fmt.Errorf("block device doesn't exist: /dev/sda1") 174 } 175 176 // Check if the secondary block device exists. 177 if _, ok := blockDevices["/dev/sdb"]; !ok { 178 return fmt.Errorf("block device doesn't exist: /dev/sdb") 179 } 180 181 // Check if the third block device exists. 182 if _, ok := blockDevices["/dev/sdc"]; !ok { 183 return fmt.Errorf("block device doesn't exist: /dev/sdc") 184 } 185 186 // Check if the encrypted block device exists 187 if _, ok := blockDevices["/dev/sdd"]; !ok { 188 return fmt.Errorf("block device doesn't exist: /dev/sdd") 189 } 190 191 return nil 192 } 193 } 194 195 resource.Test(t, resource.TestCase{ 196 PreCheck: func() { testAccPreCheck(t) }, 197 IDRefreshName: "aws_instance.foo", 198 IDRefreshIgnore: []string{ 199 "ephemeral_block_device", "security_groups", "vpc_security_groups"}, 200 Providers: testAccProviders, 201 CheckDestroy: testAccCheckInstanceDestroy, 202 Steps: []resource.TestStep{ 203 { 204 Config: testAccInstanceConfigBlockDevices, 205 Check: resource.ComposeTestCheckFunc( 206 testAccCheckInstanceExists( 207 "aws_instance.foo", &v), 208 resource.TestCheckResourceAttr( 209 "aws_instance.foo", "root_block_device.#", "1"), 210 resource.TestCheckResourceAttr( 211 "aws_instance.foo", "root_block_device.0.volume_size", "11"), 212 resource.TestCheckResourceAttr( 213 "aws_instance.foo", "root_block_device.0.volume_type", "gp2"), 214 resource.TestCheckResourceAttr( 215 "aws_instance.foo", "ebs_block_device.#", "3"), 216 resource.TestCheckResourceAttr( 217 "aws_instance.foo", "ebs_block_device.2576023345.device_name", "/dev/sdb"), 218 resource.TestCheckResourceAttr( 219 "aws_instance.foo", "ebs_block_device.2576023345.volume_size", "9"), 220 resource.TestCheckResourceAttr( 221 "aws_instance.foo", "ebs_block_device.2576023345.volume_type", "standard"), 222 resource.TestCheckResourceAttr( 223 "aws_instance.foo", "ebs_block_device.2554893574.device_name", "/dev/sdc"), 224 resource.TestCheckResourceAttr( 225 "aws_instance.foo", "ebs_block_device.2554893574.volume_size", "10"), 226 resource.TestCheckResourceAttr( 227 "aws_instance.foo", "ebs_block_device.2554893574.volume_type", "io1"), 228 resource.TestCheckResourceAttr( 229 "aws_instance.foo", "ebs_block_device.2554893574.iops", "100"), 230 resource.TestCheckResourceAttr( 231 "aws_instance.foo", "ebs_block_device.2634515331.device_name", "/dev/sdd"), 232 resource.TestCheckResourceAttr( 233 "aws_instance.foo", "ebs_block_device.2634515331.encrypted", "true"), 234 resource.TestCheckResourceAttr( 235 "aws_instance.foo", "ebs_block_device.2634515331.volume_size", "12"), 236 resource.TestCheckResourceAttr( 237 "aws_instance.foo", "ephemeral_block_device.#", "1"), 238 resource.TestCheckResourceAttr( 239 "aws_instance.foo", "ephemeral_block_device.1692014856.device_name", "/dev/sde"), 240 resource.TestCheckResourceAttr( 241 "aws_instance.foo", "ephemeral_block_device.1692014856.virtual_name", "ephemeral0"), 242 testCheck(), 243 ), 244 }, 245 }, 246 }) 247 } 248 249 func TestAccAWSInstance_rootInstanceStore(t *testing.T) { 250 var v ec2.Instance 251 252 resource.Test(t, resource.TestCase{ 253 PreCheck: func() { testAccPreCheck(t) }, 254 IDRefreshName: "aws_instance.foo", 255 Providers: testAccProviders, 256 CheckDestroy: testAccCheckInstanceDestroy, 257 Steps: []resource.TestStep{ 258 { 259 Config: ` 260 resource "aws_instance" "foo" { 261 # us-west-2 262 # Amazon Linux HVM Instance Store 64-bit (2016.09.0) 263 # https://aws.amazon.com/amazon-linux-ami 264 ami = "ami-44c36524" 265 266 # Only certain instance types support ephemeral root instance stores. 267 # http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/InstanceStorage.html 268 instance_type = "m3.medium" 269 }`, 270 Check: resource.ComposeTestCheckFunc( 271 testAccCheckInstanceExists( 272 "aws_instance.foo", &v), 273 resource.TestCheckResourceAttr( 274 "aws_instance.foo", "ami", "ami-44c36524"), 275 resource.TestCheckResourceAttr( 276 "aws_instance.foo", "ebs_block_device.#", "0"), 277 resource.TestCheckResourceAttr( 278 "aws_instance.foo", "ebs_optimized", "false"), 279 resource.TestCheckResourceAttr( 280 "aws_instance.foo", "instance_type", "m3.medium"), 281 resource.TestCheckResourceAttr( 282 "aws_instance.foo", "root_block_device.#", "0"), 283 ), 284 }, 285 }, 286 }) 287 } 288 289 func TestAcctABSInstance_noAMIEphemeralDevices(t *testing.T) { 290 var v ec2.Instance 291 292 testCheck := func() resource.TestCheckFunc { 293 return func(*terraform.State) error { 294 295 // Map out the block devices by name, which should be unique. 296 blockDevices := make(map[string]*ec2.InstanceBlockDeviceMapping) 297 for _, blockDevice := range v.BlockDeviceMappings { 298 blockDevices[*blockDevice.DeviceName] = blockDevice 299 } 300 301 // Check if the root block device exists. 302 if _, ok := blockDevices["/dev/sda1"]; !ok { 303 return fmt.Errorf("block device doesn't exist: /dev/sda1") 304 } 305 306 // Check if the secondary block not exists. 307 if _, ok := blockDevices["/dev/sdb"]; ok { 308 return fmt.Errorf("block device exist: /dev/sdb") 309 } 310 311 // Check if the third block device not exists. 312 if _, ok := blockDevices["/dev/sdc"]; ok { 313 return fmt.Errorf("block device exist: /dev/sdc") 314 } 315 return nil 316 } 317 } 318 319 resource.Test(t, resource.TestCase{ 320 PreCheck: func() { testAccPreCheck(t) }, 321 IDRefreshName: "aws_instance.foo", 322 IDRefreshIgnore: []string{ 323 "ephemeral_block_device", "security_groups", "vpc_security_groups"}, 324 Providers: testAccProviders, 325 CheckDestroy: testAccCheckInstanceDestroy, 326 Steps: []resource.TestStep{ 327 { 328 Config: ` 329 resource "aws_instance" "foo" { 330 # us-west-2 331 ami = "ami-01f05461" // This AMI (Ubuntu) contains two ephemerals 332 333 instance_type = "c3.large" 334 335 root_block_device { 336 volume_type = "gp2" 337 volume_size = 11 338 } 339 ephemeral_block_device { 340 device_name = "/dev/sdb" 341 no_device = true 342 } 343 ephemeral_block_device { 344 device_name = "/dev/sdc" 345 no_device = true 346 } 347 }`, 348 Check: resource.ComposeTestCheckFunc( 349 testAccCheckInstanceExists( 350 "aws_instance.foo", &v), 351 resource.TestCheckResourceAttr( 352 "aws_instance.foo", "ami", "ami-01f05461"), 353 resource.TestCheckResourceAttr( 354 "aws_instance.foo", "ebs_optimized", "false"), 355 resource.TestCheckResourceAttr( 356 "aws_instance.foo", "instance_type", "c3.large"), 357 resource.TestCheckResourceAttr( 358 "aws_instance.foo", "root_block_device.#", "1"), 359 resource.TestCheckResourceAttr( 360 "aws_instance.foo", "root_block_device.0.volume_size", "11"), 361 resource.TestCheckResourceAttr( 362 "aws_instance.foo", "root_block_device.0.volume_type", "gp2"), 363 resource.TestCheckResourceAttr( 364 "aws_instance.foo", "ebs_block_device.#", "0"), 365 resource.TestCheckResourceAttr( 366 "aws_instance.foo", "ephemeral_block_device.#", "2"), 367 resource.TestCheckResourceAttr( 368 "aws_instance.foo", "ephemeral_block_device.172787947.device_name", "/dev/sdb"), 369 resource.TestCheckResourceAttr( 370 "aws_instance.foo", "ephemeral_block_device.172787947.no_device", "true"), 371 resource.TestCheckResourceAttr( 372 "aws_instance.foo", "ephemeral_block_device.3336996981.device_name", "/dev/sdc"), 373 resource.TestCheckResourceAttr( 374 "aws_instance.foo", "ephemeral_block_device.3336996981.no_device", "true"), 375 testCheck(), 376 ), 377 }, 378 }, 379 }) 380 } 381 382 func TestAccAWSInstance_sourceDestCheck(t *testing.T) { 383 var v ec2.Instance 384 385 testCheck := func(enabled bool) resource.TestCheckFunc { 386 return func(*terraform.State) error { 387 if v.SourceDestCheck == nil { 388 return fmt.Errorf("bad source_dest_check: got nil") 389 } 390 if *v.SourceDestCheck != enabled { 391 return fmt.Errorf("bad source_dest_check: %#v", *v.SourceDestCheck) 392 } 393 394 return nil 395 } 396 } 397 398 resource.Test(t, resource.TestCase{ 399 PreCheck: func() { testAccPreCheck(t) }, 400 IDRefreshName: "aws_instance.foo", 401 Providers: testAccProviders, 402 CheckDestroy: testAccCheckInstanceDestroy, 403 Steps: []resource.TestStep{ 404 { 405 Config: testAccInstanceConfigSourceDestDisable, 406 Check: resource.ComposeTestCheckFunc( 407 testAccCheckInstanceExists("aws_instance.foo", &v), 408 testCheck(false), 409 ), 410 }, 411 412 { 413 Config: testAccInstanceConfigSourceDestEnable, 414 Check: resource.ComposeTestCheckFunc( 415 testAccCheckInstanceExists("aws_instance.foo", &v), 416 testCheck(true), 417 ), 418 }, 419 420 { 421 Config: testAccInstanceConfigSourceDestDisable, 422 Check: resource.ComposeTestCheckFunc( 423 testAccCheckInstanceExists("aws_instance.foo", &v), 424 testCheck(false), 425 ), 426 }, 427 }, 428 }) 429 } 430 431 func TestAccAWSInstance_disableApiTermination(t *testing.T) { 432 var v ec2.Instance 433 434 checkDisableApiTermination := func(expected bool) resource.TestCheckFunc { 435 return func(*terraform.State) error { 436 conn := testAccProvider.Meta().(*AWSClient).ec2conn 437 r, err := conn.DescribeInstanceAttribute(&ec2.DescribeInstanceAttributeInput{ 438 InstanceId: v.InstanceId, 439 Attribute: aws.String("disableApiTermination"), 440 }) 441 if err != nil { 442 return err 443 } 444 got := *r.DisableApiTermination.Value 445 if got != expected { 446 return fmt.Errorf("expected: %t, got: %t", expected, got) 447 } 448 return nil 449 } 450 } 451 452 resource.Test(t, resource.TestCase{ 453 PreCheck: func() { testAccPreCheck(t) }, 454 IDRefreshName: "aws_instance.foo", 455 Providers: testAccProviders, 456 CheckDestroy: testAccCheckInstanceDestroy, 457 Steps: []resource.TestStep{ 458 { 459 Config: testAccInstanceConfigDisableAPITermination(true), 460 Check: resource.ComposeTestCheckFunc( 461 testAccCheckInstanceExists("aws_instance.foo", &v), 462 checkDisableApiTermination(true), 463 ), 464 }, 465 466 { 467 Config: testAccInstanceConfigDisableAPITermination(false), 468 Check: resource.ComposeTestCheckFunc( 469 testAccCheckInstanceExists("aws_instance.foo", &v), 470 checkDisableApiTermination(false), 471 ), 472 }, 473 }, 474 }) 475 } 476 477 func TestAccAWSInstance_vpc(t *testing.T) { 478 var v ec2.Instance 479 480 resource.Test(t, resource.TestCase{ 481 PreCheck: func() { testAccPreCheck(t) }, 482 IDRefreshName: "aws_instance.foo", 483 IDRefreshIgnore: []string{"associate_public_ip_address"}, 484 Providers: testAccProviders, 485 CheckDestroy: testAccCheckInstanceDestroy, 486 Steps: []resource.TestStep{ 487 { 488 Config: testAccInstanceConfigVPC, 489 Check: resource.ComposeTestCheckFunc( 490 testAccCheckInstanceExists( 491 "aws_instance.foo", &v), 492 resource.TestCheckResourceAttr( 493 "aws_instance.foo", 494 "user_data", 495 "562a3e32810edf6ff09994f050f12e799452379d"), 496 ), 497 }, 498 }, 499 }) 500 } 501 502 func TestAccAWSInstance_ipv6_supportAddressCount(t *testing.T) { 503 var v ec2.Instance 504 505 resource.Test(t, resource.TestCase{ 506 PreCheck: func() { testAccPreCheck(t) }, 507 Providers: testAccProviders, 508 CheckDestroy: testAccCheckInstanceDestroy, 509 Steps: []resource.TestStep{ 510 { 511 Config: testAccInstanceConfigIpv6Support, 512 Check: resource.ComposeTestCheckFunc( 513 testAccCheckInstanceExists( 514 "aws_instance.foo", &v), 515 resource.TestCheckResourceAttr( 516 "aws_instance.foo", 517 "ipv6_address_count", 518 "1"), 519 ), 520 }, 521 }, 522 }) 523 } 524 525 func TestAccAWSInstance_multipleRegions(t *testing.T) { 526 var v ec2.Instance 527 528 // record the initialized providers so that we can use them to 529 // check for the instances in each region 530 var providers []*schema.Provider 531 providerFactories := map[string]terraform.ResourceProviderFactory{ 532 "aws": func() (terraform.ResourceProvider, error) { 533 p := Provider() 534 providers = append(providers, p.(*schema.Provider)) 535 return p, nil 536 }, 537 } 538 539 resource.Test(t, resource.TestCase{ 540 PreCheck: func() { testAccPreCheck(t) }, 541 ProviderFactories: providerFactories, 542 CheckDestroy: testAccCheckInstanceDestroyWithProviders(&providers), 543 Steps: []resource.TestStep{ 544 { 545 Config: testAccInstanceConfigMultipleRegions, 546 Check: resource.ComposeTestCheckFunc( 547 testAccCheckInstanceExistsWithProviders( 548 "aws_instance.foo", &v, &providers), 549 testAccCheckInstanceExistsWithProviders( 550 "aws_instance.bar", &v, &providers), 551 ), 552 }, 553 }, 554 }) 555 } 556 557 func TestAccAWSInstance_NetworkInstanceSecurityGroups(t *testing.T) { 558 var v ec2.Instance 559 560 resource.Test(t, resource.TestCase{ 561 PreCheck: func() { testAccPreCheck(t) }, 562 IDRefreshName: "aws_instance.foo_instance", 563 IDRefreshIgnore: []string{"associate_public_ip_address"}, 564 Providers: testAccProviders, 565 CheckDestroy: testAccCheckInstanceDestroy, 566 Steps: []resource.TestStep{ 567 { 568 Config: testAccInstanceNetworkInstanceSecurityGroups, 569 Check: resource.ComposeTestCheckFunc( 570 testAccCheckInstanceExists( 571 "aws_instance.foo_instance", &v), 572 ), 573 }, 574 }, 575 }) 576 } 577 578 func TestAccAWSInstance_NetworkInstanceVPCSecurityGroupIDs(t *testing.T) { 579 var v ec2.Instance 580 581 resource.Test(t, resource.TestCase{ 582 PreCheck: func() { testAccPreCheck(t) }, 583 IDRefreshName: "aws_instance.foo_instance", 584 Providers: testAccProviders, 585 CheckDestroy: testAccCheckInstanceDestroy, 586 Steps: []resource.TestStep{ 587 { 588 Config: testAccInstanceNetworkInstanceVPCSecurityGroupIDs, 589 Check: resource.ComposeTestCheckFunc( 590 testAccCheckInstanceExists( 591 "aws_instance.foo_instance", &v), 592 resource.TestCheckResourceAttr( 593 "aws_instance.foo_instance", "security_groups.#", "0"), 594 resource.TestCheckResourceAttr( 595 "aws_instance.foo_instance", "vpc_security_group_ids.#", "1"), 596 ), 597 }, 598 }, 599 }) 600 } 601 602 func TestAccAWSInstance_tags(t *testing.T) { 603 var v ec2.Instance 604 605 resource.Test(t, resource.TestCase{ 606 PreCheck: func() { testAccPreCheck(t) }, 607 Providers: testAccProviders, 608 CheckDestroy: testAccCheckInstanceDestroy, 609 Steps: []resource.TestStep{ 610 { 611 Config: testAccCheckInstanceConfigTags, 612 Check: resource.ComposeTestCheckFunc( 613 testAccCheckInstanceExists("aws_instance.foo", &v), 614 testAccCheckTags(&v.Tags, "foo", "bar"), 615 // Guard against regression of https://github.com/hashicorp/terraform/issues/914 616 testAccCheckTags(&v.Tags, "#", ""), 617 ), 618 }, 619 620 { 621 Config: testAccCheckInstanceConfigTagsUpdate, 622 Check: resource.ComposeTestCheckFunc( 623 testAccCheckInstanceExists("aws_instance.foo", &v), 624 testAccCheckTags(&v.Tags, "foo", ""), 625 testAccCheckTags(&v.Tags, "bar", "baz"), 626 ), 627 }, 628 }, 629 }) 630 } 631 632 func TestAccAWSInstance_instanceProfileChange(t *testing.T) { 633 var v ec2.Instance 634 rName := acctest.RandString(5) 635 636 testCheckInstanceProfile := func() resource.TestCheckFunc { 637 return func(*terraform.State) error { 638 if v.IamInstanceProfile == nil { 639 return fmt.Errorf("Instance Profile is nil - we expected an InstanceProfile associated with the Instance") 640 } 641 642 return nil 643 } 644 } 645 646 resource.Test(t, resource.TestCase{ 647 PreCheck: func() { testAccPreCheck(t) }, 648 IDRefreshName: "aws_instance.foo", 649 Providers: testAccProviders, 650 CheckDestroy: testAccCheckInstanceDestroy, 651 Steps: []resource.TestStep{ 652 { 653 Config: testAccInstanceConfigWithoutInstanceProfile(rName), 654 Check: resource.ComposeTestCheckFunc( 655 testAccCheckInstanceExists("aws_instance.foo", &v), 656 ), 657 }, 658 { 659 Config: testAccInstanceConfigAttachInstanceProfile(rName), 660 Check: resource.ComposeTestCheckFunc( 661 testAccCheckInstanceExists("aws_instance.foo", &v), 662 testCheckInstanceProfile(), 663 ), 664 }, 665 }, 666 }) 667 } 668 669 func TestAccAWSInstance_privateIP(t *testing.T) { 670 var v ec2.Instance 671 672 testCheckPrivateIP := func() resource.TestCheckFunc { 673 return func(*terraform.State) error { 674 if *v.PrivateIpAddress != "10.1.1.42" { 675 return fmt.Errorf("bad private IP: %s", *v.PrivateIpAddress) 676 } 677 678 return nil 679 } 680 } 681 682 resource.Test(t, resource.TestCase{ 683 PreCheck: func() { testAccPreCheck(t) }, 684 IDRefreshName: "aws_instance.foo", 685 Providers: testAccProviders, 686 CheckDestroy: testAccCheckInstanceDestroy, 687 Steps: []resource.TestStep{ 688 { 689 Config: testAccInstanceConfigPrivateIP, 690 Check: resource.ComposeTestCheckFunc( 691 testAccCheckInstanceExists("aws_instance.foo", &v), 692 testCheckPrivateIP(), 693 ), 694 }, 695 }, 696 }) 697 } 698 699 func TestAccAWSInstance_associatePublicIPAndPrivateIP(t *testing.T) { 700 var v ec2.Instance 701 702 testCheckPrivateIP := func() resource.TestCheckFunc { 703 return func(*terraform.State) error { 704 if *v.PrivateIpAddress != "10.1.1.42" { 705 return fmt.Errorf("bad private IP: %s", *v.PrivateIpAddress) 706 } 707 708 return nil 709 } 710 } 711 712 resource.Test(t, resource.TestCase{ 713 PreCheck: func() { testAccPreCheck(t) }, 714 IDRefreshName: "aws_instance.foo", 715 IDRefreshIgnore: []string{"associate_public_ip_address"}, 716 Providers: testAccProviders, 717 CheckDestroy: testAccCheckInstanceDestroy, 718 Steps: []resource.TestStep{ 719 { 720 Config: testAccInstanceConfigAssociatePublicIPAndPrivateIP, 721 Check: resource.ComposeTestCheckFunc( 722 testAccCheckInstanceExists("aws_instance.foo", &v), 723 testCheckPrivateIP(), 724 ), 725 }, 726 }, 727 }) 728 } 729 730 // Guard against regression with KeyPairs 731 // https://github.com/hashicorp/terraform/issues/2302 732 func TestAccAWSInstance_keyPairCheck(t *testing.T) { 733 var v ec2.Instance 734 735 testCheckKeyPair := func(keyName string) resource.TestCheckFunc { 736 return func(*terraform.State) error { 737 if v.KeyName == nil { 738 return fmt.Errorf("No Key Pair found, expected(%s)", keyName) 739 } 740 if v.KeyName != nil && *v.KeyName != keyName { 741 return fmt.Errorf("Bad key name, expected (%s), got (%s)", keyName, *v.KeyName) 742 } 743 744 return nil 745 } 746 } 747 748 resource.Test(t, resource.TestCase{ 749 PreCheck: func() { testAccPreCheck(t) }, 750 IDRefreshName: "aws_instance.foo", 751 IDRefreshIgnore: []string{"source_dest_check"}, 752 Providers: testAccProviders, 753 CheckDestroy: testAccCheckInstanceDestroy, 754 Steps: []resource.TestStep{ 755 { 756 Config: testAccInstanceConfigKeyPair, 757 Check: resource.ComposeTestCheckFunc( 758 testAccCheckInstanceExists("aws_instance.foo", &v), 759 testCheckKeyPair("tmp-key"), 760 ), 761 }, 762 }, 763 }) 764 } 765 766 func TestAccAWSInstance_rootBlockDeviceMismatch(t *testing.T) { 767 var v ec2.Instance 768 769 resource.Test(t, resource.TestCase{ 770 PreCheck: func() { testAccPreCheck(t) }, 771 Providers: testAccProviders, 772 CheckDestroy: testAccCheckInstanceDestroy, 773 Steps: []resource.TestStep{ 774 { 775 Config: testAccInstanceConfigRootBlockDeviceMismatch, 776 Check: resource.ComposeTestCheckFunc( 777 testAccCheckInstanceExists("aws_instance.foo", &v), 778 resource.TestCheckResourceAttr( 779 "aws_instance.foo", "root_block_device.0.volume_size", "13"), 780 ), 781 }, 782 }, 783 }) 784 } 785 786 // This test reproduces the bug here: 787 // https://github.com/hashicorp/terraform/issues/1752 788 // 789 // I wish there were a way to exercise resources built with helper.Schema in a 790 // unit context, in which case this test could be moved there, but for now this 791 // will cover the bugfix. 792 // 793 // The following triggers "diffs didn't match during apply" without the fix in to 794 // set NewRemoved on the .# field when it changes to 0. 795 func TestAccAWSInstance_forceNewAndTagsDrift(t *testing.T) { 796 var v ec2.Instance 797 798 resource.Test(t, resource.TestCase{ 799 PreCheck: func() { testAccPreCheck(t) }, 800 IDRefreshName: "aws_instance.foo", 801 Providers: testAccProviders, 802 CheckDestroy: testAccCheckInstanceDestroy, 803 Steps: []resource.TestStep{ 804 { 805 Config: testAccInstanceConfigForceNewAndTagsDrift, 806 Check: resource.ComposeTestCheckFunc( 807 testAccCheckInstanceExists("aws_instance.foo", &v), 808 driftTags(&v), 809 ), 810 ExpectNonEmptyPlan: true, 811 }, 812 { 813 Config: testAccInstanceConfigForceNewAndTagsDrift_Update, 814 Check: resource.ComposeTestCheckFunc( 815 testAccCheckInstanceExists("aws_instance.foo", &v), 816 ), 817 }, 818 }, 819 }) 820 } 821 822 func TestAccAWSInstance_changeInstanceType(t *testing.T) { 823 var before ec2.Instance 824 var after ec2.Instance 825 826 resource.Test(t, resource.TestCase{ 827 PreCheck: func() { testAccPreCheck(t) }, 828 Providers: testAccProviders, 829 CheckDestroy: testAccCheckInstanceDestroy, 830 Steps: []resource.TestStep{ 831 { 832 Config: testAccInstanceConfigWithSmallInstanceType, 833 Check: resource.ComposeTestCheckFunc( 834 testAccCheckInstanceExists("aws_instance.foo", &before), 835 ), 836 }, 837 { 838 Config: testAccInstanceConfigUpdateInstanceType, 839 Check: resource.ComposeTestCheckFunc( 840 testAccCheckInstanceExists("aws_instance.foo", &after), 841 testAccCheckInstanceNotRecreated( 842 t, &before, &after), 843 ), 844 }, 845 }, 846 }) 847 } 848 849 func testAccCheckInstanceNotRecreated(t *testing.T, 850 before, after *ec2.Instance) resource.TestCheckFunc { 851 return func(s *terraform.State) error { 852 if *before.InstanceId != *after.InstanceId { 853 t.Fatalf("AWS Instance IDs have changed. Before %s. After %s", *before.InstanceId, *after.InstanceId) 854 } 855 return nil 856 } 857 } 858 859 func testAccCheckInstanceDestroy(s *terraform.State) error { 860 return testAccCheckInstanceDestroyWithProvider(s, testAccProvider) 861 } 862 863 func testAccCheckInstanceDestroyWithProviders(providers *[]*schema.Provider) resource.TestCheckFunc { 864 return func(s *terraform.State) error { 865 for _, provider := range *providers { 866 if provider.Meta() == nil { 867 continue 868 } 869 if err := testAccCheckInstanceDestroyWithProvider(s, provider); err != nil { 870 return err 871 } 872 } 873 return nil 874 } 875 } 876 877 func testAccCheckInstanceDestroyWithProvider(s *terraform.State, provider *schema.Provider) error { 878 conn := provider.Meta().(*AWSClient).ec2conn 879 880 for _, rs := range s.RootModule().Resources { 881 if rs.Type != "aws_instance" { 882 continue 883 } 884 885 // Try to find the resource 886 resp, err := conn.DescribeInstances(&ec2.DescribeInstancesInput{ 887 InstanceIds: []*string{aws.String(rs.Primary.ID)}, 888 }) 889 if err == nil { 890 for _, r := range resp.Reservations { 891 for _, i := range r.Instances { 892 if i.State != nil && *i.State.Name != "terminated" { 893 return fmt.Errorf("Found unterminated instance: %s", i) 894 } 895 } 896 } 897 } 898 899 // Verify the error is what we want 900 if ae, ok := err.(awserr.Error); ok && ae.Code() == "InvalidInstanceID.NotFound" { 901 continue 902 } 903 904 return err 905 } 906 907 return nil 908 } 909 910 func testAccCheckInstanceExists(n string, i *ec2.Instance) resource.TestCheckFunc { 911 providers := []*schema.Provider{testAccProvider} 912 return testAccCheckInstanceExistsWithProviders(n, i, &providers) 913 } 914 915 func testAccCheckInstanceExistsWithProviders(n string, i *ec2.Instance, providers *[]*schema.Provider) resource.TestCheckFunc { 916 return func(s *terraform.State) error { 917 rs, ok := s.RootModule().Resources[n] 918 if !ok { 919 return fmt.Errorf("Not found: %s", n) 920 } 921 922 if rs.Primary.ID == "" { 923 return fmt.Errorf("No ID is set") 924 } 925 for _, provider := range *providers { 926 // Ignore if Meta is empty, this can happen for validation providers 927 if provider.Meta() == nil { 928 continue 929 } 930 931 conn := provider.Meta().(*AWSClient).ec2conn 932 resp, err := conn.DescribeInstances(&ec2.DescribeInstancesInput{ 933 InstanceIds: []*string{aws.String(rs.Primary.ID)}, 934 }) 935 if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "InvalidInstanceID.NotFound" { 936 continue 937 } 938 if err != nil { 939 return err 940 } 941 942 if len(resp.Reservations) > 0 { 943 *i = *resp.Reservations[0].Instances[0] 944 return nil 945 } 946 } 947 948 return fmt.Errorf("Instance not found") 949 } 950 } 951 952 func TestInstanceTenancySchema(t *testing.T) { 953 actualSchema := resourceAwsInstance().Schema["tenancy"] 954 expectedSchema := &schema.Schema{ 955 Type: schema.TypeString, 956 Optional: true, 957 Computed: true, 958 ForceNew: true, 959 } 960 if !reflect.DeepEqual(actualSchema, expectedSchema) { 961 t.Fatalf( 962 "Got:\n\n%#v\n\nExpected:\n\n%#v\n", 963 actualSchema, 964 expectedSchema) 965 } 966 } 967 968 func driftTags(instance *ec2.Instance) resource.TestCheckFunc { 969 return func(s *terraform.State) error { 970 conn := testAccProvider.Meta().(*AWSClient).ec2conn 971 _, err := conn.CreateTags(&ec2.CreateTagsInput{ 972 Resources: []*string{instance.InstanceId}, 973 Tags: []*ec2.Tag{ 974 { 975 Key: aws.String("Drift"), 976 Value: aws.String("Happens"), 977 }, 978 }, 979 }) 980 return err 981 } 982 } 983 984 const testAccInstanceConfig_pre = ` 985 resource "aws_security_group" "tf_test_foo" { 986 name = "tf_test_foo" 987 description = "foo" 988 989 ingress { 990 protocol = "icmp" 991 from_port = -1 992 to_port = -1 993 cidr_blocks = ["0.0.0.0/0"] 994 } 995 } 996 ` 997 998 const testAccInstanceConfig = ` 999 resource "aws_security_group" "tf_test_foo" { 1000 name = "tf_test_foo" 1001 description = "foo" 1002 1003 ingress { 1004 protocol = "icmp" 1005 from_port = -1 1006 to_port = -1 1007 cidr_blocks = ["0.0.0.0/0"] 1008 } 1009 } 1010 1011 resource "aws_instance" "foo" { 1012 # us-west-2 1013 ami = "ami-4fccb37f" 1014 availability_zone = "us-west-2a" 1015 1016 instance_type = "m1.small" 1017 security_groups = ["${aws_security_group.tf_test_foo.name}"] 1018 user_data = "foo:-with-character's" 1019 } 1020 ` 1021 1022 const testAccInstanceConfigWithSmallInstanceType = ` 1023 resource "aws_instance" "foo" { 1024 # us-west-2 1025 ami = "ami-55a7ea65" 1026 availability_zone = "us-west-2a" 1027 1028 instance_type = "m3.medium" 1029 1030 tags { 1031 Name = "tf-acctest" 1032 } 1033 } 1034 ` 1035 1036 const testAccInstanceConfigUpdateInstanceType = ` 1037 resource "aws_instance" "foo" { 1038 # us-west-2 1039 ami = "ami-55a7ea65" 1040 availability_zone = "us-west-2a" 1041 1042 instance_type = "m3.large" 1043 1044 tags { 1045 Name = "tf-acctest" 1046 } 1047 } 1048 ` 1049 1050 const testAccInstanceGP2IopsDevice = ` 1051 resource "aws_instance" "foo" { 1052 # us-west-2 1053 ami = "ami-55a7ea65" 1054 1055 # In order to attach an encrypted volume to an instance you need to have an 1056 # m3.medium or larger. See "Supported Instance Types" in: 1057 # http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EBSEncryption.html 1058 instance_type = "m3.medium" 1059 1060 root_block_device { 1061 volume_type = "gp2" 1062 volume_size = 11 1063 } 1064 } 1065 ` 1066 1067 const testAccInstanceConfigBlockDevices = ` 1068 resource "aws_instance" "foo" { 1069 # us-west-2 1070 ami = "ami-55a7ea65" 1071 1072 # In order to attach an encrypted volume to an instance you need to have an 1073 # m3.medium or larger. See "Supported Instance Types" in: 1074 # http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EBSEncryption.html 1075 instance_type = "m3.medium" 1076 1077 root_block_device { 1078 volume_type = "gp2" 1079 volume_size = 11 1080 } 1081 ebs_block_device { 1082 device_name = "/dev/sdb" 1083 volume_size = 9 1084 } 1085 ebs_block_device { 1086 device_name = "/dev/sdc" 1087 volume_size = 10 1088 volume_type = "io1" 1089 iops = 100 1090 } 1091 1092 # Encrypted ebs block device 1093 ebs_block_device { 1094 device_name = "/dev/sdd" 1095 volume_size = 12 1096 encrypted = true 1097 } 1098 1099 ephemeral_block_device { 1100 device_name = "/dev/sde" 1101 virtual_name = "ephemeral0" 1102 } 1103 } 1104 ` 1105 1106 const testAccInstanceConfigSourceDestEnable = ` 1107 resource "aws_vpc" "foo" { 1108 cidr_block = "10.1.0.0/16" 1109 } 1110 1111 resource "aws_subnet" "foo" { 1112 cidr_block = "10.1.1.0/24" 1113 vpc_id = "${aws_vpc.foo.id}" 1114 } 1115 1116 resource "aws_instance" "foo" { 1117 # us-west-2 1118 ami = "ami-4fccb37f" 1119 instance_type = "m1.small" 1120 subnet_id = "${aws_subnet.foo.id}" 1121 } 1122 ` 1123 1124 const testAccInstanceConfigSourceDestDisable = ` 1125 resource "aws_vpc" "foo" { 1126 cidr_block = "10.1.0.0/16" 1127 } 1128 1129 resource "aws_subnet" "foo" { 1130 cidr_block = "10.1.1.0/24" 1131 vpc_id = "${aws_vpc.foo.id}" 1132 } 1133 1134 resource "aws_instance" "foo" { 1135 # us-west-2 1136 ami = "ami-4fccb37f" 1137 instance_type = "m1.small" 1138 subnet_id = "${aws_subnet.foo.id}" 1139 source_dest_check = false 1140 } 1141 ` 1142 1143 func testAccInstanceConfigDisableAPITermination(val bool) string { 1144 return fmt.Sprintf(` 1145 resource "aws_vpc" "foo" { 1146 cidr_block = "10.1.0.0/16" 1147 } 1148 1149 resource "aws_subnet" "foo" { 1150 cidr_block = "10.1.1.0/24" 1151 vpc_id = "${aws_vpc.foo.id}" 1152 } 1153 1154 resource "aws_instance" "foo" { 1155 # us-west-2 1156 ami = "ami-4fccb37f" 1157 instance_type = "m1.small" 1158 subnet_id = "${aws_subnet.foo.id}" 1159 disable_api_termination = %t 1160 } 1161 `, val) 1162 } 1163 1164 const testAccInstanceConfigVPC = ` 1165 resource "aws_vpc" "foo" { 1166 cidr_block = "10.1.0.0/16" 1167 } 1168 1169 resource "aws_subnet" "foo" { 1170 cidr_block = "10.1.1.0/24" 1171 vpc_id = "${aws_vpc.foo.id}" 1172 } 1173 1174 resource "aws_instance" "foo" { 1175 # us-west-2 1176 ami = "ami-4fccb37f" 1177 instance_type = "m1.small" 1178 subnet_id = "${aws_subnet.foo.id}" 1179 associate_public_ip_address = true 1180 tenancy = "dedicated" 1181 # pre-encoded base64 data 1182 user_data = "3dc39dda39be1205215e776bad998da361a5955d" 1183 } 1184 ` 1185 1186 const testAccInstanceConfigIpv6Support = ` 1187 resource "aws_vpc" "foo" { 1188 cidr_block = "10.1.0.0/16" 1189 assign_generated_ipv6_cidr_block = true 1190 tags { 1191 Name = "tf-ipv6-instance-acc-test" 1192 } 1193 } 1194 1195 resource "aws_subnet" "foo" { 1196 cidr_block = "10.1.1.0/24" 1197 vpc_id = "${aws_vpc.foo.id}" 1198 ipv6_cidr_block = "${cidrsubnet(aws_vpc.foo.ipv6_cidr_block, 8, 1)}" 1199 tags { 1200 Name = "tf-ipv6-instance-acc-test" 1201 } 1202 } 1203 1204 resource "aws_instance" "foo" { 1205 # us-west-2 1206 ami = "ami-c5eabbf5" 1207 instance_type = "t2.micro" 1208 subnet_id = "${aws_subnet.foo.id}" 1209 1210 ipv6_address_count = 1 1211 tags { 1212 Name = "tf-ipv6-instance-acc-test" 1213 } 1214 } 1215 ` 1216 1217 const testAccInstanceConfigMultipleRegions = ` 1218 provider "aws" { 1219 alias = "west" 1220 region = "us-west-2" 1221 } 1222 1223 provider "aws" { 1224 alias = "east" 1225 region = "us-east-1" 1226 } 1227 1228 resource "aws_instance" "foo" { 1229 # us-west-2 1230 provider = "aws.west" 1231 ami = "ami-4fccb37f" 1232 instance_type = "m1.small" 1233 } 1234 1235 resource "aws_instance" "bar" { 1236 # us-east-1 1237 provider = "aws.east" 1238 ami = "ami-8c6ea9e4" 1239 instance_type = "m1.small" 1240 } 1241 ` 1242 1243 const testAccCheckInstanceConfigTags = ` 1244 resource "aws_instance" "foo" { 1245 ami = "ami-4fccb37f" 1246 instance_type = "m1.small" 1247 tags { 1248 foo = "bar" 1249 } 1250 } 1251 ` 1252 1253 const testAccCheckInstanceConfigTagsUpdate = ` 1254 resource "aws_instance" "foo" { 1255 ami = "ami-4fccb37f" 1256 instance_type = "m1.small" 1257 tags { 1258 bar = "baz" 1259 } 1260 } 1261 ` 1262 1263 func testAccInstanceConfigWithoutInstanceProfile(rName string) string { 1264 return fmt.Sprintf(` 1265 resource "aws_iam_role" "test" { 1266 name = "test-%s" 1267 assume_role_policy = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"Service\":[\"ec2.amazonaws.com\"]},\"Action\":[\"sts:AssumeRole\"]}]}" 1268 } 1269 1270 resource "aws_iam_instance_profile" "test" { 1271 name = "test-%s" 1272 roles = ["${aws_iam_role.test.name}"] 1273 } 1274 1275 resource "aws_instance" "foo" { 1276 ami = "ami-4fccb37f" 1277 instance_type = "m1.small" 1278 tags { 1279 bar = "baz" 1280 } 1281 }`, rName, rName) 1282 } 1283 1284 func testAccInstanceConfigAttachInstanceProfile(rName string) string { 1285 return fmt.Sprintf(` 1286 resource "aws_iam_role" "test" { 1287 name = "test-%s" 1288 assume_role_policy = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"Service\":[\"ec2.amazonaws.com\"]},\"Action\":[\"sts:AssumeRole\"]}]}" 1289 } 1290 1291 resource "aws_iam_instance_profile" "test" { 1292 name = "test-%s" 1293 roles = ["${aws_iam_role.test.name}"] 1294 } 1295 1296 resource "aws_instance" "foo" { 1297 ami = "ami-4fccb37f" 1298 instance_type = "m1.small" 1299 iam_instance_profile = "${aws_iam_instance_profile.test.name}" 1300 tags { 1301 bar = "baz" 1302 } 1303 }`, rName, rName) 1304 } 1305 1306 const testAccInstanceConfigPrivateIP = ` 1307 resource "aws_vpc" "foo" { 1308 cidr_block = "10.1.0.0/16" 1309 } 1310 1311 resource "aws_subnet" "foo" { 1312 cidr_block = "10.1.1.0/24" 1313 vpc_id = "${aws_vpc.foo.id}" 1314 } 1315 1316 resource "aws_instance" "foo" { 1317 ami = "ami-c5eabbf5" 1318 instance_type = "t2.micro" 1319 subnet_id = "${aws_subnet.foo.id}" 1320 private_ip = "10.1.1.42" 1321 } 1322 ` 1323 1324 const testAccInstanceConfigAssociatePublicIPAndPrivateIP = ` 1325 resource "aws_vpc" "foo" { 1326 cidr_block = "10.1.0.0/16" 1327 } 1328 1329 resource "aws_subnet" "foo" { 1330 cidr_block = "10.1.1.0/24" 1331 vpc_id = "${aws_vpc.foo.id}" 1332 } 1333 1334 resource "aws_instance" "foo" { 1335 ami = "ami-c5eabbf5" 1336 instance_type = "t2.micro" 1337 subnet_id = "${aws_subnet.foo.id}" 1338 associate_public_ip_address = true 1339 private_ip = "10.1.1.42" 1340 } 1341 ` 1342 1343 const testAccInstanceNetworkInstanceSecurityGroups = ` 1344 resource "aws_internet_gateway" "gw" { 1345 vpc_id = "${aws_vpc.foo.id}" 1346 } 1347 1348 resource "aws_vpc" "foo" { 1349 cidr_block = "10.1.0.0/16" 1350 tags { 1351 Name = "tf-network-test" 1352 } 1353 } 1354 1355 resource "aws_security_group" "tf_test_foo" { 1356 name = "tf_test_foo" 1357 description = "foo" 1358 vpc_id="${aws_vpc.foo.id}" 1359 1360 ingress { 1361 protocol = "icmp" 1362 from_port = -1 1363 to_port = -1 1364 cidr_blocks = ["0.0.0.0/0"] 1365 } 1366 } 1367 1368 resource "aws_subnet" "foo" { 1369 cidr_block = "10.1.1.0/24" 1370 vpc_id = "${aws_vpc.foo.id}" 1371 } 1372 1373 resource "aws_instance" "foo_instance" { 1374 ami = "ami-21f78e11" 1375 instance_type = "t1.micro" 1376 vpc_security_group_ids = ["${aws_security_group.tf_test_foo.id}"] 1377 subnet_id = "${aws_subnet.foo.id}" 1378 associate_public_ip_address = true 1379 depends_on = ["aws_internet_gateway.gw"] 1380 } 1381 1382 resource "aws_eip" "foo_eip" { 1383 instance = "${aws_instance.foo_instance.id}" 1384 vpc = true 1385 depends_on = ["aws_internet_gateway.gw"] 1386 } 1387 ` 1388 1389 const testAccInstanceNetworkInstanceVPCSecurityGroupIDs = ` 1390 resource "aws_internet_gateway" "gw" { 1391 vpc_id = "${aws_vpc.foo.id}" 1392 } 1393 1394 resource "aws_vpc" "foo" { 1395 cidr_block = "10.1.0.0/16" 1396 tags { 1397 Name = "tf-network-test" 1398 } 1399 } 1400 1401 resource "aws_security_group" "tf_test_foo" { 1402 name = "tf_test_foo" 1403 description = "foo" 1404 vpc_id="${aws_vpc.foo.id}" 1405 1406 ingress { 1407 protocol = "icmp" 1408 from_port = -1 1409 to_port = -1 1410 cidr_blocks = ["0.0.0.0/0"] 1411 } 1412 } 1413 1414 resource "aws_subnet" "foo" { 1415 cidr_block = "10.1.1.0/24" 1416 vpc_id = "${aws_vpc.foo.id}" 1417 } 1418 1419 resource "aws_instance" "foo_instance" { 1420 ami = "ami-21f78e11" 1421 instance_type = "t1.micro" 1422 vpc_security_group_ids = ["${aws_security_group.tf_test_foo.id}"] 1423 subnet_id = "${aws_subnet.foo.id}" 1424 depends_on = ["aws_internet_gateway.gw"] 1425 } 1426 1427 resource "aws_eip" "foo_eip" { 1428 instance = "${aws_instance.foo_instance.id}" 1429 vpc = true 1430 depends_on = ["aws_internet_gateway.gw"] 1431 } 1432 ` 1433 1434 const testAccInstanceConfigKeyPair = ` 1435 provider "aws" { 1436 region = "us-east-1" 1437 } 1438 1439 resource "aws_key_pair" "debugging" { 1440 key_name = "tmp-key" 1441 public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD3F6tyPEFEzV0LX3X8BsXdMsQz1x2cEikKDEY0aIj41qgxMCP/iteneqXSIFZBp5vizPvaoIR3Um9xK7PGoW8giupGn+EPuxIA4cDM4vzOqOkiMPhz5XK0whEjkVzTo4+S0puvDZuwIsdiW9mxhJc7tgBNL0cYlWSYVkz4G/fslNfRPW5mYAM49f4fhtxPb5ok4Q2Lg9dPKVHO/Bgeu5woMc7RY0p1ej6D4CKFE6lymSDJpW0YHX/wqE9+cfEauh7xZcG0q9t2ta6F6fmX0agvpFyZo8aFbXeUBr7osSCJNgvavWbM/06niWrOvYX2xwWdhXmXSrbX8ZbabVohBK41 phodgson@thoughtworks.com" 1442 } 1443 1444 resource "aws_instance" "foo" { 1445 ami = "ami-408c7f28" 1446 instance_type = "t1.micro" 1447 key_name = "${aws_key_pair.debugging.key_name}" 1448 tags { 1449 Name = "testAccInstanceConfigKeyPair_TestAMI" 1450 } 1451 } 1452 ` 1453 1454 const testAccInstanceConfigRootBlockDeviceMismatch = ` 1455 resource "aws_vpc" "foo" { 1456 cidr_block = "10.1.0.0/16" 1457 } 1458 1459 resource "aws_subnet" "foo" { 1460 cidr_block = "10.1.1.0/24" 1461 vpc_id = "${aws_vpc.foo.id}" 1462 } 1463 1464 resource "aws_instance" "foo" { 1465 // This is an AMI with RootDeviceName: "/dev/sda1"; actual root: "/dev/sda" 1466 ami = "ami-ef5b69df" 1467 instance_type = "t1.micro" 1468 subnet_id = "${aws_subnet.foo.id}" 1469 root_block_device { 1470 volume_size = 13 1471 } 1472 } 1473 ` 1474 1475 const testAccInstanceConfigForceNewAndTagsDrift = ` 1476 resource "aws_vpc" "foo" { 1477 cidr_block = "10.1.0.0/16" 1478 } 1479 1480 resource "aws_subnet" "foo" { 1481 cidr_block = "10.1.1.0/24" 1482 vpc_id = "${aws_vpc.foo.id}" 1483 } 1484 1485 resource "aws_instance" "foo" { 1486 ami = "ami-22b9a343" 1487 instance_type = "t2.nano" 1488 subnet_id = "${aws_subnet.foo.id}" 1489 } 1490 ` 1491 1492 const testAccInstanceConfigForceNewAndTagsDrift_Update = ` 1493 resource "aws_vpc" "foo" { 1494 cidr_block = "10.1.0.0/16" 1495 } 1496 1497 resource "aws_subnet" "foo" { 1498 cidr_block = "10.1.1.0/24" 1499 vpc_id = "${aws_vpc.foo.id}" 1500 } 1501 1502 resource "aws_instance" "foo" { 1503 ami = "ami-22b9a343" 1504 instance_type = "t2.micro" 1505 subnet_id = "${aws_subnet.foo.id}" 1506 } 1507 `