github.com/bradfeehan/terraform@v0.7.0-rc3.0.20170529055808-34b45c5ad841/builtin/providers/aws/resource_aws_instance_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "reflect" 6 "regexp" 7 "testing" 8 9 "github.com/aws/aws-sdk-go/aws" 10 "github.com/aws/aws-sdk-go/aws/awserr" 11 "github.com/aws/aws-sdk-go/service/ec2" 12 "github.com/hashicorp/terraform/helper/acctest" 13 "github.com/hashicorp/terraform/helper/resource" 14 "github.com/hashicorp/terraform/helper/schema" 15 "github.com/hashicorp/terraform/terraform" 16 ) 17 18 func TestAccAWSInstance_basic(t *testing.T) { 19 var v ec2.Instance 20 var vol *ec2.Volume 21 22 testCheck := func(*terraform.State) error { 23 if *v.Placement.AvailabilityZone != "us-west-2a" { 24 return fmt.Errorf("bad availability zone: %#v", *v.Placement.AvailabilityZone) 25 } 26 27 if len(v.SecurityGroups) == 0 { 28 return fmt.Errorf("no security groups: %#v", v.SecurityGroups) 29 } 30 if *v.SecurityGroups[0].GroupName != "tf_test_foo" { 31 return fmt.Errorf("no security groups: %#v", v.SecurityGroups) 32 } 33 34 return nil 35 } 36 37 resource.Test(t, resource.TestCase{ 38 PreCheck: func() { testAccPreCheck(t) }, 39 40 // We ignore security groups because even with EC2 classic 41 // we'll import as VPC security groups, which is fine. We verify 42 // VPC security group import in other tests 43 IDRefreshName: "aws_instance.foo", 44 IDRefreshIgnore: []string{"security_groups", "vpc_security_group_ids"}, 45 46 Providers: testAccProviders, 47 CheckDestroy: testAccCheckInstanceDestroy, 48 Steps: []resource.TestStep{ 49 // Create a volume to cover #1249 50 { 51 // Need a resource in this config so the provisioner will be available 52 Config: testAccInstanceConfig_pre, 53 Check: func(*terraform.State) error { 54 conn := testAccProvider.Meta().(*AWSClient).ec2conn 55 var err error 56 vol, err = conn.CreateVolume(&ec2.CreateVolumeInput{ 57 AvailabilityZone: aws.String("us-west-2a"), 58 Size: aws.Int64(int64(5)), 59 }) 60 return err 61 }, 62 }, 63 64 { 65 Config: testAccInstanceConfig, 66 Check: resource.ComposeTestCheckFunc( 67 testAccCheckInstanceExists( 68 "aws_instance.foo", &v), 69 testCheck, 70 resource.TestCheckResourceAttr( 71 "aws_instance.foo", 72 "user_data", 73 "3dc39dda39be1205215e776bad998da361a5955d"), 74 resource.TestCheckResourceAttr( 75 "aws_instance.foo", "ebs_block_device.#", "0"), 76 ), 77 }, 78 79 // We repeat the exact same test so that we can be sure 80 // that the user data hash stuff is working without generating 81 // an incorrect diff. 82 { 83 Config: testAccInstanceConfig, 84 Check: resource.ComposeTestCheckFunc( 85 testAccCheckInstanceExists( 86 "aws_instance.foo", &v), 87 testCheck, 88 resource.TestCheckResourceAttr( 89 "aws_instance.foo", 90 "user_data", 91 "3dc39dda39be1205215e776bad998da361a5955d"), 92 resource.TestCheckResourceAttr( 93 "aws_instance.foo", "ebs_block_device.#", "0"), 94 ), 95 }, 96 97 // Clean up volume created above 98 { 99 Config: testAccInstanceConfig, 100 Check: func(*terraform.State) error { 101 conn := testAccProvider.Meta().(*AWSClient).ec2conn 102 _, err := conn.DeleteVolume(&ec2.DeleteVolumeInput{VolumeId: vol.VolumeId}) 103 return err 104 }, 105 }, 106 }, 107 }) 108 } 109 110 func TestAccAWSInstance_GP2IopsDevice(t *testing.T) { 111 var v ec2.Instance 112 113 testCheck := func() resource.TestCheckFunc { 114 return func(*terraform.State) error { 115 116 // Map out the block devices by name, which should be unique. 117 blockDevices := make(map[string]*ec2.InstanceBlockDeviceMapping) 118 for _, blockDevice := range v.BlockDeviceMappings { 119 blockDevices[*blockDevice.DeviceName] = blockDevice 120 } 121 122 // Check if the root block device exists. 123 if _, ok := blockDevices["/dev/sda1"]; !ok { 124 return fmt.Errorf("block device doesn't exist: /dev/sda1") 125 } 126 127 return nil 128 } 129 } 130 131 resource.Test(t, resource.TestCase{ 132 PreCheck: func() { testAccPreCheck(t) }, 133 IDRefreshName: "aws_instance.foo", 134 IDRefreshIgnore: []string{ 135 "ephemeral_block_device", "user_data", "security_groups", "vpc_security_groups"}, 136 Providers: testAccProviders, 137 CheckDestroy: testAccCheckInstanceDestroy, 138 Steps: []resource.TestStep{ 139 { 140 Config: testAccInstanceGP2IopsDevice, 141 //Config: testAccInstanceConfigBlockDevices, 142 Check: resource.ComposeTestCheckFunc( 143 testAccCheckInstanceExists( 144 "aws_instance.foo", &v), 145 resource.TestCheckResourceAttr( 146 "aws_instance.foo", "root_block_device.#", "1"), 147 resource.TestCheckResourceAttr( 148 "aws_instance.foo", "root_block_device.0.volume_size", "11"), 149 resource.TestCheckResourceAttr( 150 "aws_instance.foo", "root_block_device.0.volume_type", "gp2"), 151 resource.TestCheckResourceAttr( 152 "aws_instance.foo", "root_block_device.0.iops", "100"), 153 testCheck(), 154 ), 155 }, 156 }, 157 }) 158 } 159 160 func TestAccAWSInstance_blockDevices(t *testing.T) { 161 var v ec2.Instance 162 163 testCheck := func() resource.TestCheckFunc { 164 return func(*terraform.State) error { 165 166 // Map out the block devices by name, which should be unique. 167 blockDevices := make(map[string]*ec2.InstanceBlockDeviceMapping) 168 for _, blockDevice := range v.BlockDeviceMappings { 169 blockDevices[*blockDevice.DeviceName] = blockDevice 170 } 171 172 // Check if the root block device exists. 173 if _, ok := blockDevices["/dev/sda1"]; !ok { 174 return fmt.Errorf("block device doesn't exist: /dev/sda1") 175 } 176 177 // Check if the secondary block device exists. 178 if _, ok := blockDevices["/dev/sdb"]; !ok { 179 return fmt.Errorf("block device doesn't exist: /dev/sdb") 180 } 181 182 // Check if the third block device exists. 183 if _, ok := blockDevices["/dev/sdc"]; !ok { 184 return fmt.Errorf("block device doesn't exist: /dev/sdc") 185 } 186 187 // Check if the encrypted block device exists 188 if _, ok := blockDevices["/dev/sdd"]; !ok { 189 return fmt.Errorf("block device doesn't exist: /dev/sdd") 190 } 191 192 return nil 193 } 194 } 195 196 resource.Test(t, resource.TestCase{ 197 PreCheck: func() { testAccPreCheck(t) }, 198 IDRefreshName: "aws_instance.foo", 199 IDRefreshIgnore: []string{ 200 "ephemeral_block_device", "security_groups", "vpc_security_groups"}, 201 Providers: testAccProviders, 202 CheckDestroy: testAccCheckInstanceDestroy, 203 Steps: []resource.TestStep{ 204 { 205 Config: testAccInstanceConfigBlockDevices, 206 Check: resource.ComposeTestCheckFunc( 207 testAccCheckInstanceExists( 208 "aws_instance.foo", &v), 209 resource.TestCheckResourceAttr( 210 "aws_instance.foo", "root_block_device.#", "1"), 211 resource.TestCheckResourceAttr( 212 "aws_instance.foo", "root_block_device.0.volume_size", "11"), 213 resource.TestCheckResourceAttr( 214 "aws_instance.foo", "root_block_device.0.volume_type", "gp2"), 215 resource.TestCheckResourceAttr( 216 "aws_instance.foo", "ebs_block_device.#", "3"), 217 resource.TestCheckResourceAttr( 218 "aws_instance.foo", "ebs_block_device.2576023345.device_name", "/dev/sdb"), 219 resource.TestCheckResourceAttr( 220 "aws_instance.foo", "ebs_block_device.2576023345.volume_size", "9"), 221 resource.TestCheckResourceAttr( 222 "aws_instance.foo", "ebs_block_device.2576023345.volume_type", "standard"), 223 resource.TestCheckResourceAttr( 224 "aws_instance.foo", "ebs_block_device.2554893574.device_name", "/dev/sdc"), 225 resource.TestCheckResourceAttr( 226 "aws_instance.foo", "ebs_block_device.2554893574.volume_size", "10"), 227 resource.TestCheckResourceAttr( 228 "aws_instance.foo", "ebs_block_device.2554893574.volume_type", "io1"), 229 resource.TestCheckResourceAttr( 230 "aws_instance.foo", "ebs_block_device.2554893574.iops", "100"), 231 resource.TestCheckResourceAttr( 232 "aws_instance.foo", "ebs_block_device.2634515331.device_name", "/dev/sdd"), 233 resource.TestCheckResourceAttr( 234 "aws_instance.foo", "ebs_block_device.2634515331.encrypted", "true"), 235 resource.TestCheckResourceAttr( 236 "aws_instance.foo", "ebs_block_device.2634515331.volume_size", "12"), 237 resource.TestCheckResourceAttr( 238 "aws_instance.foo", "ephemeral_block_device.#", "1"), 239 resource.TestCheckResourceAttr( 240 "aws_instance.foo", "ephemeral_block_device.1692014856.device_name", "/dev/sde"), 241 resource.TestCheckResourceAttr( 242 "aws_instance.foo", "ephemeral_block_device.1692014856.virtual_name", "ephemeral0"), 243 testCheck(), 244 ), 245 }, 246 }, 247 }) 248 } 249 250 func TestAccAWSInstance_rootInstanceStore(t *testing.T) { 251 var v ec2.Instance 252 253 resource.Test(t, resource.TestCase{ 254 PreCheck: func() { testAccPreCheck(t) }, 255 IDRefreshName: "aws_instance.foo", 256 Providers: testAccProviders, 257 CheckDestroy: testAccCheckInstanceDestroy, 258 Steps: []resource.TestStep{ 259 { 260 Config: ` 261 resource "aws_instance" "foo" { 262 # us-west-2 263 # Amazon Linux HVM Instance Store 64-bit (2016.09.0) 264 # https://aws.amazon.com/amazon-linux-ami 265 ami = "ami-44c36524" 266 267 # Only certain instance types support ephemeral root instance stores. 268 # http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/InstanceStorage.html 269 instance_type = "m3.medium" 270 }`, 271 Check: resource.ComposeTestCheckFunc( 272 testAccCheckInstanceExists( 273 "aws_instance.foo", &v), 274 resource.TestCheckResourceAttr( 275 "aws_instance.foo", "ami", "ami-44c36524"), 276 resource.TestCheckResourceAttr( 277 "aws_instance.foo", "ebs_block_device.#", "0"), 278 resource.TestCheckResourceAttr( 279 "aws_instance.foo", "ebs_optimized", "false"), 280 resource.TestCheckResourceAttr( 281 "aws_instance.foo", "instance_type", "m3.medium"), 282 resource.TestCheckResourceAttr( 283 "aws_instance.foo", "root_block_device.#", "0"), 284 ), 285 }, 286 }, 287 }) 288 } 289 290 func TestAcctABSInstance_noAMIEphemeralDevices(t *testing.T) { 291 var v ec2.Instance 292 293 testCheck := func() resource.TestCheckFunc { 294 return func(*terraform.State) error { 295 296 // Map out the block devices by name, which should be unique. 297 blockDevices := make(map[string]*ec2.InstanceBlockDeviceMapping) 298 for _, blockDevice := range v.BlockDeviceMappings { 299 blockDevices[*blockDevice.DeviceName] = blockDevice 300 } 301 302 // Check if the root block device exists. 303 if _, ok := blockDevices["/dev/sda1"]; !ok { 304 return fmt.Errorf("block device doesn't exist: /dev/sda1") 305 } 306 307 // Check if the secondary block not exists. 308 if _, ok := blockDevices["/dev/sdb"]; ok { 309 return fmt.Errorf("block device exist: /dev/sdb") 310 } 311 312 // Check if the third block device not exists. 313 if _, ok := blockDevices["/dev/sdc"]; ok { 314 return fmt.Errorf("block device exist: /dev/sdc") 315 } 316 return nil 317 } 318 } 319 320 resource.Test(t, resource.TestCase{ 321 PreCheck: func() { testAccPreCheck(t) }, 322 IDRefreshName: "aws_instance.foo", 323 IDRefreshIgnore: []string{ 324 "ephemeral_block_device", "security_groups", "vpc_security_groups"}, 325 Providers: testAccProviders, 326 CheckDestroy: testAccCheckInstanceDestroy, 327 Steps: []resource.TestStep{ 328 { 329 Config: ` 330 resource "aws_instance" "foo" { 331 # us-west-2 332 ami = "ami-01f05461" // This AMI (Ubuntu) contains two ephemerals 333 334 instance_type = "c3.large" 335 336 root_block_device { 337 volume_type = "gp2" 338 volume_size = 11 339 } 340 ephemeral_block_device { 341 device_name = "/dev/sdb" 342 no_device = true 343 } 344 ephemeral_block_device { 345 device_name = "/dev/sdc" 346 no_device = true 347 } 348 }`, 349 Check: resource.ComposeTestCheckFunc( 350 testAccCheckInstanceExists( 351 "aws_instance.foo", &v), 352 resource.TestCheckResourceAttr( 353 "aws_instance.foo", "ami", "ami-01f05461"), 354 resource.TestCheckResourceAttr( 355 "aws_instance.foo", "ebs_optimized", "false"), 356 resource.TestCheckResourceAttr( 357 "aws_instance.foo", "instance_type", "c3.large"), 358 resource.TestCheckResourceAttr( 359 "aws_instance.foo", "root_block_device.#", "1"), 360 resource.TestCheckResourceAttr( 361 "aws_instance.foo", "root_block_device.0.volume_size", "11"), 362 resource.TestCheckResourceAttr( 363 "aws_instance.foo", "root_block_device.0.volume_type", "gp2"), 364 resource.TestCheckResourceAttr( 365 "aws_instance.foo", "ebs_block_device.#", "0"), 366 resource.TestCheckResourceAttr( 367 "aws_instance.foo", "ephemeral_block_device.#", "2"), 368 resource.TestCheckResourceAttr( 369 "aws_instance.foo", "ephemeral_block_device.172787947.device_name", "/dev/sdb"), 370 resource.TestCheckResourceAttr( 371 "aws_instance.foo", "ephemeral_block_device.172787947.no_device", "true"), 372 resource.TestCheckResourceAttr( 373 "aws_instance.foo", "ephemeral_block_device.3336996981.device_name", "/dev/sdc"), 374 resource.TestCheckResourceAttr( 375 "aws_instance.foo", "ephemeral_block_device.3336996981.no_device", "true"), 376 testCheck(), 377 ), 378 }, 379 }, 380 }) 381 } 382 383 func TestAccAWSInstance_sourceDestCheck(t *testing.T) { 384 var v ec2.Instance 385 386 testCheck := func(enabled bool) resource.TestCheckFunc { 387 return func(*terraform.State) error { 388 if v.SourceDestCheck == nil { 389 return fmt.Errorf("bad source_dest_check: got nil") 390 } 391 if *v.SourceDestCheck != enabled { 392 return fmt.Errorf("bad source_dest_check: %#v", *v.SourceDestCheck) 393 } 394 395 return nil 396 } 397 } 398 399 resource.Test(t, resource.TestCase{ 400 PreCheck: func() { testAccPreCheck(t) }, 401 IDRefreshName: "aws_instance.foo", 402 Providers: testAccProviders, 403 CheckDestroy: testAccCheckInstanceDestroy, 404 Steps: []resource.TestStep{ 405 { 406 Config: testAccInstanceConfigSourceDestDisable, 407 Check: resource.ComposeTestCheckFunc( 408 testAccCheckInstanceExists("aws_instance.foo", &v), 409 testCheck(false), 410 ), 411 }, 412 413 { 414 Config: testAccInstanceConfigSourceDestEnable, 415 Check: resource.ComposeTestCheckFunc( 416 testAccCheckInstanceExists("aws_instance.foo", &v), 417 testCheck(true), 418 ), 419 }, 420 421 { 422 Config: testAccInstanceConfigSourceDestDisable, 423 Check: resource.ComposeTestCheckFunc( 424 testAccCheckInstanceExists("aws_instance.foo", &v), 425 testCheck(false), 426 ), 427 }, 428 }, 429 }) 430 } 431 432 func TestAccAWSInstance_disableApiTermination(t *testing.T) { 433 var v ec2.Instance 434 435 checkDisableApiTermination := func(expected bool) resource.TestCheckFunc { 436 return func(*terraform.State) error { 437 conn := testAccProvider.Meta().(*AWSClient).ec2conn 438 r, err := conn.DescribeInstanceAttribute(&ec2.DescribeInstanceAttributeInput{ 439 InstanceId: v.InstanceId, 440 Attribute: aws.String("disableApiTermination"), 441 }) 442 if err != nil { 443 return err 444 } 445 got := *r.DisableApiTermination.Value 446 if got != expected { 447 return fmt.Errorf("expected: %t, got: %t", expected, got) 448 } 449 return nil 450 } 451 } 452 453 resource.Test(t, resource.TestCase{ 454 PreCheck: func() { testAccPreCheck(t) }, 455 IDRefreshName: "aws_instance.foo", 456 Providers: testAccProviders, 457 CheckDestroy: testAccCheckInstanceDestroy, 458 Steps: []resource.TestStep{ 459 { 460 Config: testAccInstanceConfigDisableAPITermination(true), 461 Check: resource.ComposeTestCheckFunc( 462 testAccCheckInstanceExists("aws_instance.foo", &v), 463 checkDisableApiTermination(true), 464 ), 465 }, 466 467 { 468 Config: testAccInstanceConfigDisableAPITermination(false), 469 Check: resource.ComposeTestCheckFunc( 470 testAccCheckInstanceExists("aws_instance.foo", &v), 471 checkDisableApiTermination(false), 472 ), 473 }, 474 }, 475 }) 476 } 477 478 func TestAccAWSInstance_vpc(t *testing.T) { 479 var v ec2.Instance 480 481 resource.Test(t, resource.TestCase{ 482 PreCheck: func() { testAccPreCheck(t) }, 483 IDRefreshName: "aws_instance.foo", 484 IDRefreshIgnore: []string{"associate_public_ip_address"}, 485 Providers: testAccProviders, 486 CheckDestroy: testAccCheckInstanceDestroy, 487 Steps: []resource.TestStep{ 488 { 489 Config: testAccInstanceConfigVPC, 490 Check: resource.ComposeTestCheckFunc( 491 testAccCheckInstanceExists( 492 "aws_instance.foo", &v), 493 resource.TestCheckResourceAttr( 494 "aws_instance.foo", 495 "user_data", 496 "562a3e32810edf6ff09994f050f12e799452379d"), 497 ), 498 }, 499 }, 500 }) 501 } 502 503 func TestAccAWSInstance_ipv6_supportAddressCount(t *testing.T) { 504 var v ec2.Instance 505 506 resource.Test(t, resource.TestCase{ 507 PreCheck: func() { testAccPreCheck(t) }, 508 Providers: testAccProviders, 509 CheckDestroy: testAccCheckInstanceDestroy, 510 Steps: []resource.TestStep{ 511 { 512 Config: testAccInstanceConfigIpv6Support, 513 Check: resource.ComposeTestCheckFunc( 514 testAccCheckInstanceExists( 515 "aws_instance.foo", &v), 516 resource.TestCheckResourceAttr( 517 "aws_instance.foo", 518 "ipv6_address_count", 519 "1"), 520 ), 521 }, 522 }, 523 }) 524 } 525 526 func TestAccAWSInstance_ipv6AddressCountAndSingleAddressCausesError(t *testing.T) { 527 528 resource.Test(t, resource.TestCase{ 529 PreCheck: func() { testAccPreCheck(t) }, 530 Providers: testAccProviders, 531 CheckDestroy: testAccCheckInstanceDestroy, 532 Steps: []resource.TestStep{ 533 { 534 Config: testAccInstanceConfigIpv6ErrorConfig, 535 ExpectError: regexp.MustCompile("Only 1 of `ipv6_address_count` or `ipv6_addresses` can be specified"), 536 }, 537 }, 538 }) 539 } 540 541 func TestAccAWSInstance_multipleRegions(t *testing.T) { 542 var v ec2.Instance 543 544 // record the initialized providers so that we can use them to 545 // check for the instances in each region 546 var providers []*schema.Provider 547 providerFactories := map[string]terraform.ResourceProviderFactory{ 548 "aws": func() (terraform.ResourceProvider, error) { 549 p := Provider() 550 providers = append(providers, p.(*schema.Provider)) 551 return p, nil 552 }, 553 } 554 555 resource.Test(t, resource.TestCase{ 556 PreCheck: func() { testAccPreCheck(t) }, 557 ProviderFactories: providerFactories, 558 CheckDestroy: testAccCheckInstanceDestroyWithProviders(&providers), 559 Steps: []resource.TestStep{ 560 { 561 Config: testAccInstanceConfigMultipleRegions, 562 Check: resource.ComposeTestCheckFunc( 563 testAccCheckInstanceExistsWithProviders( 564 "aws_instance.foo", &v, &providers), 565 testAccCheckInstanceExistsWithProviders( 566 "aws_instance.bar", &v, &providers), 567 ), 568 }, 569 }, 570 }) 571 } 572 573 func TestAccAWSInstance_NetworkInstanceSecurityGroups(t *testing.T) { 574 var v ec2.Instance 575 576 resource.Test(t, resource.TestCase{ 577 PreCheck: func() { testAccPreCheck(t) }, 578 IDRefreshName: "aws_instance.foo_instance", 579 IDRefreshIgnore: []string{"associate_public_ip_address"}, 580 Providers: testAccProviders, 581 CheckDestroy: testAccCheckInstanceDestroy, 582 Steps: []resource.TestStep{ 583 { 584 Config: testAccInstanceNetworkInstanceSecurityGroups, 585 Check: resource.ComposeTestCheckFunc( 586 testAccCheckInstanceExists( 587 "aws_instance.foo_instance", &v), 588 ), 589 }, 590 }, 591 }) 592 } 593 594 func TestAccAWSInstance_NetworkInstanceVPCSecurityGroupIDs(t *testing.T) { 595 var v ec2.Instance 596 597 resource.Test(t, resource.TestCase{ 598 PreCheck: func() { testAccPreCheck(t) }, 599 IDRefreshName: "aws_instance.foo_instance", 600 Providers: testAccProviders, 601 CheckDestroy: testAccCheckInstanceDestroy, 602 Steps: []resource.TestStep{ 603 { 604 Config: testAccInstanceNetworkInstanceVPCSecurityGroupIDs, 605 Check: resource.ComposeTestCheckFunc( 606 testAccCheckInstanceExists( 607 "aws_instance.foo_instance", &v), 608 resource.TestCheckResourceAttr( 609 "aws_instance.foo_instance", "security_groups.#", "0"), 610 resource.TestCheckResourceAttr( 611 "aws_instance.foo_instance", "vpc_security_group_ids.#", "1"), 612 ), 613 }, 614 }, 615 }) 616 } 617 618 func TestAccAWSInstance_tags(t *testing.T) { 619 var v ec2.Instance 620 621 resource.Test(t, resource.TestCase{ 622 PreCheck: func() { testAccPreCheck(t) }, 623 Providers: testAccProviders, 624 CheckDestroy: testAccCheckInstanceDestroy, 625 Steps: []resource.TestStep{ 626 { 627 Config: testAccCheckInstanceConfigTags, 628 Check: resource.ComposeTestCheckFunc( 629 testAccCheckInstanceExists("aws_instance.foo", &v), 630 testAccCheckTags(&v.Tags, "foo", "bar"), 631 // Guard against regression of https://github.com/hashicorp/terraform/issues/914 632 testAccCheckTags(&v.Tags, "#", ""), 633 ), 634 }, 635 { 636 Config: testAccCheckInstanceConfigTagsUpdate, 637 Check: resource.ComposeTestCheckFunc( 638 testAccCheckInstanceExists("aws_instance.foo", &v), 639 testAccCheckTags(&v.Tags, "foo", ""), 640 testAccCheckTags(&v.Tags, "bar", "baz"), 641 ), 642 }, 643 }, 644 }) 645 } 646 647 func TestAccAWSInstance_volumeTags(t *testing.T) { 648 var v ec2.Instance 649 650 resource.Test(t, resource.TestCase{ 651 PreCheck: func() { testAccPreCheck(t) }, 652 Providers: testAccProviders, 653 CheckDestroy: testAccCheckInstanceDestroy, 654 Steps: []resource.TestStep{ 655 { 656 Config: testAccCheckInstanceConfigNoVolumeTags, 657 Check: resource.ComposeTestCheckFunc( 658 testAccCheckInstanceExists("aws_instance.foo", &v), 659 resource.TestCheckNoResourceAttr( 660 "aws_instance.foo", "volume_tags"), 661 ), 662 }, 663 { 664 Config: testAccCheckInstanceConfigWithVolumeTags, 665 Check: resource.ComposeTestCheckFunc( 666 testAccCheckInstanceExists("aws_instance.foo", &v), 667 resource.TestCheckResourceAttr( 668 "aws_instance.foo", "volume_tags.%", "1"), 669 resource.TestCheckResourceAttr( 670 "aws_instance.foo", "volume_tags.Name", "acceptance-test-volume-tag"), 671 ), 672 }, 673 { 674 Config: testAccCheckInstanceConfigWithVolumeTagsUpdate, 675 Check: resource.ComposeTestCheckFunc( 676 testAccCheckInstanceExists("aws_instance.foo", &v), 677 resource.TestCheckResourceAttr( 678 "aws_instance.foo", "volume_tags.%", "2"), 679 resource.TestCheckResourceAttr( 680 "aws_instance.foo", "volume_tags.Name", "acceptance-test-volume-tag"), 681 resource.TestCheckResourceAttr( 682 "aws_instance.foo", "volume_tags.Environment", "dev"), 683 ), 684 }, 685 { 686 Config: testAccCheckInstanceConfigNoVolumeTags, 687 Check: resource.ComposeTestCheckFunc( 688 testAccCheckInstanceExists("aws_instance.foo", &v), 689 resource.TestCheckNoResourceAttr( 690 "aws_instance.foo", "volume_tags"), 691 ), 692 }, 693 }, 694 }) 695 } 696 697 func TestAccAWSInstance_volumeTagsComputed(t *testing.T) { 698 var v ec2.Instance 699 700 resource.Test(t, resource.TestCase{ 701 PreCheck: func() { testAccPreCheck(t) }, 702 Providers: testAccProviders, 703 CheckDestroy: testAccCheckInstanceDestroy, 704 Steps: []resource.TestStep{ 705 { 706 Config: testAccCheckInstanceConfigWithAttachedVolume, 707 Check: resource.ComposeTestCheckFunc( 708 testAccCheckInstanceExists("aws_instance.foo", &v), 709 ), 710 ExpectNonEmptyPlan: false, 711 }, 712 }, 713 }) 714 } 715 716 func TestAccAWSInstance_instanceProfileChange(t *testing.T) { 717 var v ec2.Instance 718 rName := acctest.RandString(5) 719 720 testCheckInstanceProfile := func() resource.TestCheckFunc { 721 return func(*terraform.State) error { 722 if v.IamInstanceProfile == nil { 723 return fmt.Errorf("Instance Profile is nil - we expected an InstanceProfile associated with the Instance") 724 } 725 726 return nil 727 } 728 } 729 730 resource.Test(t, resource.TestCase{ 731 PreCheck: func() { testAccPreCheck(t) }, 732 IDRefreshName: "aws_instance.foo", 733 Providers: testAccProviders, 734 CheckDestroy: testAccCheckInstanceDestroy, 735 Steps: []resource.TestStep{ 736 { 737 Config: testAccInstanceConfigWithoutInstanceProfile(rName), 738 Check: resource.ComposeTestCheckFunc( 739 testAccCheckInstanceExists("aws_instance.foo", &v), 740 ), 741 }, 742 { 743 Config: testAccInstanceConfigWithInstanceProfile(rName), 744 Check: resource.ComposeTestCheckFunc( 745 testAccCheckInstanceExists("aws_instance.foo", &v), 746 testCheckInstanceProfile(), 747 ), 748 }, 749 }, 750 }) 751 } 752 753 func TestAccAWSInstance_withIamInstanceProfile(t *testing.T) { 754 var v ec2.Instance 755 rName := acctest.RandString(5) 756 757 testCheckInstanceProfile := func() resource.TestCheckFunc { 758 return func(*terraform.State) error { 759 if v.IamInstanceProfile == nil { 760 return fmt.Errorf("Instance Profile is nil - we expected an InstanceProfile associated with the Instance") 761 } 762 763 return nil 764 } 765 } 766 767 resource.Test(t, resource.TestCase{ 768 PreCheck: func() { testAccPreCheck(t) }, 769 IDRefreshName: "aws_instance.foo", 770 Providers: testAccProviders, 771 CheckDestroy: testAccCheckInstanceDestroy, 772 Steps: []resource.TestStep{ 773 { 774 Config: testAccInstanceConfigWithInstanceProfile(rName), 775 Check: resource.ComposeTestCheckFunc( 776 testAccCheckInstanceExists("aws_instance.foo", &v), 777 testCheckInstanceProfile(), 778 ), 779 }, 780 }, 781 }) 782 } 783 784 func TestAccAWSInstance_privateIP(t *testing.T) { 785 var v ec2.Instance 786 787 testCheckPrivateIP := func() resource.TestCheckFunc { 788 return func(*terraform.State) error { 789 if *v.PrivateIpAddress != "10.1.1.42" { 790 return fmt.Errorf("bad private IP: %s", *v.PrivateIpAddress) 791 } 792 793 return nil 794 } 795 } 796 797 resource.Test(t, resource.TestCase{ 798 PreCheck: func() { testAccPreCheck(t) }, 799 IDRefreshName: "aws_instance.foo", 800 Providers: testAccProviders, 801 CheckDestroy: testAccCheckInstanceDestroy, 802 Steps: []resource.TestStep{ 803 { 804 Config: testAccInstanceConfigPrivateIP, 805 Check: resource.ComposeTestCheckFunc( 806 testAccCheckInstanceExists("aws_instance.foo", &v), 807 testCheckPrivateIP(), 808 ), 809 }, 810 }, 811 }) 812 } 813 814 func TestAccAWSInstance_associatePublicIPAndPrivateIP(t *testing.T) { 815 var v ec2.Instance 816 817 testCheckPrivateIP := func() resource.TestCheckFunc { 818 return func(*terraform.State) error { 819 if *v.PrivateIpAddress != "10.1.1.42" { 820 return fmt.Errorf("bad private IP: %s", *v.PrivateIpAddress) 821 } 822 823 return nil 824 } 825 } 826 827 resource.Test(t, resource.TestCase{ 828 PreCheck: func() { testAccPreCheck(t) }, 829 IDRefreshName: "aws_instance.foo", 830 IDRefreshIgnore: []string{"associate_public_ip_address"}, 831 Providers: testAccProviders, 832 CheckDestroy: testAccCheckInstanceDestroy, 833 Steps: []resource.TestStep{ 834 { 835 Config: testAccInstanceConfigAssociatePublicIPAndPrivateIP, 836 Check: resource.ComposeTestCheckFunc( 837 testAccCheckInstanceExists("aws_instance.foo", &v), 838 testCheckPrivateIP(), 839 ), 840 }, 841 }, 842 }) 843 } 844 845 // Guard against regression with KeyPairs 846 // https://github.com/hashicorp/terraform/issues/2302 847 func TestAccAWSInstance_keyPairCheck(t *testing.T) { 848 var v ec2.Instance 849 850 testCheckKeyPair := func(keyName string) resource.TestCheckFunc { 851 return func(*terraform.State) error { 852 if v.KeyName == nil { 853 return fmt.Errorf("No Key Pair found, expected(%s)", keyName) 854 } 855 if v.KeyName != nil && *v.KeyName != keyName { 856 return fmt.Errorf("Bad key name, expected (%s), got (%s)", keyName, *v.KeyName) 857 } 858 859 return nil 860 } 861 } 862 863 keyPairName := fmt.Sprintf("tf-acc-test-%s", acctest.RandString(5)) 864 865 resource.Test(t, resource.TestCase{ 866 PreCheck: func() { testAccPreCheck(t) }, 867 IDRefreshName: "aws_instance.foo", 868 IDRefreshIgnore: []string{"source_dest_check"}, 869 Providers: testAccProviders, 870 CheckDestroy: testAccCheckInstanceDestroy, 871 Steps: []resource.TestStep{ 872 { 873 Config: testAccInstanceConfigKeyPair(keyPairName), 874 Check: resource.ComposeTestCheckFunc( 875 testAccCheckInstanceExists("aws_instance.foo", &v), 876 testCheckKeyPair(keyPairName), 877 ), 878 }, 879 }, 880 }) 881 } 882 883 func TestAccAWSInstance_rootBlockDeviceMismatch(t *testing.T) { 884 var v ec2.Instance 885 886 resource.Test(t, resource.TestCase{ 887 PreCheck: func() { testAccPreCheck(t) }, 888 Providers: testAccProviders, 889 CheckDestroy: testAccCheckInstanceDestroy, 890 Steps: []resource.TestStep{ 891 { 892 Config: testAccInstanceConfigRootBlockDeviceMismatch, 893 Check: resource.ComposeTestCheckFunc( 894 testAccCheckInstanceExists("aws_instance.foo", &v), 895 resource.TestCheckResourceAttr( 896 "aws_instance.foo", "root_block_device.0.volume_size", "13"), 897 ), 898 }, 899 }, 900 }) 901 } 902 903 // This test reproduces the bug here: 904 // https://github.com/hashicorp/terraform/issues/1752 905 // 906 // I wish there were a way to exercise resources built with helper.Schema in a 907 // unit context, in which case this test could be moved there, but for now this 908 // will cover the bugfix. 909 // 910 // The following triggers "diffs didn't match during apply" without the fix in to 911 // set NewRemoved on the .# field when it changes to 0. 912 func TestAccAWSInstance_forceNewAndTagsDrift(t *testing.T) { 913 var v ec2.Instance 914 915 resource.Test(t, resource.TestCase{ 916 PreCheck: func() { testAccPreCheck(t) }, 917 IDRefreshName: "aws_instance.foo", 918 Providers: testAccProviders, 919 CheckDestroy: testAccCheckInstanceDestroy, 920 Steps: []resource.TestStep{ 921 { 922 Config: testAccInstanceConfigForceNewAndTagsDrift, 923 Check: resource.ComposeTestCheckFunc( 924 testAccCheckInstanceExists("aws_instance.foo", &v), 925 driftTags(&v), 926 ), 927 ExpectNonEmptyPlan: true, 928 }, 929 { 930 Config: testAccInstanceConfigForceNewAndTagsDrift_Update, 931 Check: resource.ComposeTestCheckFunc( 932 testAccCheckInstanceExists("aws_instance.foo", &v), 933 ), 934 }, 935 }, 936 }) 937 } 938 939 func TestAccAWSInstance_changeInstanceType(t *testing.T) { 940 var before ec2.Instance 941 var after ec2.Instance 942 943 resource.Test(t, resource.TestCase{ 944 PreCheck: func() { testAccPreCheck(t) }, 945 Providers: testAccProviders, 946 CheckDestroy: testAccCheckInstanceDestroy, 947 Steps: []resource.TestStep{ 948 { 949 Config: testAccInstanceConfigWithSmallInstanceType, 950 Check: resource.ComposeTestCheckFunc( 951 testAccCheckInstanceExists("aws_instance.foo", &before), 952 ), 953 }, 954 { 955 Config: testAccInstanceConfigUpdateInstanceType, 956 Check: resource.ComposeTestCheckFunc( 957 testAccCheckInstanceExists("aws_instance.foo", &after), 958 testAccCheckInstanceNotRecreated( 959 t, &before, &after), 960 ), 961 }, 962 }, 963 }) 964 } 965 966 func TestAccAWSInstance_primaryNetworkInterface(t *testing.T) { 967 var instance ec2.Instance 968 var ini ec2.NetworkInterface 969 970 resource.Test(t, resource.TestCase{ 971 PreCheck: func() { testAccPreCheck(t) }, 972 Providers: testAccProviders, 973 CheckDestroy: testAccCheckInstanceDestroy, 974 Steps: []resource.TestStep{ 975 { 976 Config: testAccInstanceConfigPrimaryNetworkInterface, 977 Check: resource.ComposeTestCheckFunc( 978 testAccCheckInstanceExists("aws_instance.foo", &instance), 979 testAccCheckAWSENIExists("aws_network_interface.bar", &ini), 980 resource.TestCheckResourceAttr("aws_instance.foo", "network_interface.#", "1"), 981 ), 982 }, 983 }, 984 }) 985 } 986 987 func TestAccAWSInstance_primaryNetworkInterfaceSourceDestCheck(t *testing.T) { 988 var instance ec2.Instance 989 var ini ec2.NetworkInterface 990 991 resource.Test(t, resource.TestCase{ 992 PreCheck: func() { testAccPreCheck(t) }, 993 Providers: testAccProviders, 994 CheckDestroy: testAccCheckInstanceDestroy, 995 Steps: []resource.TestStep{ 996 { 997 Config: testAccInstanceConfigPrimaryNetworkInterfaceSourceDestCheck, 998 Check: resource.ComposeTestCheckFunc( 999 testAccCheckInstanceExists("aws_instance.foo", &instance), 1000 testAccCheckAWSENIExists("aws_network_interface.bar", &ini), 1001 resource.TestCheckResourceAttr("aws_instance.foo", "source_dest_check", "false"), 1002 ), 1003 }, 1004 }, 1005 }) 1006 } 1007 1008 func TestAccAWSInstance_addSecondaryInterface(t *testing.T) { 1009 var before ec2.Instance 1010 var after ec2.Instance 1011 var iniPrimary ec2.NetworkInterface 1012 var iniSecondary ec2.NetworkInterface 1013 1014 resource.Test(t, resource.TestCase{ 1015 PreCheck: func() { testAccPreCheck(t) }, 1016 Providers: testAccProviders, 1017 CheckDestroy: testAccCheckInstanceDestroy, 1018 Steps: []resource.TestStep{ 1019 { 1020 Config: testAccInstanceConfigAddSecondaryNetworkInterfaceBefore, 1021 Check: resource.ComposeTestCheckFunc( 1022 testAccCheckInstanceExists("aws_instance.foo", &before), 1023 testAccCheckAWSENIExists("aws_network_interface.primary", &iniPrimary), 1024 resource.TestCheckResourceAttr("aws_instance.foo", "network_interface.#", "1"), 1025 ), 1026 }, 1027 { 1028 Config: testAccInstanceConfigAddSecondaryNetworkInterfaceAfter, 1029 Check: resource.ComposeTestCheckFunc( 1030 testAccCheckInstanceExists("aws_instance.foo", &after), 1031 testAccCheckAWSENIExists("aws_network_interface.secondary", &iniSecondary), 1032 resource.TestCheckResourceAttr("aws_instance.foo", "network_interface.#", "1"), 1033 ), 1034 }, 1035 }, 1036 }) 1037 } 1038 1039 // https://github.com/hashicorp/terraform/issues/3205 1040 func TestAccAWSInstance_addSecurityGroupNetworkInterface(t *testing.T) { 1041 var before ec2.Instance 1042 var after ec2.Instance 1043 1044 resource.Test(t, resource.TestCase{ 1045 PreCheck: func() { testAccPreCheck(t) }, 1046 Providers: testAccProviders, 1047 CheckDestroy: testAccCheckInstanceDestroy, 1048 Steps: []resource.TestStep{ 1049 { 1050 Config: testAccInstanceConfigAddSecurityGroupBefore, 1051 Check: resource.ComposeTestCheckFunc( 1052 testAccCheckInstanceExists("aws_instance.foo", &before), 1053 resource.TestCheckResourceAttr("aws_instance.foo", "vpc_security_group_ids.#", "1"), 1054 ), 1055 }, 1056 { 1057 Config: testAccInstanceConfigAddSecurityGroupAfter, 1058 Check: resource.ComposeTestCheckFunc( 1059 testAccCheckInstanceExists("aws_instance.foo", &after), 1060 resource.TestCheckResourceAttr("aws_instance.foo", "vpc_security_group_ids.#", "2"), 1061 ), 1062 }, 1063 }, 1064 }) 1065 } 1066 1067 func testAccCheckInstanceNotRecreated(t *testing.T, 1068 before, after *ec2.Instance) resource.TestCheckFunc { 1069 return func(s *terraform.State) error { 1070 if *before.InstanceId != *after.InstanceId { 1071 t.Fatalf("AWS Instance IDs have changed. Before %s. After %s", *before.InstanceId, *after.InstanceId) 1072 } 1073 return nil 1074 } 1075 } 1076 1077 func testAccCheckInstanceDestroy(s *terraform.State) error { 1078 return testAccCheckInstanceDestroyWithProvider(s, testAccProvider) 1079 } 1080 1081 func testAccCheckInstanceDestroyWithProviders(providers *[]*schema.Provider) resource.TestCheckFunc { 1082 return func(s *terraform.State) error { 1083 for _, provider := range *providers { 1084 if provider.Meta() == nil { 1085 continue 1086 } 1087 if err := testAccCheckInstanceDestroyWithProvider(s, provider); err != nil { 1088 return err 1089 } 1090 } 1091 return nil 1092 } 1093 } 1094 1095 func testAccCheckInstanceDestroyWithProvider(s *terraform.State, provider *schema.Provider) error { 1096 conn := provider.Meta().(*AWSClient).ec2conn 1097 1098 for _, rs := range s.RootModule().Resources { 1099 if rs.Type != "aws_instance" { 1100 continue 1101 } 1102 1103 // Try to find the resource 1104 resp, err := conn.DescribeInstances(&ec2.DescribeInstancesInput{ 1105 InstanceIds: []*string{aws.String(rs.Primary.ID)}, 1106 }) 1107 if err == nil { 1108 for _, r := range resp.Reservations { 1109 for _, i := range r.Instances { 1110 if i.State != nil && *i.State.Name != "terminated" { 1111 return fmt.Errorf("Found unterminated instance: %s", i) 1112 } 1113 } 1114 } 1115 } 1116 1117 // Verify the error is what we want 1118 if ae, ok := err.(awserr.Error); ok && ae.Code() == "InvalidInstanceID.NotFound" { 1119 continue 1120 } 1121 1122 return err 1123 } 1124 1125 return nil 1126 } 1127 1128 func testAccCheckInstanceExists(n string, i *ec2.Instance) resource.TestCheckFunc { 1129 providers := []*schema.Provider{testAccProvider} 1130 return testAccCheckInstanceExistsWithProviders(n, i, &providers) 1131 } 1132 1133 func testAccCheckInstanceExistsWithProviders(n string, i *ec2.Instance, providers *[]*schema.Provider) resource.TestCheckFunc { 1134 return func(s *terraform.State) error { 1135 rs, ok := s.RootModule().Resources[n] 1136 if !ok { 1137 return fmt.Errorf("Not found: %s", n) 1138 } 1139 1140 if rs.Primary.ID == "" { 1141 return fmt.Errorf("No ID is set") 1142 } 1143 for _, provider := range *providers { 1144 // Ignore if Meta is empty, this can happen for validation providers 1145 if provider.Meta() == nil { 1146 continue 1147 } 1148 1149 conn := provider.Meta().(*AWSClient).ec2conn 1150 resp, err := conn.DescribeInstances(&ec2.DescribeInstancesInput{ 1151 InstanceIds: []*string{aws.String(rs.Primary.ID)}, 1152 }) 1153 if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "InvalidInstanceID.NotFound" { 1154 continue 1155 } 1156 if err != nil { 1157 return err 1158 } 1159 1160 if len(resp.Reservations) > 0 { 1161 *i = *resp.Reservations[0].Instances[0] 1162 return nil 1163 } 1164 } 1165 1166 return fmt.Errorf("Instance not found") 1167 } 1168 } 1169 1170 func TestInstanceTenancySchema(t *testing.T) { 1171 actualSchema := resourceAwsInstance().Schema["tenancy"] 1172 expectedSchema := &schema.Schema{ 1173 Type: schema.TypeString, 1174 Optional: true, 1175 Computed: true, 1176 ForceNew: true, 1177 } 1178 if !reflect.DeepEqual(actualSchema, expectedSchema) { 1179 t.Fatalf( 1180 "Got:\n\n%#v\n\nExpected:\n\n%#v\n", 1181 actualSchema, 1182 expectedSchema) 1183 } 1184 } 1185 1186 func driftTags(instance *ec2.Instance) resource.TestCheckFunc { 1187 return func(s *terraform.State) error { 1188 conn := testAccProvider.Meta().(*AWSClient).ec2conn 1189 _, err := conn.CreateTags(&ec2.CreateTagsInput{ 1190 Resources: []*string{instance.InstanceId}, 1191 Tags: []*ec2.Tag{ 1192 { 1193 Key: aws.String("Drift"), 1194 Value: aws.String("Happens"), 1195 }, 1196 }, 1197 }) 1198 return err 1199 } 1200 } 1201 1202 const testAccInstanceConfig_pre = ` 1203 resource "aws_security_group" "tf_test_foo" { 1204 name = "tf_test_foo" 1205 description = "foo" 1206 1207 ingress { 1208 protocol = "icmp" 1209 from_port = -1 1210 to_port = -1 1211 cidr_blocks = ["0.0.0.0/0"] 1212 } 1213 } 1214 ` 1215 1216 const testAccInstanceConfig = ` 1217 resource "aws_security_group" "tf_test_foo" { 1218 name = "tf_test_foo" 1219 description = "foo" 1220 1221 ingress { 1222 protocol = "icmp" 1223 from_port = -1 1224 to_port = -1 1225 cidr_blocks = ["0.0.0.0/0"] 1226 } 1227 } 1228 1229 resource "aws_instance" "foo" { 1230 # us-west-2 1231 ami = "ami-4fccb37f" 1232 availability_zone = "us-west-2a" 1233 1234 instance_type = "m1.small" 1235 security_groups = ["${aws_security_group.tf_test_foo.name}"] 1236 user_data = "foo:-with-character's" 1237 } 1238 ` 1239 1240 const testAccInstanceConfigWithSmallInstanceType = ` 1241 resource "aws_instance" "foo" { 1242 # us-west-2 1243 ami = "ami-55a7ea65" 1244 availability_zone = "us-west-2a" 1245 1246 instance_type = "m3.medium" 1247 1248 tags { 1249 Name = "tf-acctest" 1250 } 1251 } 1252 ` 1253 1254 const testAccInstanceConfigUpdateInstanceType = ` 1255 resource "aws_instance" "foo" { 1256 # us-west-2 1257 ami = "ami-55a7ea65" 1258 availability_zone = "us-west-2a" 1259 1260 instance_type = "m3.large" 1261 1262 tags { 1263 Name = "tf-acctest" 1264 } 1265 } 1266 ` 1267 1268 const testAccInstanceGP2IopsDevice = ` 1269 resource "aws_instance" "foo" { 1270 # us-west-2 1271 ami = "ami-55a7ea65" 1272 1273 # In order to attach an encrypted volume to an instance you need to have an 1274 # m3.medium or larger. See "Supported Instance Types" in: 1275 # http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EBSEncryption.html 1276 instance_type = "m3.medium" 1277 1278 root_block_device { 1279 volume_type = "gp2" 1280 volume_size = 11 1281 } 1282 } 1283 ` 1284 1285 const testAccInstanceConfigBlockDevices = ` 1286 resource "aws_instance" "foo" { 1287 # us-west-2 1288 ami = "ami-55a7ea65" 1289 1290 # In order to attach an encrypted volume to an instance you need to have an 1291 # m3.medium or larger. See "Supported Instance Types" in: 1292 # http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EBSEncryption.html 1293 instance_type = "m3.medium" 1294 1295 root_block_device { 1296 volume_type = "gp2" 1297 volume_size = 11 1298 } 1299 ebs_block_device { 1300 device_name = "/dev/sdb" 1301 volume_size = 9 1302 } 1303 ebs_block_device { 1304 device_name = "/dev/sdc" 1305 volume_size = 10 1306 volume_type = "io1" 1307 iops = 100 1308 } 1309 1310 # Encrypted ebs block device 1311 ebs_block_device { 1312 device_name = "/dev/sdd" 1313 volume_size = 12 1314 encrypted = true 1315 } 1316 1317 ephemeral_block_device { 1318 device_name = "/dev/sde" 1319 virtual_name = "ephemeral0" 1320 } 1321 } 1322 ` 1323 1324 const testAccInstanceConfigSourceDestEnable = ` 1325 resource "aws_vpc" "foo" { 1326 cidr_block = "10.1.0.0/16" 1327 tags { 1328 Name = "testAccInstanceConfigSourceDestEnable" 1329 } 1330 } 1331 1332 resource "aws_subnet" "foo" { 1333 cidr_block = "10.1.1.0/24" 1334 vpc_id = "${aws_vpc.foo.id}" 1335 } 1336 1337 resource "aws_instance" "foo" { 1338 # us-west-2 1339 ami = "ami-4fccb37f" 1340 instance_type = "m1.small" 1341 subnet_id = "${aws_subnet.foo.id}" 1342 } 1343 ` 1344 1345 const testAccInstanceConfigSourceDestDisable = ` 1346 resource "aws_vpc" "foo" { 1347 cidr_block = "10.1.0.0/16" 1348 tags { 1349 Name = "testAccInstanceConfigSourceDestDisable" 1350 } 1351 } 1352 1353 resource "aws_subnet" "foo" { 1354 cidr_block = "10.1.1.0/24" 1355 vpc_id = "${aws_vpc.foo.id}" 1356 } 1357 1358 resource "aws_instance" "foo" { 1359 # us-west-2 1360 ami = "ami-4fccb37f" 1361 instance_type = "m1.small" 1362 subnet_id = "${aws_subnet.foo.id}" 1363 source_dest_check = false 1364 } 1365 ` 1366 1367 func testAccInstanceConfigDisableAPITermination(val bool) string { 1368 return fmt.Sprintf(` 1369 resource "aws_vpc" "foo" { 1370 cidr_block = "10.1.0.0/16" 1371 tags { 1372 Name = "testAccInstanceConfigDisableAPITermination" 1373 } 1374 } 1375 1376 resource "aws_subnet" "foo" { 1377 cidr_block = "10.1.1.0/24" 1378 vpc_id = "${aws_vpc.foo.id}" 1379 } 1380 1381 resource "aws_instance" "foo" { 1382 # us-west-2 1383 ami = "ami-4fccb37f" 1384 instance_type = "m1.small" 1385 subnet_id = "${aws_subnet.foo.id}" 1386 disable_api_termination = %t 1387 } 1388 `, val) 1389 } 1390 1391 const testAccInstanceConfigVPC = ` 1392 resource "aws_vpc" "foo" { 1393 cidr_block = "10.1.0.0/16" 1394 tags { 1395 Name = "testAccInstanceConfigVPC" 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" { 1405 # us-west-2 1406 ami = "ami-4fccb37f" 1407 instance_type = "m1.small" 1408 subnet_id = "${aws_subnet.foo.id}" 1409 associate_public_ip_address = true 1410 tenancy = "dedicated" 1411 # pre-encoded base64 data 1412 user_data = "3dc39dda39be1205215e776bad998da361a5955d" 1413 } 1414 ` 1415 1416 const testAccInstanceConfigIpv6ErrorConfig = ` 1417 resource "aws_vpc" "foo" { 1418 cidr_block = "10.1.0.0/16" 1419 assign_generated_ipv6_cidr_block = true 1420 tags { 1421 Name = "tf-ipv6-instance-acc-test" 1422 } 1423 } 1424 1425 resource "aws_subnet" "foo" { 1426 cidr_block = "10.1.1.0/24" 1427 vpc_id = "${aws_vpc.foo.id}" 1428 ipv6_cidr_block = "${cidrsubnet(aws_vpc.foo.ipv6_cidr_block, 8, 1)}" 1429 tags { 1430 Name = "tf-ipv6-instance-acc-test" 1431 } 1432 } 1433 1434 resource "aws_instance" "foo" { 1435 # us-west-2 1436 ami = "ami-c5eabbf5" 1437 instance_type = "t2.micro" 1438 subnet_id = "${aws_subnet.foo.id}" 1439 ipv6_addresses = ["2600:1f14:bb2:e501::10"] 1440 ipv6_address_count = 1 1441 tags { 1442 Name = "tf-ipv6-instance-acc-test" 1443 } 1444 } 1445 ` 1446 1447 const testAccInstanceConfigIpv6Support = ` 1448 resource "aws_vpc" "foo" { 1449 cidr_block = "10.1.0.0/16" 1450 assign_generated_ipv6_cidr_block = true 1451 tags { 1452 Name = "tf-ipv6-instance-acc-test" 1453 } 1454 } 1455 1456 resource "aws_subnet" "foo" { 1457 cidr_block = "10.1.1.0/24" 1458 vpc_id = "${aws_vpc.foo.id}" 1459 ipv6_cidr_block = "${cidrsubnet(aws_vpc.foo.ipv6_cidr_block, 8, 1)}" 1460 tags { 1461 Name = "tf-ipv6-instance-acc-test" 1462 } 1463 } 1464 1465 resource "aws_instance" "foo" { 1466 # us-west-2 1467 ami = "ami-c5eabbf5" 1468 instance_type = "t2.micro" 1469 subnet_id = "${aws_subnet.foo.id}" 1470 1471 ipv6_address_count = 1 1472 tags { 1473 Name = "tf-ipv6-instance-acc-test" 1474 } 1475 } 1476 ` 1477 1478 const testAccInstanceConfigMultipleRegions = ` 1479 provider "aws" { 1480 alias = "west" 1481 region = "us-west-2" 1482 } 1483 1484 provider "aws" { 1485 alias = "east" 1486 region = "us-east-1" 1487 } 1488 1489 resource "aws_instance" "foo" { 1490 # us-west-2 1491 provider = "aws.west" 1492 ami = "ami-4fccb37f" 1493 instance_type = "m1.small" 1494 } 1495 1496 resource "aws_instance" "bar" { 1497 # us-east-1 1498 provider = "aws.east" 1499 ami = "ami-8c6ea9e4" 1500 instance_type = "m1.small" 1501 } 1502 ` 1503 1504 const testAccCheckInstanceConfigTags = ` 1505 resource "aws_instance" "foo" { 1506 ami = "ami-4fccb37f" 1507 instance_type = "m1.small" 1508 tags { 1509 foo = "bar" 1510 } 1511 } 1512 ` 1513 1514 const testAccCheckInstanceConfigWithAttachedVolume = ` 1515 data "aws_ami" "debian_jessie_latest" { 1516 most_recent = true 1517 1518 filter { 1519 name = "name" 1520 values = ["debian-jessie-*"] 1521 } 1522 1523 filter { 1524 name = "virtualization-type" 1525 values = ["hvm"] 1526 } 1527 1528 filter { 1529 name = "architecture" 1530 values = ["x86_64"] 1531 } 1532 1533 filter { 1534 name = "root-device-type" 1535 values = ["ebs"] 1536 } 1537 1538 owners = ["379101102735"] # Debian 1539 } 1540 1541 resource "aws_instance" "foo" { 1542 ami = "${data.aws_ami.debian_jessie_latest.id}" 1543 associate_public_ip_address = true 1544 count = 1 1545 instance_type = "t2.medium" 1546 1547 root_block_device { 1548 volume_size = "10" 1549 volume_type = "standard" 1550 delete_on_termination = true 1551 } 1552 1553 tags { 1554 Name = "test-terraform" 1555 } 1556 } 1557 1558 resource "aws_ebs_volume" "test" { 1559 depends_on = ["aws_instance.foo"] 1560 availability_zone = "${aws_instance.foo.availability_zone}" 1561 type = "gp2" 1562 size = "10" 1563 1564 tags { 1565 Name = "test-terraform" 1566 } 1567 } 1568 1569 resource "aws_volume_attachment" "test" { 1570 depends_on = ["aws_ebs_volume.test"] 1571 device_name = "/dev/xvdg" 1572 volume_id = "${aws_ebs_volume.test.id}" 1573 instance_id = "${aws_instance.foo.id}" 1574 } 1575 ` 1576 1577 const testAccCheckInstanceConfigNoVolumeTags = ` 1578 resource "aws_instance" "foo" { 1579 ami = "ami-55a7ea65" 1580 1581 instance_type = "m3.medium" 1582 1583 root_block_device { 1584 volume_type = "gp2" 1585 volume_size = 11 1586 } 1587 ebs_block_device { 1588 device_name = "/dev/sdb" 1589 volume_size = 9 1590 } 1591 ebs_block_device { 1592 device_name = "/dev/sdc" 1593 volume_size = 10 1594 volume_type = "io1" 1595 iops = 100 1596 } 1597 1598 ebs_block_device { 1599 device_name = "/dev/sdd" 1600 volume_size = 12 1601 encrypted = true 1602 } 1603 1604 ephemeral_block_device { 1605 device_name = "/dev/sde" 1606 virtual_name = "ephemeral0" 1607 } 1608 } 1609 ` 1610 1611 const testAccCheckInstanceConfigWithVolumeTags = ` 1612 resource "aws_instance" "foo" { 1613 ami = "ami-55a7ea65" 1614 1615 instance_type = "m3.medium" 1616 1617 root_block_device { 1618 volume_type = "gp2" 1619 volume_size = 11 1620 } 1621 ebs_block_device { 1622 device_name = "/dev/sdb" 1623 volume_size = 9 1624 } 1625 ebs_block_device { 1626 device_name = "/dev/sdc" 1627 volume_size = 10 1628 volume_type = "io1" 1629 iops = 100 1630 } 1631 1632 ebs_block_device { 1633 device_name = "/dev/sdd" 1634 volume_size = 12 1635 encrypted = true 1636 } 1637 1638 ephemeral_block_device { 1639 device_name = "/dev/sde" 1640 virtual_name = "ephemeral0" 1641 } 1642 1643 volume_tags { 1644 Name = "acceptance-test-volume-tag" 1645 } 1646 } 1647 ` 1648 1649 const testAccCheckInstanceConfigWithVolumeTagsUpdate = ` 1650 resource "aws_instance" "foo" { 1651 ami = "ami-55a7ea65" 1652 1653 instance_type = "m3.medium" 1654 1655 root_block_device { 1656 volume_type = "gp2" 1657 volume_size = 11 1658 } 1659 ebs_block_device { 1660 device_name = "/dev/sdb" 1661 volume_size = 9 1662 } 1663 ebs_block_device { 1664 device_name = "/dev/sdc" 1665 volume_size = 10 1666 volume_type = "io1" 1667 iops = 100 1668 } 1669 1670 ebs_block_device { 1671 device_name = "/dev/sdd" 1672 volume_size = 12 1673 encrypted = true 1674 } 1675 1676 ephemeral_block_device { 1677 device_name = "/dev/sde" 1678 virtual_name = "ephemeral0" 1679 } 1680 1681 volume_tags { 1682 Name = "acceptance-test-volume-tag" 1683 Environment = "dev" 1684 } 1685 } 1686 ` 1687 1688 const testAccCheckInstanceConfigTagsUpdate = ` 1689 resource "aws_instance" "foo" { 1690 ami = "ami-4fccb37f" 1691 instance_type = "m1.small" 1692 tags { 1693 bar = "baz" 1694 } 1695 } 1696 ` 1697 1698 func testAccInstanceConfigWithoutInstanceProfile(rName string) string { 1699 return fmt.Sprintf(` 1700 resource "aws_iam_role" "test" { 1701 name = "test-%s" 1702 assume_role_policy = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"Service\":[\"ec2.amazonaws.com\"]},\"Action\":[\"sts:AssumeRole\"]}]}" 1703 } 1704 1705 resource "aws_iam_instance_profile" "test" { 1706 name = "test-%s" 1707 roles = ["${aws_iam_role.test.name}"] 1708 } 1709 1710 resource "aws_instance" "foo" { 1711 ami = "ami-4fccb37f" 1712 instance_type = "m1.small" 1713 tags { 1714 bar = "baz" 1715 } 1716 }`, rName, rName) 1717 } 1718 1719 func testAccInstanceConfigWithInstanceProfile(rName string) string { 1720 return fmt.Sprintf(` 1721 resource "aws_iam_role" "test" { 1722 name = "test-%s" 1723 assume_role_policy = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"Service\":[\"ec2.amazonaws.com\"]},\"Action\":[\"sts:AssumeRole\"]}]}" 1724 } 1725 1726 resource "aws_iam_instance_profile" "test" { 1727 name = "test-%s" 1728 roles = ["${aws_iam_role.test.name}"] 1729 } 1730 1731 resource "aws_instance" "foo" { 1732 ami = "ami-4fccb37f" 1733 instance_type = "m1.small" 1734 iam_instance_profile = "${aws_iam_instance_profile.test.name}" 1735 tags { 1736 bar = "baz" 1737 } 1738 }`, rName, rName) 1739 } 1740 1741 const testAccInstanceConfigPrivateIP = ` 1742 resource "aws_vpc" "foo" { 1743 cidr_block = "10.1.0.0/16" 1744 tags { 1745 Name = "testAccInstanceConfigPrivateIP" 1746 } 1747 } 1748 1749 resource "aws_subnet" "foo" { 1750 cidr_block = "10.1.1.0/24" 1751 vpc_id = "${aws_vpc.foo.id}" 1752 } 1753 1754 resource "aws_instance" "foo" { 1755 ami = "ami-c5eabbf5" 1756 instance_type = "t2.micro" 1757 subnet_id = "${aws_subnet.foo.id}" 1758 private_ip = "10.1.1.42" 1759 } 1760 ` 1761 1762 const testAccInstanceConfigAssociatePublicIPAndPrivateIP = ` 1763 resource "aws_vpc" "foo" { 1764 cidr_block = "10.1.0.0/16" 1765 tags { 1766 Name = "testAccInstanceConfigAssociatePublicIPAndPrivateIP" 1767 } 1768 } 1769 1770 resource "aws_subnet" "foo" { 1771 cidr_block = "10.1.1.0/24" 1772 vpc_id = "${aws_vpc.foo.id}" 1773 } 1774 1775 resource "aws_instance" "foo" { 1776 ami = "ami-c5eabbf5" 1777 instance_type = "t2.micro" 1778 subnet_id = "${aws_subnet.foo.id}" 1779 associate_public_ip_address = true 1780 private_ip = "10.1.1.42" 1781 } 1782 ` 1783 1784 const testAccInstanceNetworkInstanceSecurityGroups = ` 1785 resource "aws_internet_gateway" "gw" { 1786 vpc_id = "${aws_vpc.foo.id}" 1787 } 1788 1789 resource "aws_vpc" "foo" { 1790 cidr_block = "10.1.0.0/16" 1791 tags { 1792 Name = "tf-network-test" 1793 } 1794 } 1795 1796 resource "aws_security_group" "tf_test_foo" { 1797 name = "tf_test_foo" 1798 description = "foo" 1799 vpc_id="${aws_vpc.foo.id}" 1800 1801 ingress { 1802 protocol = "icmp" 1803 from_port = -1 1804 to_port = -1 1805 cidr_blocks = ["0.0.0.0/0"] 1806 } 1807 } 1808 1809 resource "aws_subnet" "foo" { 1810 cidr_block = "10.1.1.0/24" 1811 vpc_id = "${aws_vpc.foo.id}" 1812 } 1813 1814 resource "aws_instance" "foo_instance" { 1815 ami = "ami-21f78e11" 1816 instance_type = "t1.micro" 1817 vpc_security_group_ids = ["${aws_security_group.tf_test_foo.id}"] 1818 subnet_id = "${aws_subnet.foo.id}" 1819 associate_public_ip_address = true 1820 depends_on = ["aws_internet_gateway.gw"] 1821 } 1822 1823 resource "aws_eip" "foo_eip" { 1824 instance = "${aws_instance.foo_instance.id}" 1825 vpc = true 1826 depends_on = ["aws_internet_gateway.gw"] 1827 } 1828 ` 1829 1830 const testAccInstanceNetworkInstanceVPCSecurityGroupIDs = ` 1831 resource "aws_internet_gateway" "gw" { 1832 vpc_id = "${aws_vpc.foo.id}" 1833 } 1834 1835 resource "aws_vpc" "foo" { 1836 cidr_block = "10.1.0.0/16" 1837 tags { 1838 Name = "tf-network-test" 1839 } 1840 } 1841 1842 resource "aws_security_group" "tf_test_foo" { 1843 name = "tf_test_foo" 1844 description = "foo" 1845 vpc_id="${aws_vpc.foo.id}" 1846 1847 ingress { 1848 protocol = "icmp" 1849 from_port = -1 1850 to_port = -1 1851 cidr_blocks = ["0.0.0.0/0"] 1852 } 1853 } 1854 1855 resource "aws_subnet" "foo" { 1856 cidr_block = "10.1.1.0/24" 1857 vpc_id = "${aws_vpc.foo.id}" 1858 } 1859 1860 resource "aws_instance" "foo_instance" { 1861 ami = "ami-21f78e11" 1862 instance_type = "t1.micro" 1863 vpc_security_group_ids = ["${aws_security_group.tf_test_foo.id}"] 1864 subnet_id = "${aws_subnet.foo.id}" 1865 depends_on = ["aws_internet_gateway.gw"] 1866 } 1867 1868 resource "aws_eip" "foo_eip" { 1869 instance = "${aws_instance.foo_instance.id}" 1870 vpc = true 1871 depends_on = ["aws_internet_gateway.gw"] 1872 } 1873 ` 1874 1875 func testAccInstanceConfigKeyPair(keyPairName string) string { 1876 return fmt.Sprintf(` 1877 provider "aws" { 1878 region = "us-east-1" 1879 } 1880 1881 resource "aws_key_pair" "debugging" { 1882 key_name = "%s" 1883 public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD3F6tyPEFEzV0LX3X8BsXdMsQz1x2cEikKDEY0aIj41qgxMCP/iteneqXSIFZBp5vizPvaoIR3Um9xK7PGoW8giupGn+EPuxIA4cDM4vzOqOkiMPhz5XK0whEjkVzTo4+S0puvDZuwIsdiW9mxhJc7tgBNL0cYlWSYVkz4G/fslNfRPW5mYAM49f4fhtxPb5ok4Q2Lg9dPKVHO/Bgeu5woMc7RY0p1ej6D4CKFE6lymSDJpW0YHX/wqE9+cfEauh7xZcG0q9t2ta6F6fmX0agvpFyZo8aFbXeUBr7osSCJNgvavWbM/06niWrOvYX2xwWdhXmXSrbX8ZbabVohBK41 phodgson@thoughtworks.com" 1884 } 1885 1886 resource "aws_instance" "foo" { 1887 ami = "ami-408c7f28" 1888 instance_type = "t1.micro" 1889 key_name = "${aws_key_pair.debugging.key_name}" 1890 tags { 1891 Name = "testAccInstanceConfigKeyPair_TestAMI" 1892 } 1893 } 1894 `, keyPairName) 1895 } 1896 1897 const testAccInstanceConfigRootBlockDeviceMismatch = ` 1898 resource "aws_vpc" "foo" { 1899 cidr_block = "10.1.0.0/16" 1900 tags { 1901 Name = "testAccInstanceConfigRootBlockDeviceMismatch" 1902 } 1903 } 1904 1905 resource "aws_subnet" "foo" { 1906 cidr_block = "10.1.1.0/24" 1907 vpc_id = "${aws_vpc.foo.id}" 1908 } 1909 1910 resource "aws_instance" "foo" { 1911 // This is an AMI with RootDeviceName: "/dev/sda1"; actual root: "/dev/sda" 1912 ami = "ami-ef5b69df" 1913 instance_type = "t1.micro" 1914 subnet_id = "${aws_subnet.foo.id}" 1915 root_block_device { 1916 volume_size = 13 1917 } 1918 } 1919 ` 1920 1921 const testAccInstanceConfigForceNewAndTagsDrift = ` 1922 resource "aws_vpc" "foo" { 1923 cidr_block = "10.1.0.0/16" 1924 tags { 1925 Name = "testAccInstanceConfigForceNewAndTagsDrift" 1926 } 1927 } 1928 1929 resource "aws_subnet" "foo" { 1930 cidr_block = "10.1.1.0/24" 1931 vpc_id = "${aws_vpc.foo.id}" 1932 } 1933 1934 resource "aws_instance" "foo" { 1935 ami = "ami-22b9a343" 1936 instance_type = "t2.nano" 1937 subnet_id = "${aws_subnet.foo.id}" 1938 } 1939 ` 1940 1941 const testAccInstanceConfigForceNewAndTagsDrift_Update = ` 1942 resource "aws_vpc" "foo" { 1943 cidr_block = "10.1.0.0/16" 1944 tags { 1945 Name = "testAccInstanceConfigForceNewAndTagsDrift_Update" 1946 } 1947 } 1948 1949 resource "aws_subnet" "foo" { 1950 cidr_block = "10.1.1.0/24" 1951 vpc_id = "${aws_vpc.foo.id}" 1952 } 1953 1954 resource "aws_instance" "foo" { 1955 ami = "ami-22b9a343" 1956 instance_type = "t2.micro" 1957 subnet_id = "${aws_subnet.foo.id}" 1958 } 1959 ` 1960 1961 const testAccInstanceConfigPrimaryNetworkInterface = ` 1962 resource "aws_vpc" "foo" { 1963 cidr_block = "172.16.0.0/16" 1964 tags { 1965 Name = "tf-instance-test" 1966 } 1967 } 1968 1969 resource "aws_subnet" "foo" { 1970 vpc_id = "${aws_vpc.foo.id}" 1971 cidr_block = "172.16.10.0/24" 1972 availability_zone = "us-west-2a" 1973 tags { 1974 Name = "tf-instance-test" 1975 } 1976 } 1977 1978 resource "aws_network_interface" "bar" { 1979 subnet_id = "${aws_subnet.foo.id}" 1980 private_ips = ["172.16.10.100"] 1981 tags { 1982 Name = "primary_network_interface" 1983 } 1984 } 1985 1986 resource "aws_instance" "foo" { 1987 ami = "ami-22b9a343" 1988 instance_type = "t2.micro" 1989 network_interface { 1990 network_interface_id = "${aws_network_interface.bar.id}" 1991 device_index = 0 1992 } 1993 } 1994 ` 1995 1996 const testAccInstanceConfigPrimaryNetworkInterfaceSourceDestCheck = ` 1997 resource "aws_vpc" "foo" { 1998 cidr_block = "172.16.0.0/16" 1999 tags { 2000 Name = "tf-instance-test" 2001 } 2002 } 2003 2004 resource "aws_subnet" "foo" { 2005 vpc_id = "${aws_vpc.foo.id}" 2006 cidr_block = "172.16.10.0/24" 2007 availability_zone = "us-west-2a" 2008 tags { 2009 Name = "tf-instance-test" 2010 } 2011 } 2012 2013 resource "aws_network_interface" "bar" { 2014 subnet_id = "${aws_subnet.foo.id}" 2015 private_ips = ["172.16.10.100"] 2016 source_dest_check = false 2017 tags { 2018 Name = "primary_network_interface" 2019 } 2020 } 2021 2022 resource "aws_instance" "foo" { 2023 ami = "ami-22b9a343" 2024 instance_type = "t2.micro" 2025 network_interface { 2026 network_interface_id = "${aws_network_interface.bar.id}" 2027 device_index = 0 2028 } 2029 } 2030 ` 2031 2032 const testAccInstanceConfigAddSecondaryNetworkInterfaceBefore = ` 2033 resource "aws_vpc" "foo" { 2034 cidr_block = "172.16.0.0/16" 2035 tags { 2036 Name = "tf-instance-test" 2037 } 2038 } 2039 2040 resource "aws_subnet" "foo" { 2041 vpc_id = "${aws_vpc.foo.id}" 2042 cidr_block = "172.16.10.0/24" 2043 availability_zone = "us-west-2a" 2044 tags { 2045 Name = "tf-instance-test" 2046 } 2047 } 2048 2049 resource "aws_network_interface" "primary" { 2050 subnet_id = "${aws_subnet.foo.id}" 2051 private_ips = ["172.16.10.100"] 2052 tags { 2053 Name = "primary_network_interface" 2054 } 2055 } 2056 2057 resource "aws_network_interface" "secondary" { 2058 subnet_id = "${aws_subnet.foo.id}" 2059 private_ips = ["172.16.10.101"] 2060 tags { 2061 Name = "secondary_network_interface" 2062 } 2063 } 2064 2065 resource "aws_instance" "foo" { 2066 ami = "ami-22b9a343" 2067 instance_type = "t2.micro" 2068 network_interface { 2069 network_interface_id = "${aws_network_interface.primary.id}" 2070 device_index = 0 2071 } 2072 } 2073 ` 2074 2075 const testAccInstanceConfigAddSecondaryNetworkInterfaceAfter = ` 2076 resource "aws_vpc" "foo" { 2077 cidr_block = "172.16.0.0/16" 2078 tags { 2079 Name = "tf-instance-test" 2080 } 2081 } 2082 2083 resource "aws_subnet" "foo" { 2084 vpc_id = "${aws_vpc.foo.id}" 2085 cidr_block = "172.16.10.0/24" 2086 availability_zone = "us-west-2a" 2087 tags { 2088 Name = "tf-instance-test" 2089 } 2090 } 2091 2092 resource "aws_network_interface" "primary" { 2093 subnet_id = "${aws_subnet.foo.id}" 2094 private_ips = ["172.16.10.100"] 2095 tags { 2096 Name = "primary_network_interface" 2097 } 2098 } 2099 2100 // Attach previously created network interface, observe no state diff on instance resource 2101 resource "aws_network_interface" "secondary" { 2102 subnet_id = "${aws_subnet.foo.id}" 2103 private_ips = ["172.16.10.101"] 2104 tags { 2105 Name = "secondary_network_interface" 2106 } 2107 attachment { 2108 instance = "${aws_instance.foo.id}" 2109 device_index = 1 2110 } 2111 } 2112 2113 resource "aws_instance" "foo" { 2114 ami = "ami-22b9a343" 2115 instance_type = "t2.micro" 2116 network_interface { 2117 network_interface_id = "${aws_network_interface.primary.id}" 2118 device_index = 0 2119 } 2120 } 2121 ` 2122 2123 const testAccInstanceConfigAddSecurityGroupBefore = ` 2124 resource "aws_vpc" "foo" { 2125 cidr_block = "172.16.0.0/16" 2126 tags { 2127 Name = "tf-eni-test" 2128 } 2129 } 2130 2131 resource "aws_subnet" "foo" { 2132 vpc_id = "${aws_vpc.foo.id}" 2133 cidr_block = "172.16.10.0/24" 2134 availability_zone = "us-west-2a" 2135 tags { 2136 Name = "tf-foo-instance-add-sg-test" 2137 } 2138 } 2139 2140 resource "aws_subnet" "bar" { 2141 vpc_id = "${aws_vpc.foo.id}" 2142 cidr_block = "172.16.11.0/24" 2143 availability_zone = "us-west-2a" 2144 tags { 2145 Name = "tf-bar-instance-add-sg-test" 2146 } 2147 } 2148 2149 resource "aws_security_group" "foo" { 2150 vpc_id = "${aws_vpc.foo.id}" 2151 description = "foo" 2152 name = "foo" 2153 } 2154 2155 resource "aws_security_group" "bar" { 2156 vpc_id = "${aws_vpc.foo.id}" 2157 description = "bar" 2158 name = "bar" 2159 } 2160 2161 resource "aws_instance" "foo" { 2162 ami = "ami-c5eabbf5" 2163 instance_type = "t2.micro" 2164 subnet_id = "${aws_subnet.bar.id}" 2165 associate_public_ip_address = false 2166 vpc_security_group_ids = [ 2167 "${aws_security_group.foo.id}" 2168 ] 2169 tags { 2170 Name = "foo-instance-sg-add-test" 2171 } 2172 } 2173 2174 resource "aws_network_interface" "bar" { 2175 subnet_id = "${aws_subnet.foo.id}" 2176 private_ips = ["172.16.10.100"] 2177 security_groups = ["${aws_security_group.foo.id}"] 2178 attachment { 2179 instance = "${aws_instance.foo.id}" 2180 device_index = 1 2181 } 2182 tags { 2183 Name = "bar_interface" 2184 } 2185 } 2186 ` 2187 2188 const testAccInstanceConfigAddSecurityGroupAfter = ` 2189 resource "aws_vpc" "foo" { 2190 cidr_block = "172.16.0.0/16" 2191 tags { 2192 Name = "tf-eni-test" 2193 } 2194 } 2195 2196 resource "aws_subnet" "foo" { 2197 vpc_id = "${aws_vpc.foo.id}" 2198 cidr_block = "172.16.10.0/24" 2199 availability_zone = "us-west-2a" 2200 tags { 2201 Name = "tf-foo-instance-add-sg-test" 2202 } 2203 } 2204 2205 resource "aws_subnet" "bar" { 2206 vpc_id = "${aws_vpc.foo.id}" 2207 cidr_block = "172.16.11.0/24" 2208 availability_zone = "us-west-2a" 2209 tags { 2210 Name = "tf-bar-instance-add-sg-test" 2211 } 2212 } 2213 2214 resource "aws_security_group" "foo" { 2215 vpc_id = "${aws_vpc.foo.id}" 2216 description = "foo" 2217 name = "foo" 2218 } 2219 2220 resource "aws_security_group" "bar" { 2221 vpc_id = "${aws_vpc.foo.id}" 2222 description = "bar" 2223 name = "bar" 2224 } 2225 2226 resource "aws_instance" "foo" { 2227 ami = "ami-c5eabbf5" 2228 instance_type = "t2.micro" 2229 subnet_id = "${aws_subnet.bar.id}" 2230 associate_public_ip_address = false 2231 vpc_security_group_ids = [ 2232 "${aws_security_group.foo.id}", 2233 "${aws_security_group.bar.id}" 2234 ] 2235 tags { 2236 Name = "foo-instance-sg-add-test" 2237 } 2238 } 2239 2240 resource "aws_network_interface" "bar" { 2241 subnet_id = "${aws_subnet.foo.id}" 2242 private_ips = ["172.16.10.100"] 2243 security_groups = ["${aws_security_group.foo.id}"] 2244 attachment { 2245 instance = "${aws_instance.foo.id}" 2246 device_index = 1 2247 } 2248 tags { 2249 Name = "bar_interface" 2250 } 2251 } 2252 `