github.com/cbroglie/terraform@v0.7.0-rc3.0.20170410193827-735dfc416d46/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: testAccInstanceConfigWithInstanceProfile(rName), 660 Check: resource.ComposeTestCheckFunc( 661 testAccCheckInstanceExists("aws_instance.foo", &v), 662 testCheckInstanceProfile(), 663 ), 664 }, 665 }, 666 }) 667 } 668 669 func TestAccAWSInstance_withIamInstanceProfile(t *testing.T) { 670 var v ec2.Instance 671 rName := acctest.RandString(5) 672 673 testCheckInstanceProfile := func() resource.TestCheckFunc { 674 return func(*terraform.State) error { 675 if v.IamInstanceProfile == nil { 676 return fmt.Errorf("Instance Profile is nil - we expected an InstanceProfile associated with the Instance") 677 } 678 679 return nil 680 } 681 } 682 683 resource.Test(t, resource.TestCase{ 684 PreCheck: func() { testAccPreCheck(t) }, 685 IDRefreshName: "aws_instance.foo", 686 Providers: testAccProviders, 687 CheckDestroy: testAccCheckInstanceDestroy, 688 Steps: []resource.TestStep{ 689 { 690 Config: testAccInstanceConfigWithInstanceProfile(rName), 691 Check: resource.ComposeTestCheckFunc( 692 testAccCheckInstanceExists("aws_instance.foo", &v), 693 testCheckInstanceProfile(), 694 ), 695 }, 696 }, 697 }) 698 } 699 700 func TestAccAWSInstance_privateIP(t *testing.T) { 701 var v ec2.Instance 702 703 testCheckPrivateIP := func() resource.TestCheckFunc { 704 return func(*terraform.State) error { 705 if *v.PrivateIpAddress != "10.1.1.42" { 706 return fmt.Errorf("bad private IP: %s", *v.PrivateIpAddress) 707 } 708 709 return nil 710 } 711 } 712 713 resource.Test(t, resource.TestCase{ 714 PreCheck: func() { testAccPreCheck(t) }, 715 IDRefreshName: "aws_instance.foo", 716 Providers: testAccProviders, 717 CheckDestroy: testAccCheckInstanceDestroy, 718 Steps: []resource.TestStep{ 719 { 720 Config: testAccInstanceConfigPrivateIP, 721 Check: resource.ComposeTestCheckFunc( 722 testAccCheckInstanceExists("aws_instance.foo", &v), 723 testCheckPrivateIP(), 724 ), 725 }, 726 }, 727 }) 728 } 729 730 func TestAccAWSInstance_associatePublicIPAndPrivateIP(t *testing.T) { 731 var v ec2.Instance 732 733 testCheckPrivateIP := func() resource.TestCheckFunc { 734 return func(*terraform.State) error { 735 if *v.PrivateIpAddress != "10.1.1.42" { 736 return fmt.Errorf("bad private IP: %s", *v.PrivateIpAddress) 737 } 738 739 return nil 740 } 741 } 742 743 resource.Test(t, resource.TestCase{ 744 PreCheck: func() { testAccPreCheck(t) }, 745 IDRefreshName: "aws_instance.foo", 746 IDRefreshIgnore: []string{"associate_public_ip_address"}, 747 Providers: testAccProviders, 748 CheckDestroy: testAccCheckInstanceDestroy, 749 Steps: []resource.TestStep{ 750 { 751 Config: testAccInstanceConfigAssociatePublicIPAndPrivateIP, 752 Check: resource.ComposeTestCheckFunc( 753 testAccCheckInstanceExists("aws_instance.foo", &v), 754 testCheckPrivateIP(), 755 ), 756 }, 757 }, 758 }) 759 } 760 761 // Guard against regression with KeyPairs 762 // https://github.com/hashicorp/terraform/issues/2302 763 func TestAccAWSInstance_keyPairCheck(t *testing.T) { 764 var v ec2.Instance 765 766 testCheckKeyPair := func(keyName string) resource.TestCheckFunc { 767 return func(*terraform.State) error { 768 if v.KeyName == nil { 769 return fmt.Errorf("No Key Pair found, expected(%s)", keyName) 770 } 771 if v.KeyName != nil && *v.KeyName != keyName { 772 return fmt.Errorf("Bad key name, expected (%s), got (%s)", keyName, *v.KeyName) 773 } 774 775 return nil 776 } 777 } 778 779 resource.Test(t, resource.TestCase{ 780 PreCheck: func() { testAccPreCheck(t) }, 781 IDRefreshName: "aws_instance.foo", 782 IDRefreshIgnore: []string{"source_dest_check"}, 783 Providers: testAccProviders, 784 CheckDestroy: testAccCheckInstanceDestroy, 785 Steps: []resource.TestStep{ 786 { 787 Config: testAccInstanceConfigKeyPair, 788 Check: resource.ComposeTestCheckFunc( 789 testAccCheckInstanceExists("aws_instance.foo", &v), 790 testCheckKeyPair("tmp-key"), 791 ), 792 }, 793 }, 794 }) 795 } 796 797 func TestAccAWSInstance_rootBlockDeviceMismatch(t *testing.T) { 798 var v ec2.Instance 799 800 resource.Test(t, resource.TestCase{ 801 PreCheck: func() { testAccPreCheck(t) }, 802 Providers: testAccProviders, 803 CheckDestroy: testAccCheckInstanceDestroy, 804 Steps: []resource.TestStep{ 805 { 806 Config: testAccInstanceConfigRootBlockDeviceMismatch, 807 Check: resource.ComposeTestCheckFunc( 808 testAccCheckInstanceExists("aws_instance.foo", &v), 809 resource.TestCheckResourceAttr( 810 "aws_instance.foo", "root_block_device.0.volume_size", "13"), 811 ), 812 }, 813 }, 814 }) 815 } 816 817 // This test reproduces the bug here: 818 // https://github.com/hashicorp/terraform/issues/1752 819 // 820 // I wish there were a way to exercise resources built with helper.Schema in a 821 // unit context, in which case this test could be moved there, but for now this 822 // will cover the bugfix. 823 // 824 // The following triggers "diffs didn't match during apply" without the fix in to 825 // set NewRemoved on the .# field when it changes to 0. 826 func TestAccAWSInstance_forceNewAndTagsDrift(t *testing.T) { 827 var v ec2.Instance 828 829 resource.Test(t, resource.TestCase{ 830 PreCheck: func() { testAccPreCheck(t) }, 831 IDRefreshName: "aws_instance.foo", 832 Providers: testAccProviders, 833 CheckDestroy: testAccCheckInstanceDestroy, 834 Steps: []resource.TestStep{ 835 { 836 Config: testAccInstanceConfigForceNewAndTagsDrift, 837 Check: resource.ComposeTestCheckFunc( 838 testAccCheckInstanceExists("aws_instance.foo", &v), 839 driftTags(&v), 840 ), 841 ExpectNonEmptyPlan: true, 842 }, 843 { 844 Config: testAccInstanceConfigForceNewAndTagsDrift_Update, 845 Check: resource.ComposeTestCheckFunc( 846 testAccCheckInstanceExists("aws_instance.foo", &v), 847 ), 848 }, 849 }, 850 }) 851 } 852 853 func TestAccAWSInstance_changeInstanceType(t *testing.T) { 854 var before ec2.Instance 855 var after ec2.Instance 856 857 resource.Test(t, resource.TestCase{ 858 PreCheck: func() { testAccPreCheck(t) }, 859 Providers: testAccProviders, 860 CheckDestroy: testAccCheckInstanceDestroy, 861 Steps: []resource.TestStep{ 862 { 863 Config: testAccInstanceConfigWithSmallInstanceType, 864 Check: resource.ComposeTestCheckFunc( 865 testAccCheckInstanceExists("aws_instance.foo", &before), 866 ), 867 }, 868 { 869 Config: testAccInstanceConfigUpdateInstanceType, 870 Check: resource.ComposeTestCheckFunc( 871 testAccCheckInstanceExists("aws_instance.foo", &after), 872 testAccCheckInstanceNotRecreated( 873 t, &before, &after), 874 ), 875 }, 876 }, 877 }) 878 } 879 880 func testAccCheckInstanceNotRecreated(t *testing.T, 881 before, after *ec2.Instance) resource.TestCheckFunc { 882 return func(s *terraform.State) error { 883 if *before.InstanceId != *after.InstanceId { 884 t.Fatalf("AWS Instance IDs have changed. Before %s. After %s", *before.InstanceId, *after.InstanceId) 885 } 886 return nil 887 } 888 } 889 890 func testAccCheckInstanceDestroy(s *terraform.State) error { 891 return testAccCheckInstanceDestroyWithProvider(s, testAccProvider) 892 } 893 894 func testAccCheckInstanceDestroyWithProviders(providers *[]*schema.Provider) resource.TestCheckFunc { 895 return func(s *terraform.State) error { 896 for _, provider := range *providers { 897 if provider.Meta() == nil { 898 continue 899 } 900 if err := testAccCheckInstanceDestroyWithProvider(s, provider); err != nil { 901 return err 902 } 903 } 904 return nil 905 } 906 } 907 908 func testAccCheckInstanceDestroyWithProvider(s *terraform.State, provider *schema.Provider) error { 909 conn := provider.Meta().(*AWSClient).ec2conn 910 911 for _, rs := range s.RootModule().Resources { 912 if rs.Type != "aws_instance" { 913 continue 914 } 915 916 // Try to find the resource 917 resp, err := conn.DescribeInstances(&ec2.DescribeInstancesInput{ 918 InstanceIds: []*string{aws.String(rs.Primary.ID)}, 919 }) 920 if err == nil { 921 for _, r := range resp.Reservations { 922 for _, i := range r.Instances { 923 if i.State != nil && *i.State.Name != "terminated" { 924 return fmt.Errorf("Found unterminated instance: %s", i) 925 } 926 } 927 } 928 } 929 930 // Verify the error is what we want 931 if ae, ok := err.(awserr.Error); ok && ae.Code() == "InvalidInstanceID.NotFound" { 932 continue 933 } 934 935 return err 936 } 937 938 return nil 939 } 940 941 func testAccCheckInstanceExists(n string, i *ec2.Instance) resource.TestCheckFunc { 942 providers := []*schema.Provider{testAccProvider} 943 return testAccCheckInstanceExistsWithProviders(n, i, &providers) 944 } 945 946 func testAccCheckInstanceExistsWithProviders(n string, i *ec2.Instance, providers *[]*schema.Provider) resource.TestCheckFunc { 947 return func(s *terraform.State) error { 948 rs, ok := s.RootModule().Resources[n] 949 if !ok { 950 return fmt.Errorf("Not found: %s", n) 951 } 952 953 if rs.Primary.ID == "" { 954 return fmt.Errorf("No ID is set") 955 } 956 for _, provider := range *providers { 957 // Ignore if Meta is empty, this can happen for validation providers 958 if provider.Meta() == nil { 959 continue 960 } 961 962 conn := provider.Meta().(*AWSClient).ec2conn 963 resp, err := conn.DescribeInstances(&ec2.DescribeInstancesInput{ 964 InstanceIds: []*string{aws.String(rs.Primary.ID)}, 965 }) 966 if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "InvalidInstanceID.NotFound" { 967 continue 968 } 969 if err != nil { 970 return err 971 } 972 973 if len(resp.Reservations) > 0 { 974 *i = *resp.Reservations[0].Instances[0] 975 return nil 976 } 977 } 978 979 return fmt.Errorf("Instance not found") 980 } 981 } 982 983 func TestInstanceTenancySchema(t *testing.T) { 984 actualSchema := resourceAwsInstance().Schema["tenancy"] 985 expectedSchema := &schema.Schema{ 986 Type: schema.TypeString, 987 Optional: true, 988 Computed: true, 989 ForceNew: true, 990 } 991 if !reflect.DeepEqual(actualSchema, expectedSchema) { 992 t.Fatalf( 993 "Got:\n\n%#v\n\nExpected:\n\n%#v\n", 994 actualSchema, 995 expectedSchema) 996 } 997 } 998 999 func driftTags(instance *ec2.Instance) resource.TestCheckFunc { 1000 return func(s *terraform.State) error { 1001 conn := testAccProvider.Meta().(*AWSClient).ec2conn 1002 _, err := conn.CreateTags(&ec2.CreateTagsInput{ 1003 Resources: []*string{instance.InstanceId}, 1004 Tags: []*ec2.Tag{ 1005 { 1006 Key: aws.String("Drift"), 1007 Value: aws.String("Happens"), 1008 }, 1009 }, 1010 }) 1011 return err 1012 } 1013 } 1014 1015 const testAccInstanceConfig_pre = ` 1016 resource "aws_security_group" "tf_test_foo" { 1017 name = "tf_test_foo" 1018 description = "foo" 1019 1020 ingress { 1021 protocol = "icmp" 1022 from_port = -1 1023 to_port = -1 1024 cidr_blocks = ["0.0.0.0/0"] 1025 } 1026 } 1027 ` 1028 1029 const testAccInstanceConfig = ` 1030 resource "aws_security_group" "tf_test_foo" { 1031 name = "tf_test_foo" 1032 description = "foo" 1033 1034 ingress { 1035 protocol = "icmp" 1036 from_port = -1 1037 to_port = -1 1038 cidr_blocks = ["0.0.0.0/0"] 1039 } 1040 } 1041 1042 resource "aws_instance" "foo" { 1043 # us-west-2 1044 ami = "ami-4fccb37f" 1045 availability_zone = "us-west-2a" 1046 1047 instance_type = "m1.small" 1048 security_groups = ["${aws_security_group.tf_test_foo.name}"] 1049 user_data = "foo:-with-character's" 1050 } 1051 ` 1052 1053 const testAccInstanceConfigWithSmallInstanceType = ` 1054 resource "aws_instance" "foo" { 1055 # us-west-2 1056 ami = "ami-55a7ea65" 1057 availability_zone = "us-west-2a" 1058 1059 instance_type = "m3.medium" 1060 1061 tags { 1062 Name = "tf-acctest" 1063 } 1064 } 1065 ` 1066 1067 const testAccInstanceConfigUpdateInstanceType = ` 1068 resource "aws_instance" "foo" { 1069 # us-west-2 1070 ami = "ami-55a7ea65" 1071 availability_zone = "us-west-2a" 1072 1073 instance_type = "m3.large" 1074 1075 tags { 1076 Name = "tf-acctest" 1077 } 1078 } 1079 ` 1080 1081 const testAccInstanceGP2IopsDevice = ` 1082 resource "aws_instance" "foo" { 1083 # us-west-2 1084 ami = "ami-55a7ea65" 1085 1086 # In order to attach an encrypted volume to an instance you need to have an 1087 # m3.medium or larger. See "Supported Instance Types" in: 1088 # http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EBSEncryption.html 1089 instance_type = "m3.medium" 1090 1091 root_block_device { 1092 volume_type = "gp2" 1093 volume_size = 11 1094 } 1095 } 1096 ` 1097 1098 const testAccInstanceConfigBlockDevices = ` 1099 resource "aws_instance" "foo" { 1100 # us-west-2 1101 ami = "ami-55a7ea65" 1102 1103 # In order to attach an encrypted volume to an instance you need to have an 1104 # m3.medium or larger. See "Supported Instance Types" in: 1105 # http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EBSEncryption.html 1106 instance_type = "m3.medium" 1107 1108 root_block_device { 1109 volume_type = "gp2" 1110 volume_size = 11 1111 } 1112 ebs_block_device { 1113 device_name = "/dev/sdb" 1114 volume_size = 9 1115 } 1116 ebs_block_device { 1117 device_name = "/dev/sdc" 1118 volume_size = 10 1119 volume_type = "io1" 1120 iops = 100 1121 } 1122 1123 # Encrypted ebs block device 1124 ebs_block_device { 1125 device_name = "/dev/sdd" 1126 volume_size = 12 1127 encrypted = true 1128 } 1129 1130 ephemeral_block_device { 1131 device_name = "/dev/sde" 1132 virtual_name = "ephemeral0" 1133 } 1134 } 1135 ` 1136 1137 const testAccInstanceConfigSourceDestEnable = ` 1138 resource "aws_vpc" "foo" { 1139 cidr_block = "10.1.0.0/16" 1140 } 1141 1142 resource "aws_subnet" "foo" { 1143 cidr_block = "10.1.1.0/24" 1144 vpc_id = "${aws_vpc.foo.id}" 1145 } 1146 1147 resource "aws_instance" "foo" { 1148 # us-west-2 1149 ami = "ami-4fccb37f" 1150 instance_type = "m1.small" 1151 subnet_id = "${aws_subnet.foo.id}" 1152 } 1153 ` 1154 1155 const testAccInstanceConfigSourceDestDisable = ` 1156 resource "aws_vpc" "foo" { 1157 cidr_block = "10.1.0.0/16" 1158 } 1159 1160 resource "aws_subnet" "foo" { 1161 cidr_block = "10.1.1.0/24" 1162 vpc_id = "${aws_vpc.foo.id}" 1163 } 1164 1165 resource "aws_instance" "foo" { 1166 # us-west-2 1167 ami = "ami-4fccb37f" 1168 instance_type = "m1.small" 1169 subnet_id = "${aws_subnet.foo.id}" 1170 source_dest_check = false 1171 } 1172 ` 1173 1174 func testAccInstanceConfigDisableAPITermination(val bool) string { 1175 return fmt.Sprintf(` 1176 resource "aws_vpc" "foo" { 1177 cidr_block = "10.1.0.0/16" 1178 } 1179 1180 resource "aws_subnet" "foo" { 1181 cidr_block = "10.1.1.0/24" 1182 vpc_id = "${aws_vpc.foo.id}" 1183 } 1184 1185 resource "aws_instance" "foo" { 1186 # us-west-2 1187 ami = "ami-4fccb37f" 1188 instance_type = "m1.small" 1189 subnet_id = "${aws_subnet.foo.id}" 1190 disable_api_termination = %t 1191 } 1192 `, val) 1193 } 1194 1195 const testAccInstanceConfigVPC = ` 1196 resource "aws_vpc" "foo" { 1197 cidr_block = "10.1.0.0/16" 1198 } 1199 1200 resource "aws_subnet" "foo" { 1201 cidr_block = "10.1.1.0/24" 1202 vpc_id = "${aws_vpc.foo.id}" 1203 } 1204 1205 resource "aws_instance" "foo" { 1206 # us-west-2 1207 ami = "ami-4fccb37f" 1208 instance_type = "m1.small" 1209 subnet_id = "${aws_subnet.foo.id}" 1210 associate_public_ip_address = true 1211 tenancy = "dedicated" 1212 # pre-encoded base64 data 1213 user_data = "3dc39dda39be1205215e776bad998da361a5955d" 1214 } 1215 ` 1216 1217 const testAccInstanceConfigIpv6Support = ` 1218 resource "aws_vpc" "foo" { 1219 cidr_block = "10.1.0.0/16" 1220 assign_generated_ipv6_cidr_block = true 1221 tags { 1222 Name = "tf-ipv6-instance-acc-test" 1223 } 1224 } 1225 1226 resource "aws_subnet" "foo" { 1227 cidr_block = "10.1.1.0/24" 1228 vpc_id = "${aws_vpc.foo.id}" 1229 ipv6_cidr_block = "${cidrsubnet(aws_vpc.foo.ipv6_cidr_block, 8, 1)}" 1230 tags { 1231 Name = "tf-ipv6-instance-acc-test" 1232 } 1233 } 1234 1235 resource "aws_instance" "foo" { 1236 # us-west-2 1237 ami = "ami-c5eabbf5" 1238 instance_type = "t2.micro" 1239 subnet_id = "${aws_subnet.foo.id}" 1240 1241 ipv6_address_count = 1 1242 tags { 1243 Name = "tf-ipv6-instance-acc-test" 1244 } 1245 } 1246 ` 1247 1248 const testAccInstanceConfigMultipleRegions = ` 1249 provider "aws" { 1250 alias = "west" 1251 region = "us-west-2" 1252 } 1253 1254 provider "aws" { 1255 alias = "east" 1256 region = "us-east-1" 1257 } 1258 1259 resource "aws_instance" "foo" { 1260 # us-west-2 1261 provider = "aws.west" 1262 ami = "ami-4fccb37f" 1263 instance_type = "m1.small" 1264 } 1265 1266 resource "aws_instance" "bar" { 1267 # us-east-1 1268 provider = "aws.east" 1269 ami = "ami-8c6ea9e4" 1270 instance_type = "m1.small" 1271 } 1272 ` 1273 1274 const testAccCheckInstanceConfigTags = ` 1275 resource "aws_instance" "foo" { 1276 ami = "ami-4fccb37f" 1277 instance_type = "m1.small" 1278 tags { 1279 foo = "bar" 1280 } 1281 } 1282 ` 1283 1284 const testAccCheckInstanceConfigTagsUpdate = ` 1285 resource "aws_instance" "foo" { 1286 ami = "ami-4fccb37f" 1287 instance_type = "m1.small" 1288 tags { 1289 bar = "baz" 1290 } 1291 } 1292 ` 1293 1294 func testAccInstanceConfigWithoutInstanceProfile(rName string) string { 1295 return fmt.Sprintf(` 1296 resource "aws_iam_role" "test" { 1297 name = "test-%s" 1298 assume_role_policy = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"Service\":[\"ec2.amazonaws.com\"]},\"Action\":[\"sts:AssumeRole\"]}]}" 1299 } 1300 1301 resource "aws_iam_instance_profile" "test" { 1302 name = "test-%s" 1303 roles = ["${aws_iam_role.test.name}"] 1304 } 1305 1306 resource "aws_instance" "foo" { 1307 ami = "ami-4fccb37f" 1308 instance_type = "m1.small" 1309 tags { 1310 bar = "baz" 1311 } 1312 }`, rName, rName) 1313 } 1314 1315 func testAccInstanceConfigWithInstanceProfile(rName string) string { 1316 return fmt.Sprintf(` 1317 resource "aws_iam_role" "test" { 1318 name = "test-%s" 1319 assume_role_policy = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"Service\":[\"ec2.amazonaws.com\"]},\"Action\":[\"sts:AssumeRole\"]}]}" 1320 } 1321 1322 resource "aws_iam_instance_profile" "test" { 1323 name = "test-%s" 1324 roles = ["${aws_iam_role.test.name}"] 1325 } 1326 1327 resource "aws_instance" "foo" { 1328 ami = "ami-4fccb37f" 1329 instance_type = "m1.small" 1330 iam_instance_profile = "${aws_iam_instance_profile.test.name}" 1331 tags { 1332 bar = "baz" 1333 } 1334 }`, rName, rName) 1335 } 1336 1337 const testAccInstanceConfigPrivateIP = ` 1338 resource "aws_vpc" "foo" { 1339 cidr_block = "10.1.0.0/16" 1340 } 1341 1342 resource "aws_subnet" "foo" { 1343 cidr_block = "10.1.1.0/24" 1344 vpc_id = "${aws_vpc.foo.id}" 1345 } 1346 1347 resource "aws_instance" "foo" { 1348 ami = "ami-c5eabbf5" 1349 instance_type = "t2.micro" 1350 subnet_id = "${aws_subnet.foo.id}" 1351 private_ip = "10.1.1.42" 1352 } 1353 ` 1354 1355 const testAccInstanceConfigAssociatePublicIPAndPrivateIP = ` 1356 resource "aws_vpc" "foo" { 1357 cidr_block = "10.1.0.0/16" 1358 } 1359 1360 resource "aws_subnet" "foo" { 1361 cidr_block = "10.1.1.0/24" 1362 vpc_id = "${aws_vpc.foo.id}" 1363 } 1364 1365 resource "aws_instance" "foo" { 1366 ami = "ami-c5eabbf5" 1367 instance_type = "t2.micro" 1368 subnet_id = "${aws_subnet.foo.id}" 1369 associate_public_ip_address = true 1370 private_ip = "10.1.1.42" 1371 } 1372 ` 1373 1374 const testAccInstanceNetworkInstanceSecurityGroups = ` 1375 resource "aws_internet_gateway" "gw" { 1376 vpc_id = "${aws_vpc.foo.id}" 1377 } 1378 1379 resource "aws_vpc" "foo" { 1380 cidr_block = "10.1.0.0/16" 1381 tags { 1382 Name = "tf-network-test" 1383 } 1384 } 1385 1386 resource "aws_security_group" "tf_test_foo" { 1387 name = "tf_test_foo" 1388 description = "foo" 1389 vpc_id="${aws_vpc.foo.id}" 1390 1391 ingress { 1392 protocol = "icmp" 1393 from_port = -1 1394 to_port = -1 1395 cidr_blocks = ["0.0.0.0/0"] 1396 } 1397 } 1398 1399 resource "aws_subnet" "foo" { 1400 cidr_block = "10.1.1.0/24" 1401 vpc_id = "${aws_vpc.foo.id}" 1402 } 1403 1404 resource "aws_instance" "foo_instance" { 1405 ami = "ami-21f78e11" 1406 instance_type = "t1.micro" 1407 vpc_security_group_ids = ["${aws_security_group.tf_test_foo.id}"] 1408 subnet_id = "${aws_subnet.foo.id}" 1409 associate_public_ip_address = true 1410 depends_on = ["aws_internet_gateway.gw"] 1411 } 1412 1413 resource "aws_eip" "foo_eip" { 1414 instance = "${aws_instance.foo_instance.id}" 1415 vpc = true 1416 depends_on = ["aws_internet_gateway.gw"] 1417 } 1418 ` 1419 1420 const testAccInstanceNetworkInstanceVPCSecurityGroupIDs = ` 1421 resource "aws_internet_gateway" "gw" { 1422 vpc_id = "${aws_vpc.foo.id}" 1423 } 1424 1425 resource "aws_vpc" "foo" { 1426 cidr_block = "10.1.0.0/16" 1427 tags { 1428 Name = "tf-network-test" 1429 } 1430 } 1431 1432 resource "aws_security_group" "tf_test_foo" { 1433 name = "tf_test_foo" 1434 description = "foo" 1435 vpc_id="${aws_vpc.foo.id}" 1436 1437 ingress { 1438 protocol = "icmp" 1439 from_port = -1 1440 to_port = -1 1441 cidr_blocks = ["0.0.0.0/0"] 1442 } 1443 } 1444 1445 resource "aws_subnet" "foo" { 1446 cidr_block = "10.1.1.0/24" 1447 vpc_id = "${aws_vpc.foo.id}" 1448 } 1449 1450 resource "aws_instance" "foo_instance" { 1451 ami = "ami-21f78e11" 1452 instance_type = "t1.micro" 1453 vpc_security_group_ids = ["${aws_security_group.tf_test_foo.id}"] 1454 subnet_id = "${aws_subnet.foo.id}" 1455 depends_on = ["aws_internet_gateway.gw"] 1456 } 1457 1458 resource "aws_eip" "foo_eip" { 1459 instance = "${aws_instance.foo_instance.id}" 1460 vpc = true 1461 depends_on = ["aws_internet_gateway.gw"] 1462 } 1463 ` 1464 1465 const testAccInstanceConfigKeyPair = ` 1466 provider "aws" { 1467 region = "us-east-1" 1468 } 1469 1470 resource "aws_key_pair" "debugging" { 1471 key_name = "tmp-key" 1472 public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD3F6tyPEFEzV0LX3X8BsXdMsQz1x2cEikKDEY0aIj41qgxMCP/iteneqXSIFZBp5vizPvaoIR3Um9xK7PGoW8giupGn+EPuxIA4cDM4vzOqOkiMPhz5XK0whEjkVzTo4+S0puvDZuwIsdiW9mxhJc7tgBNL0cYlWSYVkz4G/fslNfRPW5mYAM49f4fhtxPb5ok4Q2Lg9dPKVHO/Bgeu5woMc7RY0p1ej6D4CKFE6lymSDJpW0YHX/wqE9+cfEauh7xZcG0q9t2ta6F6fmX0agvpFyZo8aFbXeUBr7osSCJNgvavWbM/06niWrOvYX2xwWdhXmXSrbX8ZbabVohBK41 phodgson@thoughtworks.com" 1473 } 1474 1475 resource "aws_instance" "foo" { 1476 ami = "ami-408c7f28" 1477 instance_type = "t1.micro" 1478 key_name = "${aws_key_pair.debugging.key_name}" 1479 tags { 1480 Name = "testAccInstanceConfigKeyPair_TestAMI" 1481 } 1482 } 1483 ` 1484 1485 const testAccInstanceConfigRootBlockDeviceMismatch = ` 1486 resource "aws_vpc" "foo" { 1487 cidr_block = "10.1.0.0/16" 1488 } 1489 1490 resource "aws_subnet" "foo" { 1491 cidr_block = "10.1.1.0/24" 1492 vpc_id = "${aws_vpc.foo.id}" 1493 } 1494 1495 resource "aws_instance" "foo" { 1496 // This is an AMI with RootDeviceName: "/dev/sda1"; actual root: "/dev/sda" 1497 ami = "ami-ef5b69df" 1498 instance_type = "t1.micro" 1499 subnet_id = "${aws_subnet.foo.id}" 1500 root_block_device { 1501 volume_size = 13 1502 } 1503 } 1504 ` 1505 1506 const testAccInstanceConfigForceNewAndTagsDrift = ` 1507 resource "aws_vpc" "foo" { 1508 cidr_block = "10.1.0.0/16" 1509 } 1510 1511 resource "aws_subnet" "foo" { 1512 cidr_block = "10.1.1.0/24" 1513 vpc_id = "${aws_vpc.foo.id}" 1514 } 1515 1516 resource "aws_instance" "foo" { 1517 ami = "ami-22b9a343" 1518 instance_type = "t2.nano" 1519 subnet_id = "${aws_subnet.foo.id}" 1520 } 1521 ` 1522 1523 const testAccInstanceConfigForceNewAndTagsDrift_Update = ` 1524 resource "aws_vpc" "foo" { 1525 cidr_block = "10.1.0.0/16" 1526 } 1527 1528 resource "aws_subnet" "foo" { 1529 cidr_block = "10.1.1.0/24" 1530 vpc_id = "${aws_vpc.foo.id}" 1531 } 1532 1533 resource "aws_instance" "foo" { 1534 ami = "ami-22b9a343" 1535 instance_type = "t2.micro" 1536 subnet_id = "${aws_subnet.foo.id}" 1537 } 1538 `