EC2 Launch Template doesn't start Spot Instance (but works for on-demand instance)
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" }
}
}
```