EC2 DescribeInstanceTypes: 9 hardcoded x86_64 types, and the Terraform data source cannot read any of them
Hi, thanks for fakecloud. DescribeInstanceTypes (v0.51.0) only knows a small fixed list of x86_64 types, and I have a fix.
The problem
DescribeInstanceTypes always returns the same short list (a few t3, m5, c5, r5 and t2 sizes), and every type is x86_64. Other types, such as m7g.large, come back empty. The instance-type filter is ignored.
The items also have no ebsInfo, networkInfo or placementGroupInfo. The Terraform/OpenTofu AWS provider (6.72.0) treats a type without them as not found. So data "aws_ec2_instance_type" fails with "no matching EC2 Instance Type found", even for t3.large and m5.large.
Repro
aws --endpoint-url http://localhost:4566 ec2 describe-instance-types --instance-types m7g.large
# empty InstanceTypes
data "aws_ec2_instance_type" "t" { instance_type = "t3.large" }
# Error: no matching EC2 Instance Type found
Fix
The code already has a full list of instance types in instance_types.rs, but DescribeInstanceTypes does not use it. The patch below makes it use that list:
- The architecture comes from the family name. A
gafter the generation number means Graviton, som7gisarm64, whileg5andg4dnstayx86_64. - It adds cores, threads and minimal
ebsInfo,networkInfoandplacementGroupInfo. - It supports the
instance-type(with wildcards),processor-info.supported-architecture,vcpu-info.default-vcpusandbare-metalfilters.
I checked it on v0.51.0 with the AWS CLI and the provider data source. m7g.large and c7g.xlarge report arm64, and the data source reads m7g.large, t3a.large, t3.large and m5.large.
Two limits, which I left alone:
- Every type reports the same placeholder memory (1,024 MiB), because the list has no memory data.
GetInstanceTypesFromInstanceRequirements(the call that finds types matching vCPU or memory ranges) still uses the old short list.
I'll open a PR with this fix.
Patch
diff --git a/crates/fakecloud-ec2/src/instance_types.rs b/crates/fakecloud-ec2/src/instance_types.rs
index 595b3b1..420e065 100644
--- a/crates/fakecloud-ec2/src/instance_types.rs
+++ b/crates/fakecloud-ec2/src/instance_types.rs
@@ -1452,10 +1452,67 @@ pub(crate) fn default_vcpus(instance_type: &str) -> Option<u32> {
.map(|i| INSTANCE_TYPE_VCPUS[i].1)
}
+/// The CPU architecture of `instance_type`: `arm64` for Graviton (and Apple
+/// silicon `mac2`) families, `x86_64` otherwise.
+///
+/// A family name is `<letters><generation digits><attribute letters>`. AWS puts
+/// a `g` in the attribute letters of every Graviton family (`m7g`, `c6gn`,
+/// `x2gd`, `im4gn`, `is4gen`, `i8ge`, `g5g`, `hpc7g`). The GPU families `g4dn`,
+/// `g5`, `g6` and `gr6` have the `g` in the leading letters, so they stay
+/// `x86_64`. `a1` is the one Graviton family with no `g`.
+pub(crate) fn architecture(instance_type: &str) -> &'static str {
+ let family = instance_type.split('.').next().unwrap_or(instance_type);
+ let attributes = family
+ .trim_start_matches(|c: char| c.is_ascii_lowercase())
+ .trim_start_matches(|c: char| c.is_ascii_digit());
+ if family == "a1" || family.starts_with("mac2") || attributes.contains('g') {
+ "arm64"
+ } else {
+ "x86_64"
+ }
+}
+
+/// Threads per core: 1 for Graviton, single-vCPU types and the AMD families
+/// that turn off SMT, 2 for the rest.
+pub(crate) fn default_threads_per_core(instance_type: &str, vcpus: u32) -> u32 {
+ let family = instance_type.split('.').next().unwrap_or(instance_type);
+ let no_smt = ["c7a", "c8a", "m7a", "m8a", "r7a", "r8a", "hpc6a", "hpc7a"];
+ if vcpus < 2 || architecture(instance_type) == "arm64" || no_smt.contains(&family) {
+ 1
+ } else {
+ 2
+ }
+}
+
#[cfg(test)]
mod tests {
use super::*;
+ #[test]
+ fn architecture_follows_the_graviton_naming_rule() {
+ for t in [
+ "a1.large", "t4g.micro", "m7g.large", "c6gn.xlarge", "x2gd.metal", "im4gn.large",
+ "is4gen.large", "i8ge.large", "g5g.xlarge", "hpc7g.16xlarge", "mac2.metal",
+ ] {
+ assert_eq!(architecture(t), "arm64", "{t}");
+ }
+ for t in [
+ "t3a.large", "m5.large", "g4dn.xlarge", "g5.xlarge", "g6e.xlarge", "gr6.4xlarge",
+ "i3en.large", "p4d.24xlarge", "inf2.xlarge", "mac1.metal", "u-6tb1.112xlarge",
+ "m6idn.large", "c6in.large",
+ ] {
+ assert_eq!(architecture(t), "x86_64", "{t}");
+ }
+ }
+
+ #[test]
+ fn threads_per_core() {
+ assert_eq!(default_threads_per_core("t2.micro", 1), 1);
+ assert_eq!(default_threads_per_core("m7g.large", 2), 1);
+ assert_eq!(default_threads_per_core("c7a.large", 2), 1);
+ assert_eq!(default_threads_per_core("t3.large", 2), 2);
+ }
+
#[test]
fn table_is_sorted_and_unique() {
assert!(INSTANCE_TYPE_VCPUS.windows(2).all(|w| w[0].0 < w[1].0));
diff --git a/crates/fakecloud-ec2/src/service/instance.rs b/crates/fakecloud-ec2/src/service/instance.rs
index 252faa8..862bc29 100644
--- a/crates/fakecloud-ec2/src/service/instance.rs
+++ b/crates/fakecloud-ec2/src/service/instance.rs
@@ -2420,23 +2420,52 @@ pub(crate) fn describe_instance_status(
))
}
+fn instance_type_matches_filters(instance_type: &str, filters: &[Filter]) -> bool {
+ filters.iter().all(|f| {
+ let actual = match f.name.as_str() {
+ "instance-type" => instance_type.to_string(),
+ "processor-info.supported-architecture" => {
+ crate::instance_types::architecture(instance_type).to_string()
+ }
+ "vcpu-info.default-vcpus" => crate::instance_types::default_vcpus(instance_type)
+ .unwrap_or(2)
+ .to_string(),
+ "bare-metal" => instance_type.contains(".metal").to_string(),
+ // Attributes the table does not model are not filtered on.
+ _ => return true,
+ };
+ f.values
+ .iter()
+ .any(|v| crate::service_helpers::filter_value_matches(v, &actual))
+ })
+}
+
+fn instance_type_item(instance_type: &str) -> String {
+ let vcpus = crate::instance_types::default_vcpus(instance_type).unwrap_or(2);
+ let threads = crate::instance_types::default_threads_per_core(instance_type, vcpus);
+ let bare_metal = instance_type.contains(".metal");
+ let hypervisor = if bare_metal {
+ ""
+ } else {
+ "<hypervisor>nitro</hypervisor>"
+ };
+ format!(
+ "{}<currentGeneration>true</currentGeneration><bareMetal>{bare_metal}</bareMetal> {hypervisor}<instanceStorageSupported>false</instanceStorageSupported> <supportedUsageClasses><item>on-demand</item><item>spot</item></supportedUsageClasses> <processorInfo><supportedArchitectures><item>{}</item></supportedArchitectures></processorInfo> <vCpuInfo><defaultVCpus>{vcpus}</defaultVCpus><defaultCores>{}</defaultCores> <defaultThreadsPerCore>{threads}</defaultThreadsPerCore></vCpuInfo> <memoryInfo><sizeInMiB>1024</sizeInMiB></memoryInfo> <ebsInfo><ebsOptimizedSupport>default</ebsOptimizedSupport><encryptionSupport>supported</encryptionSupport><nvmeSupport>required</nvmeSupport></ebsInfo> <networkInfo><networkPerformance>Up to 5 Gigabit</networkPerformance><maximumNetworkInterfaces>3</maximumNetworkInterfaces><maximumNetworkCards>1</maximumNetworkCards><defaultNetworkCardIndex>0</defaultNetworkCardIndex><ipv4AddressesPerInterface>10</ipv4AddressesPerInterface><ipv6AddressesPerInterface>10</ipv6AddressesPerInterface><ipv6Supported>true</ipv6Supported><enaSupport>required</enaSupport><efaSupported>false</efaSupported></networkInfo> <placementGroupInfo><supportedStrategies><item>cluster</item><item>partition</item><item>spread</item></supportedStrategies></placementGroupInfo> <supportedVirtualizationTypes><item>hvm</item></supportedVirtualizationTypes>",
+ ec2_elem("instanceType", instance_type),
+ crate::instance_types::architecture(instance_type),
+ (vcpus / threads).max(1),
+ )
+}
+
fn instance_type_items(req: &AwsRequest) -> Vec<String> {
let wanted = indexed_list(&req.query_params, "InstanceType");
- INSTANCE_TYPES
+ let filters = parse_filters(&req.query_params);
+ crate::instance_types::INSTANCE_TYPE_VCPUS
.iter()
- .filter(|t| wanted.is_empty() || wanted.iter().any(|w| w == *t))
- .map(|t| {
- format!(
- "{}<currentGeneration>true</currentGeneration><bareMetal>false</bareMetal>\
- <hypervisor>nitro</hypervisor><instanceStorageSupported>false</instanceStorageSupported>\
- <processorInfo><supportedArchitectures><item>x86_64</item></supportedArchitectures></processorInfo>\
- <vCpuInfo><defaultVCpus>{}</defaultVCpus></vCpuInfo>\
- <memoryInfo><sizeInMiB>1024</sizeInMiB></memoryInfo>\
- <supportedVirtualizationTypes><item>hvm</item></supportedVirtualizationTypes>",
- ec2_elem("instanceType", t),
- crate::instance_types::default_vcpus(t).unwrap_or(2),
- )
- })
+ .map(|(t, _)| *t)
+ .filter(|t| wanted.is_empty() || wanted.iter().any(|w| w == t))
+ .filter(|t| instance_type_matches_filters(t, &filters))
+ .map(instance_type_item)
.collect()
}
diff --git a/crates/fakecloud-ec2/src/service/resource_quotas_tests.rs b/crates/fakecloud-ec2/src/service/resource_quotas_tests.rs
index 6af4886..145580f 100644
--- a/crates/fakecloud-ec2/src/service/resource_quotas_tests.rs
+++ b/crates/fakecloud-ec2/src/service/resource_quotas_tests.rs
@@ -676,6 +676,30 @@ fn describe_instance_types_reports_real_vcpus() {
assert!(out.contains("<defaultVCpus>1</defaultVCpus>"), "{out}");
}
+#[test]
+fn describe_instance_types_serves_the_full_catalog_and_filters() {
+ let describe = |params: &[(&str, &str)]| {
+ body(instance::describe_instance_types(
+ &Ec2Service::new(),
+ &req("DescribeInstanceTypes", params),
+ ))
+ };
+ let graviton = describe(&[("InstanceType.1", "m7g.large")]);
+ assert!(graviton.contains("<item>arm64</item>"), "{graviton}");
+ assert!(graviton.contains("<defaultThreadsPerCore>1</defaultThreadsPerCore>"));
+ let amd = describe(&[("InstanceType.1", "t3a.large")]);
+ assert!(amd.contains("<item>x86_64</item>"), "{amd}");
+ assert!(amd.contains("<defaultCores>1</defaultCores>"));
+ let filtered = describe(&[
+ ("Filter.1.Name", "instance-type"),
+ ("Filter.1.Value.1", "m7g.*"),
+ ("Filter.2.Name", "processor-info.supported-architecture"),
+ ("Filter.2.Value.1", "arm64"),
+ ]);
+ assert!(filtered.contains("<instanceType>m7g.large</instanceType>"));
+ assert!(!filtered.contains("<instanceType>m5.large</instanceType>"));
+}
+
#[test]
fn an_account_ec2_has_not_stored_reports_its_default_network() {
let usage = Ec2QuotaUsage::new(Ec2Service::new().state.clone());