Skip to content

Commit b85d0db

Browse files
committed
fix: instance type and architecture incompatiblity
1 parent 5b6d994 commit b85d0db

File tree

4 files changed

+33
-1
lines changed

4 files changed

+33
-1
lines changed

data.tf

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

+22
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
1+
locals {
2+
# Validate that only 'arm64' architecture is used with 'g' processor instances to ensure compatibility.
3+
# https://docs.aws.amazon.com/ec2/latest/instancetypes/instance-type-names.html
4+
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)))
9+
)
10+
}
11+
12+
resource "null_resource" "validate_instance_type" {
13+
count = local.is_instance_compatible ? 0 : 1
14+
15+
lifecycle {
16+
precondition {
17+
condition = local.is_instance_compatible
18+
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)."
19+
}
20+
}
21+
}
22+
123
module "role_label" {
224
source = "cloudposse/label/null"
325
version = "0.25.0"

variables.tf

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

+4
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)