Skip to content

Commit a4f6606

Browse files
committed
fix: instance type and architecture incompatiblity + tests
1 parent b85d0db commit a4f6606

File tree

2 files changed

+109
-4
lines changed

2 files changed

+109
-4
lines changed

main.tf

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
locals {
2+
instance_type_chars = split("", var.instance_type)
23
# Validate that only 'arm64' architecture is used with 'g' processor instances to ensure compatibility.
34
# https://docs.aws.amazon.com/ec2/latest/instancetypes/instance-type-names.html
45
is_instance_compatible = (
5-
# True if does not contain 'g' when architecture is x86_64
6-
(var.architecture == "x86_64" && !can(regex("g", var.instance_type))) ||
7-
# True if contains 'g' when architecture is arm64
8-
(var.architecture == "arm64" && can(regex("g", var.instance_type)))
6+
# True if does not contain 'g' in the third position when architecture is x86_64
7+
(var.architecture == "x86_64" && element(local.instance_type_chars, 2) != "g") ||
8+
# True if contains 'g' in the third position when architecture is arm64
9+
(var.architecture == "arm64" && element(local.instance_type_chars, 2) == "g")
910
)
1011
}
1112

tests/main.tftest.hcl

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
variables {
2+
vpc_id = "vpc-12345678"
3+
subnet_ids = ["subnet-12345678", "subnet-87654321"]
4+
stage = "test"
5+
namespace = "mp"
6+
name = "ssm-agent"
7+
region = "us-east-1"
8+
availability_zones = ["us-east-1a"]
9+
nat_gateway_enabled = true
10+
ipv6_enabled = true
11+
}
12+
13+
### TESTING INSTANCE and ARCHITECTURE COMPATIBILITY ###
14+
# https://docs.aws.amazon.com/ec2/latest/instancetypes/instance-type-names.html
15+
# https://aws.amazon.com/ec2/instance-types/
16+
17+
# Test valid x86_64 instance type
18+
run "valid_x86_64_instance" {
19+
command = plan
20+
21+
variables {
22+
instance_type = "t3.micro"
23+
architecture = "x86_64"
24+
}
25+
26+
assert {
27+
condition = local.is_instance_compatible
28+
error_message = "Expected instance type t3.micro to be compatible with x86_64 architecture"
29+
}
30+
}
31+
32+
# Test valid arm64 instance type
33+
run "valid_arm64_instance" {
34+
command = plan
35+
36+
variables {
37+
instance_type = "t4g.micro"
38+
architecture = "arm64"
39+
}
40+
41+
assert {
42+
condition = local.is_instance_compatible
43+
error_message = "Expected instance type t4g.micro to be compatible with arm64 architecture"
44+
}
45+
}
46+
47+
# Test invalid x86_64 instance type (using arm64 instance type)
48+
run "invalid_x86_64_instance" {
49+
command = plan
50+
51+
variables {
52+
instance_type = "t4g.micro"
53+
architecture = "x86_64"
54+
}
55+
56+
expect_failures = [
57+
null_resource.validate_instance_type
58+
]
59+
}
60+
61+
# Test invalid arm64 instance type (using x86_64 instance type)
62+
run "invalid_arm64_instance" {
63+
command = plan
64+
65+
variables {
66+
instance_type = "t3.micro"
67+
architecture = "arm64"
68+
}
69+
70+
expect_failures = [
71+
null_resource.validate_instance_type
72+
]
73+
}
74+
75+
# Test edge case, where the 'g' is defined as the instance family rather than the processor family
76+
# It has 'g' in the name, but it's still an x86_64 instance type because the 'g' is the instance family
77+
run "graphics_instance_arm_incompatiblity_edge_case" {
78+
command = plan
79+
80+
variables {
81+
instance_type = "g3s.xlarge"
82+
architecture = "arm64"
83+
}
84+
85+
expect_failures = [
86+
null_resource.validate_instance_type
87+
]
88+
}
89+
90+
# Test edge case, where the 'g' is defined as the instance family rather than the processor family
91+
# It has 'g' in the name, but it still is compatible with x86_64 since the 'g' is the instance family
92+
run "graphics_instance_x86_compatibility_edge_case" {
93+
command = plan
94+
95+
variables {
96+
instance_type = "g4dn.xlarge"
97+
architecture = "x86_64"
98+
}
99+
100+
assert {
101+
condition = local.is_instance_compatible
102+
error_message = "Expected instance type g3s.xlarge to be compatible with x86_64 architecture"
103+
}
104+
}

0 commit comments

Comments
 (0)