Skip to content

Commit 9b3172c

Browse files
authored
Merge pull request #26 from masterpointio/fix/instance-type-compatiblity
fix: instance type and architecture incompatiblity
2 parents 5b6d994 + a4f6606 commit 9b3172c

File tree

5 files changed

+138
-1
lines changed

5 files changed

+138
-1
lines changed

data.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ data "aws_ami" "amazon_linux_2023" {
1313

1414
filter {
1515
name = "architecture"
16-
values = ["x86_64"]
16+
values = [var.architecture]
1717
}
1818

1919
filter {

main.tf

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,26 @@
1+
locals {
2+
instance_type_chars = split("", var.instance_type)
3+
# Validate that only 'arm64' architecture is used with 'g' processor instances to ensure compatibility.
4+
# https://docs.aws.amazon.com/ec2/latest/instancetypes/instance-type-names.html
5+
is_instance_compatible = (
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")
10+
)
11+
}
12+
13+
resource "null_resource" "validate_instance_type" {
14+
count = local.is_instance_compatible ? 0 : 1
15+
16+
lifecycle {
17+
precondition {
18+
condition = local.is_instance_compatible
19+
error_message = "The instance_type must be compatible with the specified architecture. For x86_64, you cannot use instance types with ARM processors (e.g., t3, m5, c5). For arm64, use instance types with 'g' indicating ARM processor (e.g., t4g, c6g, m6g)."
20+
}
21+
}
22+
}
23+
124
module "role_label" {
225
source = "cloudposse/label/null"
326
version = "0.25.0"

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+
}

variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ variable "ami" {
3030
description = "The AMI to use for the SSM Agent EC2 Instance. If not provided, the latest Amazon Linux 2023 AMI will be used. Note: This will update periodically as AWS releases updates to their AL2023 AMI. Pin to a specific AMI if you would like to avoid these updates."
3131
}
3232

33+
variable "architecture" {
34+
description = "The architecture of the AMI (e.g., x86_64, arm64)"
35+
type = string
36+
default = "arm64"
37+
}
38+
3339
variable "user_data" {
3440
default = <<EOT
3541
#!/bin/bash

versions.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,9 @@ terraform {
1111
source = "hashicorp/time"
1212
version = ">= 0.7"
1313
}
14+
null = {
15+
source = "hashicorp/null"
16+
version = ">= 3.2"
17+
}
1418
}
1519
}

0 commit comments

Comments
 (0)