github.com/simonswine/terraform@v0.9.0-beta2/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 iops = 330 1064 } 1065 } 1066 ` 1067 1068 const testAccInstanceConfigBlockDevices = ` 1069 resource "aws_instance" "foo" { 1070 # us-west-2 1071 ami = "ami-55a7ea65" 1072 1073 # In order to attach an encrypted volume to an instance you need to have an 1074 # m3.medium or larger. See "Supported Instance Types" in: 1075 # http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EBSEncryption.html 1076 instance_type = "m3.medium" 1077 1078 root_block_device { 1079 volume_type = "gp2" 1080 volume_size = 11 1081 } 1082 ebs_block_device { 1083 device_name = "/dev/sdb" 1084 volume_size = 9 1085 } 1086 ebs_block_device { 1087 device_name = "/dev/sdc" 1088 volume_size = 10 1089 volume_type = "io1" 1090 iops = 100 1091 } 1092 1093 # Encrypted ebs block device 1094 ebs_block_device { 1095 device_name = "/dev/sdd" 1096 volume_size = 12 1097 encrypted = true 1098 } 1099 1100 ephemeral_block_device { 1101 device_name = "/dev/sde" 1102 virtual_name = "ephemeral0" 1103 } 1104 } 1105 ` 1106 1107 const testAccInstanceConfigSourceDestEnable = ` 1108 resource "aws_vpc" "foo" { 1109 cidr_block = "10.1.0.0/16" 1110 } 1111 1112 resource "aws_subnet" "foo" { 1113 cidr_block = "10.1.1.0/24" 1114 vpc_id = "${aws_vpc.foo.id}" 1115 } 1116 1117 resource "aws_instance" "foo" { 1118 # us-west-2 1119 ami = "ami-4fccb37f" 1120 instance_type = "m1.small" 1121 subnet_id = "${aws_subnet.foo.id}" 1122 } 1123 ` 1124 1125 const testAccInstanceConfigSourceDestDisable = ` 1126 resource "aws_vpc" "foo" { 1127 cidr_block = "10.1.0.0/16" 1128 } 1129 1130 resource "aws_subnet" "foo" { 1131 cidr_block = "10.1.1.0/24" 1132 vpc_id = "${aws_vpc.foo.id}" 1133 } 1134 1135 resource "aws_instance" "foo" { 1136 # us-west-2 1137 ami = "ami-4fccb37f" 1138 instance_type = "m1.small" 1139 subnet_id = "${aws_subnet.foo.id}" 1140 source_dest_check = false 1141 } 1142 ` 1143 1144 func testAccInstanceConfigDisableAPITermination(val bool) string { 1145 return fmt.Sprintf(` 1146 resource "aws_vpc" "foo" { 1147 cidr_block = "10.1.0.0/16" 1148 } 1149 1150 resource "aws_subnet" "foo" { 1151 cidr_block = "10.1.1.0/24" 1152 vpc_id = "${aws_vpc.foo.id}" 1153 } 1154 1155 resource "aws_instance" "foo" { 1156 # us-west-2 1157 ami = "ami-4fccb37f" 1158 instance_type = "m1.small" 1159 subnet_id = "${aws_subnet.foo.id}" 1160 disable_api_termination = %t 1161 } 1162 `, val) 1163 } 1164 1165 const testAccInstanceConfigVPC = ` 1166 resource "aws_vpc" "foo" { 1167 cidr_block = "10.1.0.0/16" 1168 } 1169 1170 resource "aws_subnet" "foo" { 1171 cidr_block = "10.1.1.0/24" 1172 vpc_id = "${aws_vpc.foo.id}" 1173 } 1174 1175 resource "aws_instance" "foo" { 1176 # us-west-2 1177 ami = "ami-4fccb37f" 1178 instance_type = "m1.small" 1179 subnet_id = "${aws_subnet.foo.id}" 1180 associate_public_ip_address = true 1181 tenancy = "dedicated" 1182 # pre-encoded base64 data 1183 user_data = "3dc39dda39be1205215e776bad998da361a5955d" 1184 } 1185 ` 1186 1187 const testAccInstanceConfigIpv6Support = ` 1188 resource "aws_vpc" "foo" { 1189 cidr_block = "10.1.0.0/16" 1190 assign_generated_ipv6_cidr_block = true 1191 tags { 1192 Name = "tf-ipv6-instance-acc-test" 1193 } 1194 } 1195 1196 resource "aws_subnet" "foo" { 1197 cidr_block = "10.1.1.0/24" 1198 vpc_id = "${aws_vpc.foo.id}" 1199 ipv6_cidr_block = "${cidrsubnet(aws_vpc.foo.ipv6_cidr_block, 8, 1)}" 1200 tags { 1201 Name = "tf-ipv6-instance-acc-test" 1202 } 1203 } 1204 1205 resource "aws_instance" "foo" { 1206 # us-west-2 1207 ami = "ami-c5eabbf5" 1208 instance_type = "t2.micro" 1209 subnet_id = "${aws_subnet.foo.id}" 1210 1211 ipv6_address_count = 1 1212 tags { 1213 Name = "tf-ipv6-instance-acc-test" 1214 } 1215 } 1216 ` 1217 1218 const testAccInstanceConfigMultipleRegions = ` 1219 provider "aws" { 1220 alias = "west" 1221 region = "us-west-2" 1222 } 1223 1224 provider "aws" { 1225 alias = "east" 1226 region = "us-east-1" 1227 } 1228 1229 resource "aws_instance" "foo" { 1230 # us-west-2 1231 provider = "aws.west" 1232 ami = "ami-4fccb37f" 1233 instance_type = "m1.small" 1234 } 1235 1236 resource "aws_instance" "bar" { 1237 # us-east-1 1238 provider = "aws.east" 1239 ami = "ami-8c6ea9e4" 1240 instance_type = "m1.small" 1241 } 1242 ` 1243 1244 const testAccCheckInstanceConfigTags = ` 1245 resource "aws_instance" "foo" { 1246 ami = "ami-4fccb37f" 1247 instance_type = "m1.small" 1248 tags { 1249 foo = "bar" 1250 } 1251 } 1252 ` 1253 1254 const testAccCheckInstanceConfigTagsUpdate = ` 1255 resource "aws_instance" "foo" { 1256 ami = "ami-4fccb37f" 1257 instance_type = "m1.small" 1258 tags { 1259 bar = "baz" 1260 } 1261 } 1262 ` 1263 1264 func testAccInstanceConfigWithoutInstanceProfile(rName string) string { 1265 return fmt.Sprintf(` 1266 resource "aws_iam_role" "test" { 1267 name = "test-%s" 1268 assume_role_policy = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"Service\":[\"ec2.amazonaws.com\"]},\"Action\":[\"sts:AssumeRole\"]}]}" 1269 } 1270 1271 resource "aws_iam_instance_profile" "test" { 1272 name = "test-%s" 1273 roles = ["${aws_iam_role.test.name}"] 1274 } 1275 1276 resource "aws_instance" "foo" { 1277 ami = "ami-4fccb37f" 1278 instance_type = "m1.small" 1279 tags { 1280 bar = "baz" 1281 } 1282 }`, rName, rName) 1283 } 1284 1285 func testAccInstanceConfigAttachInstanceProfile(rName string) string { 1286 return fmt.Sprintf(` 1287 resource "aws_iam_role" "test" { 1288 name = "test-%s" 1289 assume_role_policy = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"Service\":[\"ec2.amazonaws.com\"]},\"Action\":[\"sts:AssumeRole\"]}]}" 1290 } 1291 1292 resource "aws_iam_instance_profile" "test" { 1293 name = "test-%s" 1294 roles = ["${aws_iam_role.test.name}"] 1295 } 1296 1297 resource "aws_instance" "foo" { 1298 ami = "ami-4fccb37f" 1299 instance_type = "m1.small" 1300 iam_instance_profile = "${aws_iam_instance_profile.test.name}" 1301 tags { 1302 bar = "baz" 1303 } 1304 }`, rName, rName) 1305 } 1306 1307 const testAccInstanceConfigPrivateIP = ` 1308 resource "aws_vpc" "foo" { 1309 cidr_block = "10.1.0.0/16" 1310 } 1311 1312 resource "aws_subnet" "foo" { 1313 cidr_block = "10.1.1.0/24" 1314 vpc_id = "${aws_vpc.foo.id}" 1315 } 1316 1317 resource "aws_instance" "foo" { 1318 ami = "ami-c5eabbf5" 1319 instance_type = "t2.micro" 1320 subnet_id = "${aws_subnet.foo.id}" 1321 private_ip = "10.1.1.42" 1322 } 1323 ` 1324 1325 const testAccInstanceConfigAssociatePublicIPAndPrivateIP = ` 1326 resource "aws_vpc" "foo" { 1327 cidr_block = "10.1.0.0/16" 1328 } 1329 1330 resource "aws_subnet" "foo" { 1331 cidr_block = "10.1.1.0/24" 1332 vpc_id = "${aws_vpc.foo.id}" 1333 } 1334 1335 resource "aws_instance" "foo" { 1336 ami = "ami-c5eabbf5" 1337 instance_type = "t2.micro" 1338 subnet_id = "${aws_subnet.foo.id}" 1339 associate_public_ip_address = true 1340 private_ip = "10.1.1.42" 1341 } 1342 ` 1343 1344 const testAccInstanceNetworkInstanceSecurityGroups = ` 1345 resource "aws_internet_gateway" "gw" { 1346 vpc_id = "${aws_vpc.foo.id}" 1347 } 1348 1349 resource "aws_vpc" "foo" { 1350 cidr_block = "10.1.0.0/16" 1351 tags { 1352 Name = "tf-network-test" 1353 } 1354 } 1355 1356 resource "aws_security_group" "tf_test_foo" { 1357 name = "tf_test_foo" 1358 description = "foo" 1359 vpc_id="${aws_vpc.foo.id}" 1360 1361 ingress { 1362 protocol = "icmp" 1363 from_port = -1 1364 to_port = -1 1365 cidr_blocks = ["0.0.0.0/0"] 1366 } 1367 } 1368 1369 resource "aws_subnet" "foo" { 1370 cidr_block = "10.1.1.0/24" 1371 vpc_id = "${aws_vpc.foo.id}" 1372 } 1373 1374 resource "aws_instance" "foo_instance" { 1375 ami = "ami-21f78e11" 1376 instance_type = "t1.micro" 1377 vpc_security_group_ids = ["${aws_security_group.tf_test_foo.id}"] 1378 subnet_id = "${aws_subnet.foo.id}" 1379 associate_public_ip_address = true 1380 depends_on = ["aws_internet_gateway.gw"] 1381 } 1382 1383 resource "aws_eip" "foo_eip" { 1384 instance = "${aws_instance.foo_instance.id}" 1385 vpc = true 1386 depends_on = ["aws_internet_gateway.gw"] 1387 } 1388 ` 1389 1390 const testAccInstanceNetworkInstanceVPCSecurityGroupIDs = ` 1391 resource "aws_internet_gateway" "gw" { 1392 vpc_id = "${aws_vpc.foo.id}" 1393 } 1394 1395 resource "aws_vpc" "foo" { 1396 cidr_block = "10.1.0.0/16" 1397 tags { 1398 Name = "tf-network-test" 1399 } 1400 } 1401 1402 resource "aws_security_group" "tf_test_foo" { 1403 name = "tf_test_foo" 1404 description = "foo" 1405 vpc_id="${aws_vpc.foo.id}" 1406 1407 ingress { 1408 protocol = "icmp" 1409 from_port = -1 1410 to_port = -1 1411 cidr_blocks = ["0.0.0.0/0"] 1412 } 1413 } 1414 1415 resource "aws_subnet" "foo" { 1416 cidr_block = "10.1.1.0/24" 1417 vpc_id = "${aws_vpc.foo.id}" 1418 } 1419 1420 resource "aws_instance" "foo_instance" { 1421 ami = "ami-21f78e11" 1422 instance_type = "t1.micro" 1423 vpc_security_group_ids = ["${aws_security_group.tf_test_foo.id}"] 1424 subnet_id = "${aws_subnet.foo.id}" 1425 depends_on = ["aws_internet_gateway.gw"] 1426 } 1427 1428 resource "aws_eip" "foo_eip" { 1429 instance = "${aws_instance.foo_instance.id}" 1430 vpc = true 1431 depends_on = ["aws_internet_gateway.gw"] 1432 } 1433 ` 1434 1435 const testAccInstanceConfigKeyPair = ` 1436 provider "aws" { 1437 region = "us-east-1" 1438 } 1439 1440 resource "aws_key_pair" "debugging" { 1441 key_name = "tmp-key" 1442 public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD3F6tyPEFEzV0LX3X8BsXdMsQz1x2cEikKDEY0aIj41qgxMCP/iteneqXSIFZBp5vizPvaoIR3Um9xK7PGoW8giupGn+EPuxIA4cDM4vzOqOkiMPhz5XK0whEjkVzTo4+S0puvDZuwIsdiW9mxhJc7tgBNL0cYlWSYVkz4G/fslNfRPW5mYAM49f4fhtxPb5ok4Q2Lg9dPKVHO/Bgeu5woMc7RY0p1ej6D4CKFE6lymSDJpW0YHX/wqE9+cfEauh7xZcG0q9t2ta6F6fmX0agvpFyZo8aFbXeUBr7osSCJNgvavWbM/06niWrOvYX2xwWdhXmXSrbX8ZbabVohBK41 phodgson@thoughtworks.com" 1443 } 1444 1445 resource "aws_instance" "foo" { 1446 ami = "ami-408c7f28" 1447 instance_type = "t1.micro" 1448 key_name = "${aws_key_pair.debugging.key_name}" 1449 tags { 1450 Name = "testAccInstanceConfigKeyPair_TestAMI" 1451 } 1452 } 1453 ` 1454 1455 const testAccInstanceConfigRootBlockDeviceMismatch = ` 1456 resource "aws_vpc" "foo" { 1457 cidr_block = "10.1.0.0/16" 1458 } 1459 1460 resource "aws_subnet" "foo" { 1461 cidr_block = "10.1.1.0/24" 1462 vpc_id = "${aws_vpc.foo.id}" 1463 } 1464 1465 resource "aws_instance" "foo" { 1466 // This is an AMI with RootDeviceName: "/dev/sda1"; actual root: "/dev/sda" 1467 ami = "ami-ef5b69df" 1468 instance_type = "t1.micro" 1469 subnet_id = "${aws_subnet.foo.id}" 1470 root_block_device { 1471 volume_size = 13 1472 } 1473 } 1474 ` 1475 1476 const testAccInstanceConfigForceNewAndTagsDrift = ` 1477 resource "aws_vpc" "foo" { 1478 cidr_block = "10.1.0.0/16" 1479 } 1480 1481 resource "aws_subnet" "foo" { 1482 cidr_block = "10.1.1.0/24" 1483 vpc_id = "${aws_vpc.foo.id}" 1484 } 1485 1486 resource "aws_instance" "foo" { 1487 ami = "ami-22b9a343" 1488 instance_type = "t2.nano" 1489 subnet_id = "${aws_subnet.foo.id}" 1490 } 1491 ` 1492 1493 const testAccInstanceConfigForceNewAndTagsDrift_Update = ` 1494 resource "aws_vpc" "foo" { 1495 cidr_block = "10.1.0.0/16" 1496 } 1497 1498 resource "aws_subnet" "foo" { 1499 cidr_block = "10.1.1.0/24" 1500 vpc_id = "${aws_vpc.foo.id}" 1501 } 1502 1503 resource "aws_instance" "foo" { 1504 ami = "ami-22b9a343" 1505 instance_type = "t2.micro" 1506 subnet_id = "${aws_subnet.foo.id}" 1507 } 1508 `