github.com/bendemaree/terraform@v0.5.4-0.20150613200311-f50d97d6eee6/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/aws/awsutil" 11 "github.com/aws/aws-sdk-go/service/ec2" 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 Providers: testAccProviders, 39 CheckDestroy: testAccCheckInstanceDestroy, 40 Steps: []resource.TestStep{ 41 // Create a volume to cover #1249 42 resource.TestStep{ 43 // Need a resource in this config so the provisioner will be available 44 Config: testAccInstanceConfig_pre, 45 Check: func(*terraform.State) error { 46 conn := testAccProvider.Meta().(*AWSClient).ec2conn 47 var err error 48 vol, err = conn.CreateVolume(&ec2.CreateVolumeInput{ 49 AvailabilityZone: aws.String("us-west-2a"), 50 Size: aws.Long(int64(5)), 51 }) 52 return err 53 }, 54 }, 55 56 resource.TestStep{ 57 Config: testAccInstanceConfig, 58 Check: resource.ComposeTestCheckFunc( 59 testAccCheckInstanceExists( 60 "aws_instance.foo", &v), 61 testCheck, 62 resource.TestCheckResourceAttr( 63 "aws_instance.foo", 64 "user_data", 65 "3dc39dda39be1205215e776bad998da361a5955d"), 66 resource.TestCheckResourceAttr( 67 "aws_instance.foo", "ebs_block_device.#", "0"), 68 ), 69 }, 70 71 // We repeat the exact same test so that we can be sure 72 // that the user data hash stuff is working without generating 73 // an incorrect diff. 74 resource.TestStep{ 75 Config: testAccInstanceConfig, 76 Check: resource.ComposeTestCheckFunc( 77 testAccCheckInstanceExists( 78 "aws_instance.foo", &v), 79 testCheck, 80 resource.TestCheckResourceAttr( 81 "aws_instance.foo", 82 "user_data", 83 "3dc39dda39be1205215e776bad998da361a5955d"), 84 resource.TestCheckResourceAttr( 85 "aws_instance.foo", "ebs_block_device.#", "0"), 86 ), 87 }, 88 89 // Clean up volume created above 90 resource.TestStep{ 91 Config: testAccInstanceConfig, 92 Check: func(*terraform.State) error { 93 conn := testAccProvider.Meta().(*AWSClient).ec2conn 94 _, err := conn.DeleteVolume(&ec2.DeleteVolumeInput{VolumeID: vol.VolumeID}) 95 return err 96 }, 97 }, 98 }, 99 }) 100 } 101 102 func TestAccAWSInstance_blockDevices(t *testing.T) { 103 var v ec2.Instance 104 105 testCheck := func() resource.TestCheckFunc { 106 return func(*terraform.State) error { 107 108 // Map out the block devices by name, which should be unique. 109 blockDevices := make(map[string]*ec2.InstanceBlockDeviceMapping) 110 for _, blockDevice := range v.BlockDeviceMappings { 111 blockDevices[*blockDevice.DeviceName] = blockDevice 112 } 113 114 // Check if the root block device exists. 115 if _, ok := blockDevices["/dev/sda1"]; !ok { 116 fmt.Errorf("block device doesn't exist: /dev/sda1") 117 } 118 119 // Check if the secondary block device exists. 120 if _, ok := blockDevices["/dev/sdb"]; !ok { 121 fmt.Errorf("block device doesn't exist: /dev/sdb") 122 } 123 124 // Check if the third block device exists. 125 if _, ok := blockDevices["/dev/sdc"]; !ok { 126 fmt.Errorf("block device doesn't exist: /dev/sdc") 127 } 128 129 // Check if the encrypted block device exists 130 if _, ok := blockDevices["/dev/sdd"]; !ok { 131 fmt.Errorf("block device doesn't exist: /dev/sdd") 132 } 133 134 return nil 135 } 136 } 137 138 resource.Test(t, resource.TestCase{ 139 PreCheck: func() { testAccPreCheck(t) }, 140 Providers: testAccProviders, 141 CheckDestroy: testAccCheckInstanceDestroy, 142 Steps: []resource.TestStep{ 143 resource.TestStep{ 144 Config: testAccInstanceConfigBlockDevices, 145 Check: resource.ComposeTestCheckFunc( 146 testAccCheckInstanceExists( 147 "aws_instance.foo", &v), 148 resource.TestCheckResourceAttr( 149 "aws_instance.foo", "root_block_device.#", "1"), 150 resource.TestCheckResourceAttr( 151 "aws_instance.foo", "root_block_device.0.volume_size", "11"), 152 resource.TestCheckResourceAttr( 153 "aws_instance.foo", "root_block_device.0.volume_type", "gp2"), 154 resource.TestCheckResourceAttr( 155 "aws_instance.foo", "ebs_block_device.#", "3"), 156 resource.TestCheckResourceAttr( 157 "aws_instance.foo", "ebs_block_device.2576023345.device_name", "/dev/sdb"), 158 resource.TestCheckResourceAttr( 159 "aws_instance.foo", "ebs_block_device.2576023345.volume_size", "9"), 160 resource.TestCheckResourceAttr( 161 "aws_instance.foo", "ebs_block_device.2576023345.volume_type", "standard"), 162 resource.TestCheckResourceAttr( 163 "aws_instance.foo", "ebs_block_device.2554893574.device_name", "/dev/sdc"), 164 resource.TestCheckResourceAttr( 165 "aws_instance.foo", "ebs_block_device.2554893574.volume_size", "10"), 166 resource.TestCheckResourceAttr( 167 "aws_instance.foo", "ebs_block_device.2554893574.volume_type", "io1"), 168 resource.TestCheckResourceAttr( 169 "aws_instance.foo", "ebs_block_device.2554893574.iops", "100"), 170 resource.TestCheckResourceAttr( 171 "aws_instance.foo", "ebs_block_device.2634515331.device_name", "/dev/sdd"), 172 resource.TestCheckResourceAttr( 173 "aws_instance.foo", "ebs_block_device.2634515331.encrypted", "true"), 174 resource.TestCheckResourceAttr( 175 "aws_instance.foo", "ebs_block_device.2634515331.volume_size", "12"), 176 resource.TestCheckResourceAttr( 177 "aws_instance.foo", "ephemeral_block_device.#", "1"), 178 resource.TestCheckResourceAttr( 179 "aws_instance.foo", "ephemeral_block_device.1692014856.device_name", "/dev/sde"), 180 resource.TestCheckResourceAttr( 181 "aws_instance.foo", "ephemeral_block_device.1692014856.virtual_name", "ephemeral0"), 182 testCheck(), 183 ), 184 }, 185 }, 186 }) 187 } 188 189 func TestAccAWSInstance_sourceDestCheck(t *testing.T) { 190 var v ec2.Instance 191 192 testCheck := func(enabled bool) resource.TestCheckFunc { 193 return func(*terraform.State) error { 194 if *v.SourceDestCheck != enabled { 195 return fmt.Errorf("bad source_dest_check: %#v", *v.SourceDestCheck) 196 } 197 198 return nil 199 } 200 } 201 202 resource.Test(t, resource.TestCase{ 203 PreCheck: func() { testAccPreCheck(t) }, 204 Providers: testAccProviders, 205 CheckDestroy: testAccCheckInstanceDestroy, 206 Steps: []resource.TestStep{ 207 resource.TestStep{ 208 Config: testAccInstanceConfigSourceDestDisable, 209 Check: resource.ComposeTestCheckFunc( 210 testAccCheckInstanceExists("aws_instance.foo", &v), 211 testCheck(false), 212 ), 213 }, 214 215 resource.TestStep{ 216 Config: testAccInstanceConfigSourceDestEnable, 217 Check: resource.ComposeTestCheckFunc( 218 testAccCheckInstanceExists("aws_instance.foo", &v), 219 testCheck(true), 220 ), 221 }, 222 223 resource.TestStep{ 224 Config: testAccInstanceConfigSourceDestDisable, 225 Check: resource.ComposeTestCheckFunc( 226 testAccCheckInstanceExists("aws_instance.foo", &v), 227 testCheck(false), 228 ), 229 }, 230 }, 231 }) 232 } 233 234 func TestAccAWSInstance_disableApiTermination(t *testing.T) { 235 var v ec2.Instance 236 237 checkDisableApiTermination := func(expected bool) resource.TestCheckFunc { 238 return func(*terraform.State) error { 239 conn := testAccProvider.Meta().(*AWSClient).ec2conn 240 r, err := conn.DescribeInstanceAttribute(&ec2.DescribeInstanceAttributeInput{ 241 InstanceID: v.InstanceID, 242 Attribute: aws.String("disableApiTermination"), 243 }) 244 if err != nil { 245 return err 246 } 247 got := *r.DisableAPITermination.Value 248 if got != expected { 249 return fmt.Errorf("expected: %t, got: %t", expected, got) 250 } 251 return nil 252 } 253 } 254 255 resource.Test(t, resource.TestCase{ 256 PreCheck: func() { testAccPreCheck(t) }, 257 Providers: testAccProviders, 258 CheckDestroy: testAccCheckInstanceDestroy, 259 Steps: []resource.TestStep{ 260 resource.TestStep{ 261 Config: testAccInstanceConfigDisableAPITermination(true), 262 Check: resource.ComposeTestCheckFunc( 263 testAccCheckInstanceExists("aws_instance.foo", &v), 264 checkDisableApiTermination(true), 265 ), 266 }, 267 268 resource.TestStep{ 269 Config: testAccInstanceConfigDisableAPITermination(false), 270 Check: resource.ComposeTestCheckFunc( 271 testAccCheckInstanceExists("aws_instance.foo", &v), 272 checkDisableApiTermination(false), 273 ), 274 }, 275 }, 276 }) 277 } 278 279 func TestAccAWSInstance_vpc(t *testing.T) { 280 var v ec2.Instance 281 282 resource.Test(t, resource.TestCase{ 283 PreCheck: func() { testAccPreCheck(t) }, 284 Providers: testAccProviders, 285 CheckDestroy: testAccCheckInstanceDestroy, 286 Steps: []resource.TestStep{ 287 resource.TestStep{ 288 Config: testAccInstanceConfigVPC, 289 Check: resource.ComposeTestCheckFunc( 290 testAccCheckInstanceExists( 291 "aws_instance.foo", &v), 292 ), 293 }, 294 }, 295 }) 296 } 297 298 func TestAccAWSInstance_multipleRegions(t *testing.T) { 299 var v ec2.Instance 300 301 // record the initialized providers so that we can use them to 302 // check for the instances in each region 303 var providers []*schema.Provider 304 providerFactories := map[string]terraform.ResourceProviderFactory{ 305 "aws": func() (terraform.ResourceProvider, error) { 306 p := Provider() 307 providers = append(providers, p.(*schema.Provider)) 308 return p, nil 309 }, 310 } 311 312 resource.Test(t, resource.TestCase{ 313 PreCheck: func() { testAccPreCheck(t) }, 314 ProviderFactories: providerFactories, 315 CheckDestroy: testAccCheckInstanceDestroyWithProviders(&providers), 316 Steps: []resource.TestStep{ 317 resource.TestStep{ 318 Config: testAccInstanceConfigMultipleRegions, 319 Check: resource.ComposeTestCheckFunc( 320 testAccCheckInstanceExistsWithProviders( 321 "aws_instance.foo", &v, &providers), 322 testAccCheckInstanceExistsWithProviders( 323 "aws_instance.bar", &v, &providers), 324 ), 325 }, 326 }, 327 }) 328 } 329 330 func TestAccAWSInstance_NetworkInstanceSecurityGroups(t *testing.T) { 331 var v ec2.Instance 332 333 resource.Test(t, resource.TestCase{ 334 PreCheck: func() { testAccPreCheck(t) }, 335 Providers: testAccProviders, 336 CheckDestroy: testAccCheckInstanceDestroy, 337 Steps: []resource.TestStep{ 338 resource.TestStep{ 339 Config: testAccInstanceNetworkInstanceSecurityGroups, 340 Check: resource.ComposeTestCheckFunc( 341 testAccCheckInstanceExists( 342 "aws_instance.foo_instance", &v), 343 ), 344 }, 345 }, 346 }) 347 } 348 349 func TestAccAWSInstance_NetworkInstanceVPCSecurityGroupIDs(t *testing.T) { 350 var v ec2.Instance 351 352 resource.Test(t, resource.TestCase{ 353 PreCheck: func() { testAccPreCheck(t) }, 354 Providers: testAccProviders, 355 CheckDestroy: testAccCheckInstanceDestroy, 356 Steps: []resource.TestStep{ 357 resource.TestStep{ 358 Config: testAccInstanceNetworkInstanceVPCSecurityGroupIDs, 359 Check: resource.ComposeTestCheckFunc( 360 testAccCheckInstanceExists( 361 "aws_instance.foo_instance", &v), 362 resource.TestCheckResourceAttr( 363 "aws_instance.foo_instance", "security_groups.#", "0"), 364 resource.TestCheckResourceAttr( 365 "aws_instance.foo_instance", "vpc_security_group_ids.#", "1"), 366 ), 367 }, 368 }, 369 }) 370 } 371 372 func TestAccAWSInstance_tags(t *testing.T) { 373 var v ec2.Instance 374 375 resource.Test(t, resource.TestCase{ 376 PreCheck: func() { testAccPreCheck(t) }, 377 Providers: testAccProviders, 378 CheckDestroy: testAccCheckInstanceDestroy, 379 Steps: []resource.TestStep{ 380 resource.TestStep{ 381 Config: testAccCheckInstanceConfigTags, 382 Check: resource.ComposeTestCheckFunc( 383 testAccCheckInstanceExists("aws_instance.foo", &v), 384 testAccCheckTags(&v.Tags, "foo", "bar"), 385 // Guard against regression of https://github.com/hashicorp/terraform/issues/914 386 testAccCheckTags(&v.Tags, "#", ""), 387 ), 388 }, 389 390 resource.TestStep{ 391 Config: testAccCheckInstanceConfigTagsUpdate, 392 Check: resource.ComposeTestCheckFunc( 393 testAccCheckInstanceExists("aws_instance.foo", &v), 394 testAccCheckTags(&v.Tags, "foo", ""), 395 testAccCheckTags(&v.Tags, "bar", "baz"), 396 ), 397 }, 398 }, 399 }) 400 } 401 402 func TestAccAWSInstance_privateIP(t *testing.T) { 403 var v ec2.Instance 404 405 testCheckPrivateIP := func() resource.TestCheckFunc { 406 return func(*terraform.State) error { 407 if *v.PrivateIPAddress != "10.1.1.42" { 408 return fmt.Errorf("bad private IP: %s", *v.PrivateIPAddress) 409 } 410 411 return nil 412 } 413 } 414 415 resource.Test(t, resource.TestCase{ 416 PreCheck: func() { testAccPreCheck(t) }, 417 Providers: testAccProviders, 418 CheckDestroy: testAccCheckInstanceDestroy, 419 Steps: []resource.TestStep{ 420 resource.TestStep{ 421 Config: testAccInstanceConfigPrivateIP, 422 Check: resource.ComposeTestCheckFunc( 423 testAccCheckInstanceExists("aws_instance.foo", &v), 424 testCheckPrivateIP(), 425 ), 426 }, 427 }, 428 }) 429 } 430 431 func TestAccAWSInstance_associatePublicIPAndPrivateIP(t *testing.T) { 432 var v ec2.Instance 433 434 testCheckPrivateIP := func() resource.TestCheckFunc { 435 return func(*terraform.State) error { 436 if *v.PrivateIPAddress != "10.1.1.42" { 437 return fmt.Errorf("bad private IP: %s", *v.PrivateIPAddress) 438 } 439 440 return nil 441 } 442 } 443 444 resource.Test(t, resource.TestCase{ 445 PreCheck: func() { testAccPreCheck(t) }, 446 Providers: testAccProviders, 447 CheckDestroy: testAccCheckInstanceDestroy, 448 Steps: []resource.TestStep{ 449 resource.TestStep{ 450 Config: testAccInstanceConfigAssociatePublicIPAndPrivateIP, 451 Check: resource.ComposeTestCheckFunc( 452 testAccCheckInstanceExists("aws_instance.foo", &v), 453 testCheckPrivateIP(), 454 ), 455 }, 456 }, 457 }) 458 } 459 460 // Guard against regression with KeyPairs 461 // https://github.com/hashicorp/terraform/issues/2302 462 func TestAccAWSInstance_keyPairCheck(t *testing.T) { 463 var v ec2.Instance 464 465 testCheckKeyPair := func(keyName string) resource.TestCheckFunc { 466 return func(*terraform.State) error { 467 if v.KeyName == nil { 468 return fmt.Errorf("No Key Pair found, expected(%s)", keyName) 469 } 470 if *v.KeyName != keyName { 471 return fmt.Errorf("Bad key name, expected (%s), got (%s)", keyName, awsutil.StringValue(v.KeyName)) 472 } 473 474 return nil 475 } 476 } 477 478 resource.Test(t, resource.TestCase{ 479 PreCheck: func() { testAccPreCheck(t) }, 480 Providers: testAccProviders, 481 CheckDestroy: testAccCheckInstanceDestroy, 482 Steps: []resource.TestStep{ 483 resource.TestStep{ 484 Config: testAccInstanceConfigKeyPair, 485 Check: resource.ComposeTestCheckFunc( 486 testAccCheckInstanceExists("aws_instance.foo", &v), 487 testCheckKeyPair("tmp-key"), 488 ), 489 }, 490 }, 491 }) 492 } 493 494 func testAccCheckInstanceDestroy(s *terraform.State) error { 495 return testAccCheckInstanceDestroyWithProvider(s, testAccProvider) 496 } 497 498 func testAccCheckInstanceDestroyWithProviders(providers *[]*schema.Provider) resource.TestCheckFunc { 499 return func(s *terraform.State) error { 500 for _, provider := range *providers { 501 if provider.Meta() == nil { 502 continue 503 } 504 if err := testAccCheckInstanceDestroyWithProvider(s, provider); err != nil { 505 return err 506 } 507 } 508 return nil 509 } 510 } 511 512 func testAccCheckInstanceDestroyWithProvider(s *terraform.State, provider *schema.Provider) error { 513 conn := provider.Meta().(*AWSClient).ec2conn 514 515 for _, rs := range s.RootModule().Resources { 516 if rs.Type != "aws_instance" { 517 continue 518 } 519 520 // Try to find the resource 521 var err error 522 resp, err := conn.DescribeInstances(&ec2.DescribeInstancesInput{ 523 InstanceIDs: []*string{aws.String(rs.Primary.ID)}, 524 }) 525 if err == nil { 526 if len(resp.Reservations) > 0 { 527 return fmt.Errorf("still exist.") 528 } 529 530 return nil 531 } 532 533 // Verify the error is what we want 534 ec2err, ok := err.(awserr.Error) 535 if !ok { 536 return err 537 } 538 if ec2err.Code() != "InvalidInstanceID.NotFound" { 539 return err 540 } 541 } 542 543 return nil 544 } 545 546 func testAccCheckInstanceExists(n string, i *ec2.Instance) resource.TestCheckFunc { 547 providers := []*schema.Provider{testAccProvider} 548 return testAccCheckInstanceExistsWithProviders(n, i, &providers) 549 } 550 551 func testAccCheckInstanceExistsWithProviders(n string, i *ec2.Instance, providers *[]*schema.Provider) resource.TestCheckFunc { 552 return func(s *terraform.State) error { 553 rs, ok := s.RootModule().Resources[n] 554 if !ok { 555 return fmt.Errorf("Not found: %s", n) 556 } 557 558 if rs.Primary.ID == "" { 559 return fmt.Errorf("No ID is set") 560 } 561 for _, provider := range *providers { 562 conn := provider.Meta().(*AWSClient).ec2conn 563 resp, err := conn.DescribeInstances(&ec2.DescribeInstancesInput{ 564 InstanceIDs: []*string{aws.String(rs.Primary.ID)}, 565 }) 566 if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "InvalidInstanceID.NotFound" { 567 continue 568 } 569 if err != nil { 570 return err 571 } 572 573 if len(resp.Reservations) > 0 { 574 *i = *resp.Reservations[0].Instances[0] 575 return nil 576 } 577 } 578 579 return fmt.Errorf("Instance not found") 580 } 581 } 582 583 func TestInstanceTenancySchema(t *testing.T) { 584 actualSchema := resourceAwsInstance().Schema["tenancy"] 585 expectedSchema := &schema.Schema{ 586 Type: schema.TypeString, 587 Optional: true, 588 Computed: true, 589 ForceNew: true, 590 } 591 if !reflect.DeepEqual(actualSchema, expectedSchema) { 592 t.Fatalf( 593 "Got:\n\n%#v\n\nExpected:\n\n%#v\n", 594 actualSchema, 595 expectedSchema) 596 } 597 } 598 599 const testAccInstanceConfig_pre = ` 600 resource "aws_security_group" "tf_test_foo" { 601 name = "tf_test_foo" 602 description = "foo" 603 604 ingress { 605 protocol = "icmp" 606 from_port = -1 607 to_port = -1 608 cidr_blocks = ["0.0.0.0/0"] 609 } 610 } 611 ` 612 613 const testAccInstanceConfig = ` 614 resource "aws_security_group" "tf_test_foo" { 615 name = "tf_test_foo" 616 description = "foo" 617 618 ingress { 619 protocol = "icmp" 620 from_port = -1 621 to_port = -1 622 cidr_blocks = ["0.0.0.0/0"] 623 } 624 } 625 626 resource "aws_instance" "foo" { 627 # us-west-2 628 ami = "ami-4fccb37f" 629 availability_zone = "us-west-2a" 630 631 instance_type = "m1.small" 632 security_groups = ["${aws_security_group.tf_test_foo.name}"] 633 user_data = "foo:-with-character's" 634 } 635 ` 636 637 const testAccInstanceConfigBlockDevices = ` 638 resource "aws_instance" "foo" { 639 # us-west-2 640 ami = "ami-55a7ea65" 641 642 # In order to attach an encrypted volume to an instance you need to have an 643 # m3.medium or larger. See "Supported Instance Types" in: 644 # http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EBSEncryption.html 645 instance_type = "m3.medium" 646 647 root_block_device { 648 volume_type = "gp2" 649 volume_size = 11 650 } 651 ebs_block_device { 652 device_name = "/dev/sdb" 653 volume_size = 9 654 } 655 ebs_block_device { 656 device_name = "/dev/sdc" 657 volume_size = 10 658 volume_type = "io1" 659 iops = 100 660 } 661 662 # Encrypted ebs block device 663 ebs_block_device { 664 device_name = "/dev/sdd" 665 volume_size = 12 666 encrypted = true 667 } 668 669 ephemeral_block_device { 670 device_name = "/dev/sde" 671 virtual_name = "ephemeral0" 672 } 673 } 674 ` 675 676 const testAccInstanceConfigSourceDestEnable = ` 677 resource "aws_vpc" "foo" { 678 cidr_block = "10.1.0.0/16" 679 } 680 681 resource "aws_subnet" "foo" { 682 cidr_block = "10.1.1.0/24" 683 vpc_id = "${aws_vpc.foo.id}" 684 } 685 686 resource "aws_instance" "foo" { 687 # us-west-2 688 ami = "ami-4fccb37f" 689 instance_type = "m1.small" 690 subnet_id = "${aws_subnet.foo.id}" 691 } 692 ` 693 694 const testAccInstanceConfigSourceDestDisable = ` 695 resource "aws_vpc" "foo" { 696 cidr_block = "10.1.0.0/16" 697 } 698 699 resource "aws_subnet" "foo" { 700 cidr_block = "10.1.1.0/24" 701 vpc_id = "${aws_vpc.foo.id}" 702 } 703 704 resource "aws_instance" "foo" { 705 # us-west-2 706 ami = "ami-4fccb37f" 707 instance_type = "m1.small" 708 subnet_id = "${aws_subnet.foo.id}" 709 source_dest_check = false 710 } 711 ` 712 713 func testAccInstanceConfigDisableAPITermination(val bool) string { 714 return fmt.Sprintf(` 715 resource "aws_vpc" "foo" { 716 cidr_block = "10.1.0.0/16" 717 } 718 719 resource "aws_subnet" "foo" { 720 cidr_block = "10.1.1.0/24" 721 vpc_id = "${aws_vpc.foo.id}" 722 } 723 724 resource "aws_instance" "foo" { 725 # us-west-2 726 ami = "ami-4fccb37f" 727 instance_type = "m1.small" 728 subnet_id = "${aws_subnet.foo.id}" 729 disable_api_termination = %t 730 } 731 `, val) 732 } 733 734 const testAccInstanceConfigVPC = ` 735 resource "aws_vpc" "foo" { 736 cidr_block = "10.1.0.0/16" 737 } 738 739 resource "aws_subnet" "foo" { 740 cidr_block = "10.1.1.0/24" 741 vpc_id = "${aws_vpc.foo.id}" 742 } 743 744 resource "aws_instance" "foo" { 745 # us-west-2 746 ami = "ami-4fccb37f" 747 instance_type = "m1.small" 748 subnet_id = "${aws_subnet.foo.id}" 749 associate_public_ip_address = true 750 tenancy = "dedicated" 751 } 752 ` 753 754 const testAccInstanceConfigMultipleRegions = ` 755 provider "aws" { 756 alias = "west" 757 region = "us-west-2" 758 } 759 760 provider "aws" { 761 alias = "east" 762 region = "us-east-1" 763 } 764 765 resource "aws_instance" "foo" { 766 # us-west-2 767 provider = "aws.west" 768 ami = "ami-4fccb37f" 769 instance_type = "m1.small" 770 } 771 772 resource "aws_instance" "bar" { 773 # us-east-1 774 provider = "aws.east" 775 ami = "ami-8c6ea9e4" 776 instance_type = "m1.small" 777 } 778 ` 779 780 const testAccCheckInstanceConfigTags = ` 781 resource "aws_instance" "foo" { 782 ami = "ami-4fccb37f" 783 instance_type = "m1.small" 784 tags { 785 foo = "bar" 786 } 787 } 788 ` 789 790 const testAccCheckInstanceConfigTagsUpdate = ` 791 resource "aws_instance" "foo" { 792 ami = "ami-4fccb37f" 793 instance_type = "m1.small" 794 tags { 795 bar = "baz" 796 } 797 } 798 ` 799 800 const testAccInstanceConfigPrivateIP = ` 801 resource "aws_vpc" "foo" { 802 cidr_block = "10.1.0.0/16" 803 } 804 805 resource "aws_subnet" "foo" { 806 cidr_block = "10.1.1.0/24" 807 vpc_id = "${aws_vpc.foo.id}" 808 } 809 810 resource "aws_instance" "foo" { 811 ami = "ami-c5eabbf5" 812 instance_type = "t2.micro" 813 subnet_id = "${aws_subnet.foo.id}" 814 private_ip = "10.1.1.42" 815 } 816 ` 817 818 const testAccInstanceConfigAssociatePublicIPAndPrivateIP = ` 819 resource "aws_vpc" "foo" { 820 cidr_block = "10.1.0.0/16" 821 } 822 823 resource "aws_subnet" "foo" { 824 cidr_block = "10.1.1.0/24" 825 vpc_id = "${aws_vpc.foo.id}" 826 } 827 828 resource "aws_instance" "foo" { 829 ami = "ami-c5eabbf5" 830 instance_type = "t2.micro" 831 subnet_id = "${aws_subnet.foo.id}" 832 associate_public_ip_address = true 833 private_ip = "10.1.1.42" 834 } 835 ` 836 837 const testAccInstanceNetworkInstanceSecurityGroups = ` 838 resource "aws_internet_gateway" "gw" { 839 vpc_id = "${aws_vpc.foo.id}" 840 } 841 842 resource "aws_vpc" "foo" { 843 cidr_block = "10.1.0.0/16" 844 tags { 845 Name = "tf-network-test" 846 } 847 } 848 849 resource "aws_security_group" "tf_test_foo" { 850 name = "tf_test_foo" 851 description = "foo" 852 vpc_id="${aws_vpc.foo.id}" 853 854 ingress { 855 protocol = "icmp" 856 from_port = -1 857 to_port = -1 858 cidr_blocks = ["0.0.0.0/0"] 859 } 860 } 861 862 resource "aws_subnet" "foo" { 863 cidr_block = "10.1.1.0/24" 864 vpc_id = "${aws_vpc.foo.id}" 865 } 866 867 resource "aws_instance" "foo_instance" { 868 ami = "ami-21f78e11" 869 instance_type = "t1.micro" 870 security_groups = ["${aws_security_group.tf_test_foo.id}"] 871 subnet_id = "${aws_subnet.foo.id}" 872 associate_public_ip_address = true 873 depends_on = ["aws_internet_gateway.gw"] 874 } 875 876 resource "aws_eip" "foo_eip" { 877 instance = "${aws_instance.foo_instance.id}" 878 vpc = true 879 depends_on = ["aws_internet_gateway.gw"] 880 } 881 ` 882 883 const testAccInstanceNetworkInstanceVPCSecurityGroupIDs = ` 884 resource "aws_internet_gateway" "gw" { 885 vpc_id = "${aws_vpc.foo.id}" 886 } 887 888 resource "aws_vpc" "foo" { 889 cidr_block = "10.1.0.0/16" 890 tags { 891 Name = "tf-network-test" 892 } 893 } 894 895 resource "aws_security_group" "tf_test_foo" { 896 name = "tf_test_foo" 897 description = "foo" 898 vpc_id="${aws_vpc.foo.id}" 899 900 ingress { 901 protocol = "icmp" 902 from_port = -1 903 to_port = -1 904 cidr_blocks = ["0.0.0.0/0"] 905 } 906 } 907 908 resource "aws_subnet" "foo" { 909 cidr_block = "10.1.1.0/24" 910 vpc_id = "${aws_vpc.foo.id}" 911 } 912 913 resource "aws_instance" "foo_instance" { 914 ami = "ami-21f78e11" 915 instance_type = "t1.micro" 916 vpc_security_group_ids = ["${aws_security_group.tf_test_foo.id}"] 917 subnet_id = "${aws_subnet.foo.id}" 918 depends_on = ["aws_internet_gateway.gw"] 919 } 920 921 resource "aws_eip" "foo_eip" { 922 instance = "${aws_instance.foo_instance.id}" 923 vpc = true 924 depends_on = ["aws_internet_gateway.gw"] 925 } 926 ` 927 const testAccInstanceConfigKeyPair = ` 928 provider "aws" { 929 region = "us-east-1" 930 } 931 932 resource "aws_key_pair" "debugging" { 933 key_name = "tmp-key" 934 public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD3F6tyPEFEzV0LX3X8BsXdMsQz1x2cEikKDEY0aIj41qgxMCP/iteneqXSIFZBp5vizPvaoIR3Um9xK7PGoW8giupGn+EPuxIA4cDM4vzOqOkiMPhz5XK0whEjkVzTo4+S0puvDZuwIsdiW9mxhJc7tgBNL0cYlWSYVkz4G/fslNfRPW5mYAM49f4fhtxPb5ok4Q2Lg9dPKVHO/Bgeu5woMc7RY0p1ej6D4CKFE6lymSDJpW0YHX/wqE9+cfEauh7xZcG0q9t2ta6F6fmX0agvpFyZo8aFbXeUBr7osSCJNgvavWbM/06niWrOvYX2xwWdhXmXSrbX8ZbabVohBK41 phodgson@thoughtworks.com" 935 } 936 937 resource "aws_instance" "foo" { 938 ami = "ami-408c7f28" 939 instance_type = "t1.micro" 940 key_name = "${aws_key_pair.debugging.key_name}" 941 } 942 `