Skip to content

Commit

Permalink
fix: instance type and architecture incompatiblity
Browse files Browse the repository at this point in the history
  • Loading branch information
oycyc committed Aug 4, 2024
1 parent 5b6d994 commit b85d0db
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 1 deletion.
2 changes: 1 addition & 1 deletion data.tf
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ data "aws_ami" "amazon_linux_2023" {

filter {
name = "architecture"
values = ["x86_64"]
values = [var.architecture]
}

filter {
Expand Down
22 changes: 22 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
locals {
# Validate that only 'arm64' architecture is used with 'g' processor instances to ensure compatibility.
# https://docs.aws.amazon.com/ec2/latest/instancetypes/instance-type-names.html
is_instance_compatible = (
# True if does not contain 'g' when architecture is x86_64
(var.architecture == "x86_64" && !can(regex("g", var.instance_type))) ||
# True if contains 'g' when architecture is arm64
(var.architecture == "arm64" && can(regex("g", var.instance_type)))
)
}

resource "null_resource" "validate_instance_type" {
count = local.is_instance_compatible ? 0 : 1

lifecycle {
precondition {
condition = local.is_instance_compatible
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)."
}
}
}

module "role_label" {
source = "cloudposse/label/null"
version = "0.25.0"
Expand Down
6 changes: 6 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ variable "ami" {
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."
}

variable "architecture" {
description = "The architecture of the AMI (e.g., x86_64, arm64)"
type = string
default = "arm64"
}

variable "user_data" {
default = <<EOT
#!/bin/bash
Expand Down
4 changes: 4 additions & 0 deletions versions.tf
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,9 @@ terraform {
source = "hashicorp/time"
version = ">= 0.7"
}
null = {
source = "hashicorp/null"
version = ">= 3.2"
}
}
}

0 comments on commit b85d0db

Please sign in to comment.