github.com/candidpartners/terraform@v0.9.5-0.20171005231213-29f5f88820f6/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 Config: testAccCheckInstanceConfigTagsUpdate, 621 Check: resource.ComposeTestCheckFunc( 622 testAccCheckInstanceExists("aws_instance.foo", &v), 623 testAccCheckTags(&v.Tags, "foo", ""), 624 testAccCheckTags(&v.Tags, "bar", "baz"), 625 ), 626 }, 627 }, 628 }) 629 } 630 631 func TestAccAWSInstance_volumeTags(t *testing.T) { 632 var v ec2.Instance 633 634 resource.Test(t, resource.TestCase{ 635 PreCheck: func() { testAccPreCheck(t) }, 636 Providers: testAccProviders, 637 CheckDestroy: testAccCheckInstanceDestroy, 638 Steps: []resource.TestStep{ 639 { 640 Config: testAccCheckInstanceConfigNoVolumeTags, 641 Check: resource.ComposeTestCheckFunc( 642 testAccCheckInstanceExists("aws_instance.foo", &v), 643 resource.TestCheckNoResourceAttr( 644 "aws_instance.foo", "volume_tags"), 645 ), 646 }, 647 { 648 Config: testAccCheckInstanceConfigWithVolumeTags, 649 Check: resource.ComposeTestCheckFunc( 650 testAccCheckInstanceExists("aws_instance.foo", &v), 651 resource.TestCheckResourceAttr( 652 "aws_instance.foo", "volume_tags.%", "1"), 653 resource.TestCheckResourceAttr( 654 "aws_instance.foo", "volume_tags.Name", "acceptance-test-volume-tag"), 655 ), 656 }, 657 { 658 Config: testAccCheckInstanceConfigWithVolumeTagsUpdate, 659 Check: resource.ComposeTestCheckFunc( 660 testAccCheckInstanceExists("aws_instance.foo", &v), 661 resource.TestCheckResourceAttr( 662 "aws_instance.foo", "volume_tags.%", "2"), 663 resource.TestCheckResourceAttr( 664 "aws_instance.foo", "volume_tags.Name", "acceptance-test-volume-tag"), 665 resource.TestCheckResourceAttr( 666 "aws_instance.foo", "volume_tags.Environment", "dev"), 667 ), 668 }, 669 { 670 Config: testAccCheckInstanceConfigNoVolumeTags, 671 Check: resource.ComposeTestCheckFunc( 672 testAccCheckInstanceExists("aws_instance.foo", &v), 673 resource.TestCheckNoResourceAttr( 674 "aws_instance.foo", "volume_tags"), 675 ), 676 }, 677 }, 678 }) 679 } 680 681 func TestAccAWSInstance_volumeTagsComputed(t *testing.T) { 682 var v ec2.Instance 683 684 resource.Test(t, resource.TestCase{ 685 PreCheck: func() { testAccPreCheck(t) }, 686 Providers: testAccProviders, 687 CheckDestroy: testAccCheckInstanceDestroy, 688 Steps: []resource.TestStep{ 689 { 690 Config: testAccCheckInstanceConfigWithAttachedVolume, 691 Check: resource.ComposeTestCheckFunc( 692 testAccCheckInstanceExists("aws_instance.foo", &v), 693 ), 694 ExpectNonEmptyPlan: false, 695 }, 696 }, 697 }) 698 } 699 700 func TestAccAWSInstance_instanceProfileChange(t *testing.T) { 701 var v ec2.Instance 702 rName := acctest.RandString(5) 703 704 testCheckInstanceProfile := func() resource.TestCheckFunc { 705 return func(*terraform.State) error { 706 if v.IamInstanceProfile == nil { 707 return fmt.Errorf("Instance Profile is nil - we expected an InstanceProfile associated with the Instance") 708 } 709 710 return nil 711 } 712 } 713 714 resource.Test(t, resource.TestCase{ 715 PreCheck: func() { testAccPreCheck(t) }, 716 IDRefreshName: "aws_instance.foo", 717 Providers: testAccProviders, 718 CheckDestroy: testAccCheckInstanceDestroy, 719 Steps: []resource.TestStep{ 720 { 721 Config: testAccInstanceConfigWithoutInstanceProfile(rName), 722 Check: resource.ComposeTestCheckFunc( 723 testAccCheckInstanceExists("aws_instance.foo", &v), 724 ), 725 }, 726 { 727 Config: testAccInstanceConfigWithInstanceProfile(rName), 728 Check: resource.ComposeTestCheckFunc( 729 testAccCheckInstanceExists("aws_instance.foo", &v), 730 testCheckInstanceProfile(), 731 ), 732 }, 733 }, 734 }) 735 } 736 737 func TestAccAWSInstance_withIamInstanceProfile(t *testing.T) { 738 var v ec2.Instance 739 rName := acctest.RandString(5) 740 741 testCheckInstanceProfile := func() resource.TestCheckFunc { 742 return func(*terraform.State) error { 743 if v.IamInstanceProfile == nil { 744 return fmt.Errorf("Instance Profile is nil - we expected an InstanceProfile associated with the Instance") 745 } 746 747 return nil 748 } 749 } 750 751 resource.Test(t, resource.TestCase{ 752 PreCheck: func() { testAccPreCheck(t) }, 753 IDRefreshName: "aws_instance.foo", 754 Providers: testAccProviders, 755 CheckDestroy: testAccCheckInstanceDestroy, 756 Steps: []resource.TestStep{ 757 { 758 Config: testAccInstanceConfigWithInstanceProfile(rName), 759 Check: resource.ComposeTestCheckFunc( 760 testAccCheckInstanceExists("aws_instance.foo", &v), 761 testCheckInstanceProfile(), 762 ), 763 }, 764 }, 765 }) 766 } 767 768 func TestAccAWSInstance_privateIP(t *testing.T) { 769 var v ec2.Instance 770 771 testCheckPrivateIP := func() resource.TestCheckFunc { 772 return func(*terraform.State) error { 773 if *v.PrivateIpAddress != "10.1.1.42" { 774 return fmt.Errorf("bad private IP: %s", *v.PrivateIpAddress) 775 } 776 777 return nil 778 } 779 } 780 781 resource.Test(t, resource.TestCase{ 782 PreCheck: func() { testAccPreCheck(t) }, 783 IDRefreshName: "aws_instance.foo", 784 Providers: testAccProviders, 785 CheckDestroy: testAccCheckInstanceDestroy, 786 Steps: []resource.TestStep{ 787 { 788 Config: testAccInstanceConfigPrivateIP, 789 Check: resource.ComposeTestCheckFunc( 790 testAccCheckInstanceExists("aws_instance.foo", &v), 791 testCheckPrivateIP(), 792 ), 793 }, 794 }, 795 }) 796 } 797 798 func TestAccAWSInstance_associatePublicIPAndPrivateIP(t *testing.T) { 799 var v ec2.Instance 800 801 testCheckPrivateIP := func() resource.TestCheckFunc { 802 return func(*terraform.State) error { 803 if *v.PrivateIpAddress != "10.1.1.42" { 804 return fmt.Errorf("bad private IP: %s", *v.PrivateIpAddress) 805 } 806 807 return nil 808 } 809 } 810 811 resource.Test(t, resource.TestCase{ 812 PreCheck: func() { testAccPreCheck(t) }, 813 IDRefreshName: "aws_instance.foo", 814 IDRefreshIgnore: []string{"associate_public_ip_address"}, 815 Providers: testAccProviders, 816 CheckDestroy: testAccCheckInstanceDestroy, 817 Steps: []resource.TestStep{ 818 { 819 Config: testAccInstanceConfigAssociatePublicIPAndPrivateIP, 820 Check: resource.ComposeTestCheckFunc( 821 testAccCheckInstanceExists("aws_instance.foo", &v), 822 testCheckPrivateIP(), 823 ), 824 }, 825 }, 826 }) 827 } 828 829 // Guard against regression with KeyPairs 830 // https://github.com/hashicorp/terraform/issues/2302 831 func TestAccAWSInstance_keyPairCheck(t *testing.T) { 832 var v ec2.Instance 833 834 testCheckKeyPair := func(keyName string) resource.TestCheckFunc { 835 return func(*terraform.State) error { 836 if v.KeyName == nil { 837 return fmt.Errorf("No Key Pair found, expected(%s)", keyName) 838 } 839 if v.KeyName != nil && *v.KeyName != keyName { 840 return fmt.Errorf("Bad key name, expected (%s), got (%s)", keyName, *v.KeyName) 841 } 842 843 return nil 844 } 845 } 846 847 resource.Test(t, resource.TestCase{ 848 PreCheck: func() { testAccPreCheck(t) }, 849 IDRefreshName: "aws_instance.foo", 850 IDRefreshIgnore: []string{"source_dest_check"}, 851 Providers: testAccProviders, 852 CheckDestroy: testAccCheckInstanceDestroy, 853 Steps: []resource.TestStep{ 854 { 855 Config: testAccInstanceConfigKeyPair, 856 Check: resource.ComposeTestCheckFunc( 857 testAccCheckInstanceExists("aws_instance.foo", &v), 858 testCheckKeyPair("tmp-key"), 859 ), 860 }, 861 }, 862 }) 863 } 864 865 func TestAccAWSInstance_rootBlockDeviceMismatch(t *testing.T) { 866 var v ec2.Instance 867 868 resource.Test(t, resource.TestCase{ 869 PreCheck: func() { testAccPreCheck(t) }, 870 Providers: testAccProviders, 871 CheckDestroy: testAccCheckInstanceDestroy, 872 Steps: []resource.TestStep{ 873 { 874 Config: testAccInstanceConfigRootBlockDeviceMismatch, 875 Check: resource.ComposeTestCheckFunc( 876 testAccCheckInstanceExists("aws_instance.foo", &v), 877 resource.TestCheckResourceAttr( 878 "aws_instance.foo", "root_block_device.0.volume_size", "13"), 879 ), 880 }, 881 }, 882 }) 883 } 884 885 // This test reproduces the bug here: 886 // https://github.com/hashicorp/terraform/issues/1752 887 // 888 // I wish there were a way to exercise resources built with helper.Schema in a 889 // unit context, in which case this test could be moved there, but for now this 890 // will cover the bugfix. 891 // 892 // The following triggers "diffs didn't match during apply" without the fix in to 893 // set NewRemoved on the .# field when it changes to 0. 894 func TestAccAWSInstance_forceNewAndTagsDrift(t *testing.T) { 895 var v ec2.Instance 896 897 resource.Test(t, resource.TestCase{ 898 PreCheck: func() { testAccPreCheck(t) }, 899 IDRefreshName: "aws_instance.foo", 900 Providers: testAccProviders, 901 CheckDestroy: testAccCheckInstanceDestroy, 902 Steps: []resource.TestStep{ 903 { 904 Config: testAccInstanceConfigForceNewAndTagsDrift, 905 Check: resource.ComposeTestCheckFunc( 906 testAccCheckInstanceExists("aws_instance.foo", &v), 907 driftTags(&v), 908 ), 909 ExpectNonEmptyPlan: true, 910 }, 911 { 912 Config: testAccInstanceConfigForceNewAndTagsDrift_Update, 913 Check: resource.ComposeTestCheckFunc( 914 testAccCheckInstanceExists("aws_instance.foo", &v), 915 ), 916 }, 917 }, 918 }) 919 } 920 921 func TestAccAWSInstance_changeInstanceType(t *testing.T) { 922 var before ec2.Instance 923 var after ec2.Instance 924 925 resource.Test(t, resource.TestCase{ 926 PreCheck: func() { testAccPreCheck(t) }, 927 Providers: testAccProviders, 928 CheckDestroy: testAccCheckInstanceDestroy, 929 Steps: []resource.TestStep{ 930 { 931 Config: testAccInstanceConfigWithSmallInstanceType, 932 Check: resource.ComposeTestCheckFunc( 933 testAccCheckInstanceExists("aws_instance.foo", &before), 934 ), 935 }, 936 { 937 Config: testAccInstanceConfigUpdateInstanceType, 938 Check: resource.ComposeTestCheckFunc( 939 testAccCheckInstanceExists("aws_instance.foo", &after), 940 testAccCheckInstanceNotRecreated( 941 t, &before, &after), 942 ), 943 }, 944 }, 945 }) 946 } 947 948 func TestAccAWSInstance_primaryNetworkInterface(t *testing.T) { 949 var instance ec2.Instance 950 var ini ec2.NetworkInterface 951 952 resource.Test(t, resource.TestCase{ 953 PreCheck: func() { testAccPreCheck(t) }, 954 Providers: testAccProviders, 955 CheckDestroy: testAccCheckInstanceDestroy, 956 Steps: []resource.TestStep{ 957 { 958 Config: testAccInstanceConfigPrimaryNetworkInterface, 959 Check: resource.ComposeTestCheckFunc( 960 testAccCheckInstanceExists("aws_instance.foo", &instance), 961 testAccCheckAWSENIExists("aws_network_interface.bar", &ini), 962 resource.TestCheckResourceAttr("aws_instance.foo", "network_interface.#", "1"), 963 ), 964 }, 965 }, 966 }) 967 } 968 969 func TestAccAWSInstance_addSecondaryInterface(t *testing.T) { 970 var before ec2.Instance 971 var after ec2.Instance 972 var iniPrimary ec2.NetworkInterface 973 var iniSecondary ec2.NetworkInterface 974 975 resource.Test(t, resource.TestCase{ 976 PreCheck: func() { testAccPreCheck(t) }, 977 Providers: testAccProviders, 978 CheckDestroy: testAccCheckInstanceDestroy, 979 Steps: []resource.TestStep{ 980 { 981 Config: testAccInstanceConfigAddSecondaryNetworkInterfaceBefore, 982 Check: resource.ComposeTestCheckFunc( 983 testAccCheckInstanceExists("aws_instance.foo", &before), 984 testAccCheckAWSENIExists("aws_network_interface.primary", &iniPrimary), 985 resource.TestCheckResourceAttr("aws_instance.foo", "network_interface.#", "1"), 986 ), 987 }, 988 { 989 Config: testAccInstanceConfigAddSecondaryNetworkInterfaceAfter, 990 Check: resource.ComposeTestCheckFunc( 991 testAccCheckInstanceExists("aws_instance.foo", &after), 992 testAccCheckAWSENIExists("aws_network_interface.secondary", &iniSecondary), 993 resource.TestCheckResourceAttr("aws_instance.foo", "network_interface.#", "1"), 994 ), 995 }, 996 }, 997 }) 998 } 999 1000 func testAccCheckInstanceNotRecreated(t *testing.T, 1001 before, after *ec2.Instance) resource.TestCheckFunc { 1002 return func(s *terraform.State) error { 1003 if *before.InstanceId != *after.InstanceId { 1004 t.Fatalf("AWS Instance IDs have changed. Before %s. After %s", *before.InstanceId, *after.InstanceId) 1005 } 1006 return nil 1007 } 1008 } 1009 1010 func testAccCheckInstanceDestroy(s *terraform.State) error { 1011 return testAccCheckInstanceDestroyWithProvider(s, testAccProvider) 1012 } 1013 1014 func testAccCheckInstanceDestroyWithProviders(providers *[]*schema.Provider) resource.TestCheckFunc { 1015 return func(s *terraform.State) error { 1016 for _, provider := range *providers { 1017 if provider.Meta() == nil { 1018 continue 1019 } 1020 if err := testAccCheckInstanceDestroyWithProvider(s, provider); err != nil { 1021 return err 1022 } 1023 } 1024 return nil 1025 } 1026 } 1027 1028 func testAccCheckInstanceDestroyWithProvider(s *terraform.State, provider *schema.Provider) error { 1029 conn := provider.Meta().(*AWSClient).ec2conn 1030 1031 for _, rs := range s.RootModule().Resources { 1032 if rs.Type != "aws_instance" { 1033 continue 1034 } 1035 1036 // Try to find the resource 1037 resp, err := conn.DescribeInstances(&ec2.DescribeInstancesInput{ 1038 InstanceIds: []*string{aws.String(rs.Primary.ID)}, 1039 }) 1040 if err == nil { 1041 for _, r := range resp.Reservations { 1042 for _, i := range r.Instances { 1043 if i.State != nil && *i.State.Name != "terminated" { 1044 return fmt.Errorf("Found unterminated instance: %s", i) 1045 } 1046 } 1047 } 1048 } 1049 1050 // Verify the error is what we want 1051 if ae, ok := err.(awserr.Error); ok && ae.Code() == "InvalidInstanceID.NotFound" { 1052 continue 1053 } 1054 1055 return err 1056 } 1057 1058 return nil 1059 } 1060 1061 func testAccCheckInstanceExists(n string, i *ec2.Instance) resource.TestCheckFunc { 1062 providers := []*schema.Provider{testAccProvider} 1063 return testAccCheckInstanceExistsWithProviders(n, i, &providers) 1064 } 1065 1066 func testAccCheckInstanceExistsWithProviders(n string, i *ec2.Instance, providers *[]*schema.Provider) resource.TestCheckFunc { 1067 return func(s *terraform.State) error { 1068 rs, ok := s.RootModule().Resources[n] 1069 if !ok { 1070 return fmt.Errorf("Not found: %s", n) 1071 } 1072 1073 if rs.Primary.ID == "" { 1074 return fmt.Errorf("No ID is set") 1075 } 1076 for _, provider := range *providers { 1077 // Ignore if Meta is empty, this can happen for validation providers 1078 if provider.Meta() == nil { 1079 continue 1080 } 1081 1082 conn := provider.Meta().(*AWSClient).ec2conn 1083 resp, err := conn.DescribeInstances(&ec2.DescribeInstancesInput{ 1084 InstanceIds: []*string{aws.String(rs.Primary.ID)}, 1085 }) 1086 if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "InvalidInstanceID.NotFound" { 1087 continue 1088 } 1089 if err != nil { 1090 return err 1091 } 1092 1093 if len(resp.Reservations) > 0 { 1094 *i = *resp.Reservations[0].Instances[0] 1095 return nil 1096 } 1097 } 1098 1099 return fmt.Errorf("Instance not found") 1100 } 1101 } 1102 1103 func TestInstanceTenancySchema(t *testing.T) { 1104 actualSchema := resourceAwsInstance().Schema["tenancy"] 1105 expectedSchema := &schema.Schema{ 1106 Type: schema.TypeString, 1107 Optional: true, 1108 Computed: true, 1109 ForceNew: true, 1110 } 1111 if !reflect.DeepEqual(actualSchema, expectedSchema) { 1112 t.Fatalf( 1113 "Got:\n\n%#v\n\nExpected:\n\n%#v\n", 1114 actualSchema, 1115 expectedSchema) 1116 } 1117 } 1118 1119 func driftTags(instance *ec2.Instance) resource.TestCheckFunc { 1120 return func(s *terraform.State) error { 1121 conn := testAccProvider.Meta().(*AWSClient).ec2conn 1122 _, err := conn.CreateTags(&ec2.CreateTagsInput{ 1123 Resources: []*string{instance.InstanceId}, 1124 Tags: []*ec2.Tag{ 1125 { 1126 Key: aws.String("Drift"), 1127 Value: aws.String("Happens"), 1128 }, 1129 }, 1130 }) 1131 return err 1132 } 1133 } 1134 1135 const testAccInstanceConfig_pre = ` 1136 resource "aws_security_group" "tf_test_foo" { 1137 name = "tf_test_foo" 1138 description = "foo" 1139 1140 ingress { 1141 protocol = "icmp" 1142 from_port = -1 1143 to_port = -1 1144 cidr_blocks = ["0.0.0.0/0"] 1145 } 1146 } 1147 ` 1148 1149 const testAccInstanceConfig = ` 1150 resource "aws_security_group" "tf_test_foo" { 1151 name = "tf_test_foo" 1152 description = "foo" 1153 1154 ingress { 1155 protocol = "icmp" 1156 from_port = -1 1157 to_port = -1 1158 cidr_blocks = ["0.0.0.0/0"] 1159 } 1160 } 1161 1162 resource "aws_instance" "foo" { 1163 # us-west-2 1164 ami = "ami-4fccb37f" 1165 availability_zone = "us-west-2a" 1166 1167 instance_type = "m1.small" 1168 security_groups = ["${aws_security_group.tf_test_foo.name}"] 1169 user_data = "foo:-with-character's" 1170 } 1171 ` 1172 1173 const testAccInstanceConfigWithSmallInstanceType = ` 1174 resource "aws_instance" "foo" { 1175 # us-west-2 1176 ami = "ami-55a7ea65" 1177 availability_zone = "us-west-2a" 1178 1179 instance_type = "m3.medium" 1180 1181 tags { 1182 Name = "tf-acctest" 1183 } 1184 } 1185 ` 1186 1187 const testAccInstanceConfigUpdateInstanceType = ` 1188 resource "aws_instance" "foo" { 1189 # us-west-2 1190 ami = "ami-55a7ea65" 1191 availability_zone = "us-west-2a" 1192 1193 instance_type = "m3.large" 1194 1195 tags { 1196 Name = "tf-acctest" 1197 } 1198 } 1199 ` 1200 1201 const testAccInstanceGP2IopsDevice = ` 1202 resource "aws_instance" "foo" { 1203 # us-west-2 1204 ami = "ami-55a7ea65" 1205 1206 # In order to attach an encrypted volume to an instance you need to have an 1207 # m3.medium or larger. See "Supported Instance Types" in: 1208 # http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EBSEncryption.html 1209 instance_type = "m3.medium" 1210 1211 root_block_device { 1212 volume_type = "gp2" 1213 volume_size = 11 1214 } 1215 } 1216 ` 1217 1218 const testAccInstanceConfigBlockDevices = ` 1219 resource "aws_instance" "foo" { 1220 # us-west-2 1221 ami = "ami-55a7ea65" 1222 1223 # In order to attach an encrypted volume to an instance you need to have an 1224 # m3.medium or larger. See "Supported Instance Types" in: 1225 # http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EBSEncryption.html 1226 instance_type = "m3.medium" 1227 1228 root_block_device { 1229 volume_type = "gp2" 1230 volume_size = 11 1231 } 1232 ebs_block_device { 1233 device_name = "/dev/sdb" 1234 volume_size = 9 1235 } 1236 ebs_block_device { 1237 device_name = "/dev/sdc" 1238 volume_size = 10 1239 volume_type = "io1" 1240 iops = 100 1241 } 1242 1243 # Encrypted ebs block device 1244 ebs_block_device { 1245 device_name = "/dev/sdd" 1246 volume_size = 12 1247 encrypted = true 1248 } 1249 1250 ephemeral_block_device { 1251 device_name = "/dev/sde" 1252 virtual_name = "ephemeral0" 1253 } 1254 } 1255 ` 1256 1257 const testAccInstanceConfigSourceDestEnable = ` 1258 resource "aws_vpc" "foo" { 1259 cidr_block = "10.1.0.0/16" 1260 } 1261 1262 resource "aws_subnet" "foo" { 1263 cidr_block = "10.1.1.0/24" 1264 vpc_id = "${aws_vpc.foo.id}" 1265 } 1266 1267 resource "aws_instance" "foo" { 1268 # us-west-2 1269 ami = "ami-4fccb37f" 1270 instance_type = "m1.small" 1271 subnet_id = "${aws_subnet.foo.id}" 1272 } 1273 ` 1274 1275 const testAccInstanceConfigSourceDestDisable = ` 1276 resource "aws_vpc" "foo" { 1277 cidr_block = "10.1.0.0/16" 1278 } 1279 1280 resource "aws_subnet" "foo" { 1281 cidr_block = "10.1.1.0/24" 1282 vpc_id = "${aws_vpc.foo.id}" 1283 } 1284 1285 resource "aws_instance" "foo" { 1286 # us-west-2 1287 ami = "ami-4fccb37f" 1288 instance_type = "m1.small" 1289 subnet_id = "${aws_subnet.foo.id}" 1290 source_dest_check = false 1291 } 1292 ` 1293 1294 func testAccInstanceConfigDisableAPITermination(val bool) string { 1295 return fmt.Sprintf(` 1296 resource "aws_vpc" "foo" { 1297 cidr_block = "10.1.0.0/16" 1298 } 1299 1300 resource "aws_subnet" "foo" { 1301 cidr_block = "10.1.1.0/24" 1302 vpc_id = "${aws_vpc.foo.id}" 1303 } 1304 1305 resource "aws_instance" "foo" { 1306 # us-west-2 1307 ami = "ami-4fccb37f" 1308 instance_type = "m1.small" 1309 subnet_id = "${aws_subnet.foo.id}" 1310 disable_api_termination = %t 1311 } 1312 `, val) 1313 } 1314 1315 const testAccInstanceConfigVPC = ` 1316 resource "aws_vpc" "foo" { 1317 cidr_block = "10.1.0.0/16" 1318 } 1319 1320 resource "aws_subnet" "foo" { 1321 cidr_block = "10.1.1.0/24" 1322 vpc_id = "${aws_vpc.foo.id}" 1323 } 1324 1325 resource "aws_instance" "foo" { 1326 # us-west-2 1327 ami = "ami-4fccb37f" 1328 instance_type = "m1.small" 1329 subnet_id = "${aws_subnet.foo.id}" 1330 associate_public_ip_address = true 1331 tenancy = "dedicated" 1332 # pre-encoded base64 data 1333 user_data = "3dc39dda39be1205215e776bad998da361a5955d" 1334 } 1335 ` 1336 1337 const testAccInstanceConfigIpv6Support = ` 1338 resource "aws_vpc" "foo" { 1339 cidr_block = "10.1.0.0/16" 1340 assign_generated_ipv6_cidr_block = true 1341 tags { 1342 Name = "tf-ipv6-instance-acc-test" 1343 } 1344 } 1345 1346 resource "aws_subnet" "foo" { 1347 cidr_block = "10.1.1.0/24" 1348 vpc_id = "${aws_vpc.foo.id}" 1349 ipv6_cidr_block = "${cidrsubnet(aws_vpc.foo.ipv6_cidr_block, 8, 1)}" 1350 tags { 1351 Name = "tf-ipv6-instance-acc-test" 1352 } 1353 } 1354 1355 resource "aws_instance" "foo" { 1356 # us-west-2 1357 ami = "ami-c5eabbf5" 1358 instance_type = "t2.micro" 1359 subnet_id = "${aws_subnet.foo.id}" 1360 1361 ipv6_address_count = 1 1362 tags { 1363 Name = "tf-ipv6-instance-acc-test" 1364 } 1365 } 1366 ` 1367 1368 const testAccInstanceConfigMultipleRegions = ` 1369 provider "aws" { 1370 alias = "west" 1371 region = "us-west-2" 1372 } 1373 1374 provider "aws" { 1375 alias = "east" 1376 region = "us-east-1" 1377 } 1378 1379 resource "aws_instance" "foo" { 1380 # us-west-2 1381 provider = "aws.west" 1382 ami = "ami-4fccb37f" 1383 instance_type = "m1.small" 1384 } 1385 1386 resource "aws_instance" "bar" { 1387 # us-east-1 1388 provider = "aws.east" 1389 ami = "ami-8c6ea9e4" 1390 instance_type = "m1.small" 1391 } 1392 ` 1393 1394 const testAccCheckInstanceConfigTags = ` 1395 resource "aws_instance" "foo" { 1396 ami = "ami-4fccb37f" 1397 instance_type = "m1.small" 1398 tags { 1399 foo = "bar" 1400 } 1401 } 1402 ` 1403 1404 const testAccCheckInstanceConfigWithAttachedVolume = ` 1405 data "aws_ami" "debian_jessie_latest" { 1406 most_recent = true 1407 1408 filter { 1409 name = "name" 1410 values = ["debian-jessie-*"] 1411 } 1412 1413 filter { 1414 name = "virtualization-type" 1415 values = ["hvm"] 1416 } 1417 1418 filter { 1419 name = "architecture" 1420 values = ["x86_64"] 1421 } 1422 1423 filter { 1424 name = "root-device-type" 1425 values = ["ebs"] 1426 } 1427 1428 owners = ["379101102735"] # Debian 1429 } 1430 1431 resource "aws_instance" "foo" { 1432 ami = "${data.aws_ami.debian_jessie_latest.id}" 1433 associate_public_ip_address = true 1434 count = 1 1435 instance_type = "t2.medium" 1436 1437 root_block_device { 1438 volume_size = "10" 1439 volume_type = "standard" 1440 delete_on_termination = true 1441 } 1442 1443 tags { 1444 Name = "test-terraform" 1445 } 1446 } 1447 1448 resource "aws_ebs_volume" "test" { 1449 depends_on = ["aws_instance.foo"] 1450 availability_zone = "${aws_instance.foo.availability_zone}" 1451 type = "gp2" 1452 size = "10" 1453 1454 tags { 1455 Name = "test-terraform" 1456 } 1457 } 1458 1459 resource "aws_volume_attachment" "test" { 1460 depends_on = ["aws_ebs_volume.test"] 1461 device_name = "/dev/xvdg" 1462 volume_id = "${aws_ebs_volume.test.id}" 1463 instance_id = "${aws_instance.foo.id}" 1464 } 1465 ` 1466 1467 const testAccCheckInstanceConfigNoVolumeTags = ` 1468 resource "aws_instance" "foo" { 1469 ami = "ami-55a7ea65" 1470 1471 instance_type = "m3.medium" 1472 1473 root_block_device { 1474 volume_type = "gp2" 1475 volume_size = 11 1476 } 1477 ebs_block_device { 1478 device_name = "/dev/sdb" 1479 volume_size = 9 1480 } 1481 ebs_block_device { 1482 device_name = "/dev/sdc" 1483 volume_size = 10 1484 volume_type = "io1" 1485 iops = 100 1486 } 1487 1488 ebs_block_device { 1489 device_name = "/dev/sdd" 1490 volume_size = 12 1491 encrypted = true 1492 } 1493 1494 ephemeral_block_device { 1495 device_name = "/dev/sde" 1496 virtual_name = "ephemeral0" 1497 } 1498 } 1499 ` 1500 1501 const testAccCheckInstanceConfigWithVolumeTags = ` 1502 resource "aws_instance" "foo" { 1503 ami = "ami-55a7ea65" 1504 1505 instance_type = "m3.medium" 1506 1507 root_block_device { 1508 volume_type = "gp2" 1509 volume_size = 11 1510 } 1511 ebs_block_device { 1512 device_name = "/dev/sdb" 1513 volume_size = 9 1514 } 1515 ebs_block_device { 1516 device_name = "/dev/sdc" 1517 volume_size = 10 1518 volume_type = "io1" 1519 iops = 100 1520 } 1521 1522 ebs_block_device { 1523 device_name = "/dev/sdd" 1524 volume_size = 12 1525 encrypted = true 1526 } 1527 1528 ephemeral_block_device { 1529 device_name = "/dev/sde" 1530 virtual_name = "ephemeral0" 1531 } 1532 1533 volume_tags { 1534 Name = "acceptance-test-volume-tag" 1535 } 1536 } 1537 ` 1538 1539 const testAccCheckInstanceConfigWithVolumeTagsUpdate = ` 1540 resource "aws_instance" "foo" { 1541 ami = "ami-55a7ea65" 1542 1543 instance_type = "m3.medium" 1544 1545 root_block_device { 1546 volume_type = "gp2" 1547 volume_size = 11 1548 } 1549 ebs_block_device { 1550 device_name = "/dev/sdb" 1551 volume_size = 9 1552 } 1553 ebs_block_device { 1554 device_name = "/dev/sdc" 1555 volume_size = 10 1556 volume_type = "io1" 1557 iops = 100 1558 } 1559 1560 ebs_block_device { 1561 device_name = "/dev/sdd" 1562 volume_size = 12 1563 encrypted = true 1564 } 1565 1566 ephemeral_block_device { 1567 device_name = "/dev/sde" 1568 virtual_name = "ephemeral0" 1569 } 1570 1571 volume_tags { 1572 Name = "acceptance-test-volume-tag" 1573 Environment = "dev" 1574 } 1575 } 1576 ` 1577 1578 const testAccCheckInstanceConfigTagsUpdate = ` 1579 resource "aws_instance" "foo" { 1580 ami = "ami-4fccb37f" 1581 instance_type = "m1.small" 1582 tags { 1583 bar = "baz" 1584 } 1585 } 1586 ` 1587 1588 func testAccInstanceConfigWithoutInstanceProfile(rName string) string { 1589 return fmt.Sprintf(` 1590 resource "aws_iam_role" "test" { 1591 name = "test-%s" 1592 assume_role_policy = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"Service\":[\"ec2.amazonaws.com\"]},\"Action\":[\"sts:AssumeRole\"]}]}" 1593 } 1594 1595 resource "aws_iam_instance_profile" "test" { 1596 name = "test-%s" 1597 roles = ["${aws_iam_role.test.name}"] 1598 } 1599 1600 resource "aws_instance" "foo" { 1601 ami = "ami-4fccb37f" 1602 instance_type = "m1.small" 1603 tags { 1604 bar = "baz" 1605 } 1606 }`, rName, rName) 1607 } 1608 1609 func testAccInstanceConfigWithInstanceProfile(rName string) string { 1610 return fmt.Sprintf(` 1611 resource "aws_iam_role" "test" { 1612 name = "test-%s" 1613 assume_role_policy = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"Service\":[\"ec2.amazonaws.com\"]},\"Action\":[\"sts:AssumeRole\"]}]}" 1614 } 1615 1616 resource "aws_iam_instance_profile" "test" { 1617 name = "test-%s" 1618 roles = ["${aws_iam_role.test.name}"] 1619 } 1620 1621 resource "aws_instance" "foo" { 1622 ami = "ami-4fccb37f" 1623 instance_type = "m1.small" 1624 iam_instance_profile = "${aws_iam_instance_profile.test.name}" 1625 tags { 1626 bar = "baz" 1627 } 1628 }`, rName, rName) 1629 } 1630 1631 const testAccInstanceConfigPrivateIP = ` 1632 resource "aws_vpc" "foo" { 1633 cidr_block = "10.1.0.0/16" 1634 } 1635 1636 resource "aws_subnet" "foo" { 1637 cidr_block = "10.1.1.0/24" 1638 vpc_id = "${aws_vpc.foo.id}" 1639 } 1640 1641 resource "aws_instance" "foo" { 1642 ami = "ami-c5eabbf5" 1643 instance_type = "t2.micro" 1644 subnet_id = "${aws_subnet.foo.id}" 1645 private_ip = "10.1.1.42" 1646 } 1647 ` 1648 1649 const testAccInstanceConfigAssociatePublicIPAndPrivateIP = ` 1650 resource "aws_vpc" "foo" { 1651 cidr_block = "10.1.0.0/16" 1652 } 1653 1654 resource "aws_subnet" "foo" { 1655 cidr_block = "10.1.1.0/24" 1656 vpc_id = "${aws_vpc.foo.id}" 1657 } 1658 1659 resource "aws_instance" "foo" { 1660 ami = "ami-c5eabbf5" 1661 instance_type = "t2.micro" 1662 subnet_id = "${aws_subnet.foo.id}" 1663 associate_public_ip_address = true 1664 private_ip = "10.1.1.42" 1665 } 1666 ` 1667 1668 const testAccInstanceNetworkInstanceSecurityGroups = ` 1669 resource "aws_internet_gateway" "gw" { 1670 vpc_id = "${aws_vpc.foo.id}" 1671 } 1672 1673 resource "aws_vpc" "foo" { 1674 cidr_block = "10.1.0.0/16" 1675 tags { 1676 Name = "tf-network-test" 1677 } 1678 } 1679 1680 resource "aws_security_group" "tf_test_foo" { 1681 name = "tf_test_foo" 1682 description = "foo" 1683 vpc_id="${aws_vpc.foo.id}" 1684 1685 ingress { 1686 protocol = "icmp" 1687 from_port = -1 1688 to_port = -1 1689 cidr_blocks = ["0.0.0.0/0"] 1690 } 1691 } 1692 1693 resource "aws_subnet" "foo" { 1694 cidr_block = "10.1.1.0/24" 1695 vpc_id = "${aws_vpc.foo.id}" 1696 } 1697 1698 resource "aws_instance" "foo_instance" { 1699 ami = "ami-21f78e11" 1700 instance_type = "t1.micro" 1701 vpc_security_group_ids = ["${aws_security_group.tf_test_foo.id}"] 1702 subnet_id = "${aws_subnet.foo.id}" 1703 associate_public_ip_address = true 1704 depends_on = ["aws_internet_gateway.gw"] 1705 } 1706 1707 resource "aws_eip" "foo_eip" { 1708 instance = "${aws_instance.foo_instance.id}" 1709 vpc = true 1710 depends_on = ["aws_internet_gateway.gw"] 1711 } 1712 ` 1713 1714 const testAccInstanceNetworkInstanceVPCSecurityGroupIDs = ` 1715 resource "aws_internet_gateway" "gw" { 1716 vpc_id = "${aws_vpc.foo.id}" 1717 } 1718 1719 resource "aws_vpc" "foo" { 1720 cidr_block = "10.1.0.0/16" 1721 tags { 1722 Name = "tf-network-test" 1723 } 1724 } 1725 1726 resource "aws_security_group" "tf_test_foo" { 1727 name = "tf_test_foo" 1728 description = "foo" 1729 vpc_id="${aws_vpc.foo.id}" 1730 1731 ingress { 1732 protocol = "icmp" 1733 from_port = -1 1734 to_port = -1 1735 cidr_blocks = ["0.0.0.0/0"] 1736 } 1737 } 1738 1739 resource "aws_subnet" "foo" { 1740 cidr_block = "10.1.1.0/24" 1741 vpc_id = "${aws_vpc.foo.id}" 1742 } 1743 1744 resource "aws_instance" "foo_instance" { 1745 ami = "ami-21f78e11" 1746 instance_type = "t1.micro" 1747 vpc_security_group_ids = ["${aws_security_group.tf_test_foo.id}"] 1748 subnet_id = "${aws_subnet.foo.id}" 1749 depends_on = ["aws_internet_gateway.gw"] 1750 } 1751 1752 resource "aws_eip" "foo_eip" { 1753 instance = "${aws_instance.foo_instance.id}" 1754 vpc = true 1755 depends_on = ["aws_internet_gateway.gw"] 1756 } 1757 ` 1758 1759 const testAccInstanceConfigKeyPair = ` 1760 provider "aws" { 1761 region = "us-east-1" 1762 } 1763 1764 resource "aws_key_pair" "debugging" { 1765 key_name = "tmp-key" 1766 public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD3F6tyPEFEzV0LX3X8BsXdMsQz1x2cEikKDEY0aIj41qgxMCP/iteneqXSIFZBp5vizPvaoIR3Um9xK7PGoW8giupGn+EPuxIA4cDM4vzOqOkiMPhz5XK0whEjkVzTo4+S0puvDZuwIsdiW9mxhJc7tgBNL0cYlWSYVkz4G/fslNfRPW5mYAM49f4fhtxPb5ok4Q2Lg9dPKVHO/Bgeu5woMc7RY0p1ej6D4CKFE6lymSDJpW0YHX/wqE9+cfEauh7xZcG0q9t2ta6F6fmX0agvpFyZo8aFbXeUBr7osSCJNgvavWbM/06niWrOvYX2xwWdhXmXSrbX8ZbabVohBK41 phodgson@thoughtworks.com" 1767 } 1768 1769 resource "aws_instance" "foo" { 1770 ami = "ami-408c7f28" 1771 instance_type = "t1.micro" 1772 key_name = "${aws_key_pair.debugging.key_name}" 1773 tags { 1774 Name = "testAccInstanceConfigKeyPair_TestAMI" 1775 } 1776 } 1777 ` 1778 1779 const testAccInstanceConfigRootBlockDeviceMismatch = ` 1780 resource "aws_vpc" "foo" { 1781 cidr_block = "10.1.0.0/16" 1782 } 1783 1784 resource "aws_subnet" "foo" { 1785 cidr_block = "10.1.1.0/24" 1786 vpc_id = "${aws_vpc.foo.id}" 1787 } 1788 1789 resource "aws_instance" "foo" { 1790 // This is an AMI with RootDeviceName: "/dev/sda1"; actual root: "/dev/sda" 1791 ami = "ami-ef5b69df" 1792 instance_type = "t1.micro" 1793 subnet_id = "${aws_subnet.foo.id}" 1794 root_block_device { 1795 volume_size = 13 1796 } 1797 } 1798 ` 1799 1800 const testAccInstanceConfigForceNewAndTagsDrift = ` 1801 resource "aws_vpc" "foo" { 1802 cidr_block = "10.1.0.0/16" 1803 } 1804 1805 resource "aws_subnet" "foo" { 1806 cidr_block = "10.1.1.0/24" 1807 vpc_id = "${aws_vpc.foo.id}" 1808 } 1809 1810 resource "aws_instance" "foo" { 1811 ami = "ami-22b9a343" 1812 instance_type = "t2.nano" 1813 subnet_id = "${aws_subnet.foo.id}" 1814 } 1815 ` 1816 1817 const testAccInstanceConfigForceNewAndTagsDrift_Update = ` 1818 resource "aws_vpc" "foo" { 1819 cidr_block = "10.1.0.0/16" 1820 } 1821 1822 resource "aws_subnet" "foo" { 1823 cidr_block = "10.1.1.0/24" 1824 vpc_id = "${aws_vpc.foo.id}" 1825 } 1826 1827 resource "aws_instance" "foo" { 1828 ami = "ami-22b9a343" 1829 instance_type = "t2.micro" 1830 subnet_id = "${aws_subnet.foo.id}" 1831 } 1832 ` 1833 1834 const testAccInstanceConfigPrimaryNetworkInterface = ` 1835 resource "aws_vpc" "foo" { 1836 cidr_block = "172.16.0.0/16" 1837 tags { 1838 Name = "tf-instance-test" 1839 } 1840 } 1841 1842 resource "aws_subnet" "foo" { 1843 vpc_id = "${aws_vpc.foo.id}" 1844 cidr_block = "172.16.10.0/24" 1845 availability_zone = "us-west-2a" 1846 tags { 1847 Name = "tf-instance-test" 1848 } 1849 } 1850 1851 resource "aws_network_interface" "bar" { 1852 subnet_id = "${aws_subnet.foo.id}" 1853 private_ips = ["172.16.10.100"] 1854 tags { 1855 Name = "primary_network_interface" 1856 } 1857 } 1858 1859 resource "aws_instance" "foo" { 1860 ami = "ami-22b9a343" 1861 instance_type = "t2.micro" 1862 network_interface { 1863 network_interface_id = "${aws_network_interface.bar.id}" 1864 device_index = 0 1865 } 1866 } 1867 ` 1868 1869 const testAccInstanceConfigAddSecondaryNetworkInterfaceBefore = ` 1870 resource "aws_vpc" "foo" { 1871 cidr_block = "172.16.0.0/16" 1872 tags { 1873 Name = "tf-instance-test" 1874 } 1875 } 1876 1877 resource "aws_subnet" "foo" { 1878 vpc_id = "${aws_vpc.foo.id}" 1879 cidr_block = "172.16.10.0/24" 1880 availability_zone = "us-west-2a" 1881 tags { 1882 Name = "tf-instance-test" 1883 } 1884 } 1885 1886 resource "aws_network_interface" "primary" { 1887 subnet_id = "${aws_subnet.foo.id}" 1888 private_ips = ["172.16.10.100"] 1889 tags { 1890 Name = "primary_network_interface" 1891 } 1892 } 1893 1894 resource "aws_network_interface" "secondary" { 1895 subnet_id = "${aws_subnet.foo.id}" 1896 private_ips = ["172.16.10.101"] 1897 tags { 1898 Name = "secondary_network_interface" 1899 } 1900 } 1901 1902 resource "aws_instance" "foo" { 1903 ami = "ami-22b9a343" 1904 instance_type = "t2.micro" 1905 network_interface { 1906 network_interface_id = "${aws_network_interface.primary.id}" 1907 device_index = 0 1908 } 1909 } 1910 ` 1911 1912 const testAccInstanceConfigAddSecondaryNetworkInterfaceAfter = ` 1913 resource "aws_vpc" "foo" { 1914 cidr_block = "172.16.0.0/16" 1915 tags { 1916 Name = "tf-instance-test" 1917 } 1918 } 1919 1920 resource "aws_subnet" "foo" { 1921 vpc_id = "${aws_vpc.foo.id}" 1922 cidr_block = "172.16.10.0/24" 1923 availability_zone = "us-west-2a" 1924 tags { 1925 Name = "tf-instance-test" 1926 } 1927 } 1928 1929 resource "aws_network_interface" "primary" { 1930 subnet_id = "${aws_subnet.foo.id}" 1931 private_ips = ["172.16.10.100"] 1932 tags { 1933 Name = "primary_network_interface" 1934 } 1935 } 1936 1937 // Attach previously created network interface, observe no state diff on instance resource 1938 resource "aws_network_interface" "secondary" { 1939 subnet_id = "${aws_subnet.foo.id}" 1940 private_ips = ["172.16.10.101"] 1941 tags { 1942 Name = "secondary_network_interface" 1943 } 1944 attachment { 1945 instance = "${aws_instance.foo.id}" 1946 device_index = 1 1947 } 1948 } 1949 1950 resource "aws_instance" "foo" { 1951 ami = "ami-22b9a343" 1952 instance_type = "t2.micro" 1953 network_interface { 1954 network_interface_id = "${aws_network_interface.primary.id}" 1955 device_index = 0 1956 } 1957 } 1958 `