EC2 Launch Template doesn't start Spot Instance (but works for on-demand instance)

0

My EC2 launch template doesn't work when using it to launch a Spot instance. The launch template is set to launch a c5.xlarge instance associated to a pre-existing Elastic Network Interface @ index 0.

When launching a spot instance, I receive the following cryptic message, and the spot request fails:

c5.xlarge, ami-b2b55cd5, Linux/UNIX: A network interface may not specify both a network interface ID and a subnet

First off, how can a network interface specify a network interface id? I believe this error means to say "a spot instance may not specify both a network interface ID and a subnet", but I can't be sure. Secondly, my launch template doesn't specify a subnet directly - it only specifies a network interface ID, which in turn specifies the subnet.

As a troubleshooting step, I've tried launching an on-demand EC2 instance directly using the same launch template, via "Launch Templates -> Actions -> Launch Instance from Template" - when I do this, the EC2 instance launches successfully.

I've been able to reproduce this error consistently for over 9 months now, and am surprised that no one else has brought this up. What gives?

Here is my Spot config:

		"MySpotFleet" : {
	        "Type" : "AWS::EC2::SpotFleet",
	        "Properties" : {
	        	"SpotFleetRequestConfigData" : {
					"AllocationStrategy" : "lowestPrice",
					"IamFleetRole" : {"Fn::GetAtt" : ["MyIAMFleetRole", "Arn"]},
					"InstanceInterruptionBehavior" : "stop",
				    "LaunchTemplateConfigs": [
				        {
				            "LaunchTemplateSpecification": {
				                "LaunchTemplateId": { "Ref" : "MyLaunchTemplate" },
				                "Version": { "Fn::GetAtt" : [ "MyLaunchTemplate", "LatestVersionNumber" ]}
				            }
				        }
				    ],
					"ReplaceUnhealthyInstances" : false,
					"SpotMaxTotalPrice" : "5.01",
					"SpotPrice" : "5.01",
					"TargetCapacity" : 1,
					"TerminateInstancesWithExpiration" : false,
					"Type" : "maintain",
					"ValidFrom" : "2021-01-01T00:00:00Z",
					"ValidUntil" : "2050-12-31T23:59:59Z"
	        	}
        	},
            "DependsOn": [
                "MyLaunchTemplate" 
            ]
	    }

If I replace the above Spot config with this on-demand instance config, it works:

		"MyInstance" : {
	        "Type" : "AWS::EC2::Instance",
	        "Properties" : {
				"LaunchTemplate" : {
	                "LaunchTemplateId": { "Ref" : "MyLaunchTemplate" },
	                "Version": { "Fn::GetAtt" : [ "MyLaunchTemplate", "LatestVersionNumber" ]}
				}
        	},
            "DependsOn": [
                "MyLaunchTemplate" 
            ]
	    }

If it helps, here is my Launch Template:

		"MyLaunchTemplate" : {
	        "Type" : "AWS::EC2::LaunchTemplate",
	        "Properties" : {
				"LaunchTemplateName":"MyLaunchTemplate",
				"LaunchTemplateData":{
					"IamInstanceProfile" : {
						"Arn" : {
							"Fn::GetAtt" : ["MyEC2IAMInstanceProfile", "Arn"]
						}
					},
					"ImageId" : "ami-b2b55cd5",
					"InstanceType": "c5.xlarge",
					"NetworkInterfaces" : [
						{
         					"NetworkInterfaceId" : {"Ref" : "MyENI00"}, 
         					"DeviceIndex" : "0"
     					}
					],
					"InstanceInitiatedShutdownBehavior" : "stop",
					"KeyName" : "my-keypair"
	        }
	    }

And the ENI in question:

        "MyENI00": {
            "Type": "AWS::EC2::NetworkInterface",
            "Properties": {
                "Description" : "MyENI00",
                "GroupSet" : [ 
                	{"Ref" : "MySecurityGroup"} 
            	],
                "PrivateIpAddresses": [ 
					{
						"Primary" : true,
						"PrivateIpAddress" : "172.16.0.100"
					},
					{
						"Primary" : false,
						"PrivateIpAddress" : "172.16.0.101"
					}
                ],
                "SourceDestCheck": false,
                "SubnetId": { "Ref" : "MySubnet" }
            }
        }
  • What You are doing is Launching a Spot Fleet = Which can be 1 or more than 1 Spot Instances based on Config. Have u tried launching a Single Spot Instance ( Not Fleet) using Run Spot request ? What is happening here is Spot Fleet trying to check all available AZs and its conflicting with conditions like NetworkInterfaces which are. Subnet Based ( AZ bound )

asked 2 years ago653 views
1 Answer
0

Hello,

I understand that you are trying to launch a spot instance with the given config but are receiving the error: "A network interface may not specify both a network interface ID and a subnet".

Currently it is not possible to include a networkinterfaceId on a spotfleet. CLI documentation: https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ec2/request-spot-fleet.html

After analyzing your spot config I noticed that your "AllocationStrategy" is set to "lowestprice". This leads to spot instances being launched in spot instance pools with the lowest price in the default subnet of an AZ. Your spot instance is attempting to launch in the default subnet of an AZ, but according to your launch template the ENI to be attached to the instance has another subnet ID specified. This causes both the subnet ID of the spot instance and the given subnet ID of the ENI to be used when launching the instance.

This process is not followed when launching an on-demand instance which explains why it works during your troubleshooting session.

Here are examples of spot fleet configurations: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/spot-fleet-examples.html

answered 2 years ago
AWS
SUPPORT ENGINEER
reviewed 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions