diff --git a/.spacelift/config.yml b/.spacelift/config.yml index 4bb8788..f28a9b8 100644 --- a/.spacelift/config.yml +++ b/.spacelift/config.yml @@ -1,5 +1,5 @@ version: 1 -module_version: 2.6.4 +module_version: 2.6.5 tests: - name: AMD64-based workerpool project_root: examples/amd64 diff --git a/ami.tf b/ami.tf index 7fd660e..ac9e0be 100644 --- a/ami.tf +++ b/ami.tf @@ -1,7 +1,13 @@ -data "aws_ami" "this" { - most_recent = true - name_regex = "^spacelift-\\d{10}-x86_64$" - owners = ["643313122712"] +# get info about selected instance type +data "aws_ec2_instance_type" "spacelift" { + instance_type = var.ec2_instance_type +} + +# get list of spacelift AMI ids starting with the newest +data "aws_ami_ids" "spacelift" { + sort_ascending = false + name_regex = "^spacelift-\\d{10}-(arm64|x86_64)$" + owners = ["643313122712"] filter { name = "root-device-type" @@ -14,7 +20,16 @@ data "aws_ami" "this" { } filter { - name = "architecture" - values = ["x86_64"] + name = "architecture" + # get ami architecture based on selected instance type (arm64 or x86_64). + values = [for arch in data.aws_ec2_instance_type.spacelift.supported_architectures : arch if can(regex("(arm64|x86_64)$", arch))] + } + + lifecycle { + # Check if Spacelift AMI exists in current region. Spacelift AMIs are not replicated for all regions. + postcondition { + condition = coalesce(var.ami_id, try(self.ids[0], "ami_not_found")) != "ami_not_found" + error_message = "No Spacelift AMI found in current region '${data.aws_region.this.name}'. Use 'var.ami_id' instead to provide an existing Spacelift AMI." + } } } diff --git a/asg.tf b/asg.tf index a80ba80..2cd1e71 100644 --- a/asg.tf +++ b/asg.tf @@ -83,11 +83,12 @@ module "asg" { name = local.base_name iam_instance_profile_arn = aws_iam_instance_profile.this.arn - image_id = var.ami_id != "" ? var.ami_id : data.aws_ami.this.id - instance_type = var.ec2_instance_type - security_groups = var.security_groups - enable_monitoring = var.enable_monitoring - instance_refresh = var.instance_refresh + # if 'var.ami_id' is empty, get AMI from data source. data source lifecycle postcondition fails if no ami exists. + image_id = coalesce(var.ami_id, try(data.aws_ami_ids.spacelift.ids[0], "ami_not_found")) + instance_type = var.ec2_instance_type + security_groups = var.security_groups + enable_monitoring = var.enable_monitoring + instance_refresh = var.instance_refresh block_device_mappings = [ {