diff --git a/builtin/providers/aws/resource_aws_instance.go b/builtin/providers/aws/resource_aws_instance.go index 65e348d34..e5d4a128f 100644 --- a/builtin/providers/aws/resource_aws_instance.go +++ b/builtin/providers/aws/resource_aws_instance.go @@ -128,11 +128,20 @@ func resourceAwsInstance() *schema.Resource { Computed: true, }, + // TODO: Deprecate me "network_interface_id": { Type: schema.TypeString, Computed: true, }, + "primary_network_interface": { + ConflictsWith: []string{"associate_public_ip_address", "subnet_id", "private_ip", "vpc_security_group_ids", "security_groups", "ipv6_addresses", "ipv6_address_count"}, + Type: schema.TypeString, + Optional: true, + ForceNew: true, + Computed: true, + }, + "public_ip": { Type: schema.TypeString, Computed: true, @@ -533,7 +542,8 @@ func resourceAwsInstanceRead(d *schema.ResourceData, meta interface{}) error { for _, ni := range instance.NetworkInterfaces { if *ni.Attachment.DeviceIndex == 0 { d.Set("subnet_id", ni.SubnetId) - d.Set("network_interface_id", ni.NetworkInterfaceId) + d.Set("network_interface_id", ni.NetworkInterfaceId) // TODO: Deprecate me + d.Set("primary_network_interface", ni.NetworkInterfaceId) d.Set("associate_public_ip_address", ni.Association != nil) d.Set("ipv6_address_count", len(ni.Ipv6Addresses)) @@ -544,7 +554,8 @@ func resourceAwsInstanceRead(d *schema.ResourceData, meta interface{}) error { } } else { d.Set("subnet_id", instance.SubnetId) - d.Set("network_interface_id", "") + d.Set("network_interface_id", "") // TODO: Deprecate me + d.Set("primary_network_interface", "") } if err := d.Set("ipv6_addresses", ipv6Addresses); err != nil { @@ -1260,6 +1271,9 @@ func buildAwsInstanceOpts( } } + // Check if using non-defaullt primary network interface + interface_id, interfaceOk := d.GetOk("primary_network_interface") + if hasSubnet && associatePublicIPAddress { // If we have a non-default VPC / Subnet specified, we can flag // AssociatePublicIpAddress to get a Public IP assigned. By default these are not provided. @@ -1285,6 +1299,13 @@ func buildAwsInstanceOpts( } } + opts.NetworkInterfaces = []*ec2.InstanceNetworkInterfaceSpecification{ni} + } else if interfaceOk { + ni := &ec2.InstanceNetworkInterfaceSpecification{ + DeviceIndex: aws.Int64(int64(0)), + NetworkInterfaceId: aws.String(interface_id.(string)), + } + opts.NetworkInterfaces = []*ec2.InstanceNetworkInterfaceSpecification{ni} } else { if subnetID != "" { diff --git a/builtin/providers/aws/resource_aws_instance_test.go b/builtin/providers/aws/resource_aws_instance_test.go index 2b835f6d7..1de8ac768 100644 --- a/builtin/providers/aws/resource_aws_instance_test.go +++ b/builtin/providers/aws/resource_aws_instance_test.go @@ -877,6 +877,27 @@ func TestAccAWSInstance_changeInstanceType(t *testing.T) { }) } +func TestAccAWSInstance_primaryNetworkInterface(t *testing.T) { + var instance ec2.Instance + var ini ec2.NetworkInterface + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckInstanceDestroy, + Steps: []resource.TestStep{ + { + Config: testAccInstanceConfigPrimaryNetworkInterface, + Check: resource.ComposeTestCheckFunc( + testAccCheckInstanceExists("aws_instance.foo", &instance), + testAccCheckAWSENIExists("aws_network_interface.bar", &ini), + resource.TestCheckResourceAttrSet("aws_instance.foo", "primary_network_interface"), + ), + }, + }, + }) +} + func testAccCheckInstanceNotRecreated(t *testing.T, before, after *ec2.Instance) resource.TestCheckFunc { return func(s *terraform.State) error { @@ -1536,3 +1557,35 @@ resource "aws_instance" "foo" { subnet_id = "${aws_subnet.foo.id}" } ` + +const testAccInstanceConfigPrimaryNetworkInterface = ` +resource "aws_vpc" "foo" { + cidr_block = "172.16.0.0/16" + tags { + Name = "tf-instance-test" + } +} + +resource "aws_subnet" "foo" { + vpc_id = "${aws_vpc.foo.id}" + cidr_block = "172.16.10.0/24" + availability_zone = "us-west-2a" + tags { + Name = "tf-instance-test" + } +} + +resource "aws_network_interface" "bar" { + subnet_id = "${aws_subnet.foo.id}" + private_ips = ["172.16.10.100"] + tags { + Name = "primary_network_interface" + } +} + +resource "aws_instance" "foo" { + ami = "ami-22b9a343" + instance_type = "t2.micro" + primary_network_interface = "${aws_network_interface.bar.id}" +} +`