Skip to content

Commit 3335e7d

Browse files
committed
Add variable to specify desired AMI architecture
* Add `var.ami_architecture` to allow users to specify their desired AMI architecture. * Make `data.aws_ami.this` conditional on whether `var.ami_id` is specified. If it is specified, it's not necessary to lookup the AMI ID. * Updated the example / test case to test with the `var.ami_architecture` variable. Resolves <#139>
1 parent 0a93cee commit 3335e7d

File tree

7 files changed

+22
-27
lines changed

7 files changed

+22
-27
lines changed

.spacelift/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
version: 2
2-
module_version: 3.0.2
2+
module_version: 3.1.0
33

44
tests:
55
- name: AMD64-based workerpool

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ page.
127127

128128
## ARM-based AMI
129129

130-
You can use an ARM-based AMI by setting the `ami_id` variable to an arm64 AMI, and `ec2_instance_type` to an ARM-based instance type (e.g. `t4g.micro`).
130+
You can use an ARM-based AMI by setting the `ami_architecture` to `arm64`, or the `ami_id` variable to an arm64 AMI, and `ec2_instance_type` to an ARM-based instance type (e.g. `t4g.micro`).
131131

132132
We recommend using [Spacelift AMIs](https://github.com/spacelift-io/spacelift-worker-image/releases) because they come with every required tool preinstalled.
133133

ami.tf

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ locals {
66
}
77

88
data "aws_ami" "this" {
9+
count = var.ami_id == "" ? 0 : 1
10+
911
most_recent = true
10-
name_regex = "^spacelift-\\d{10}-x86_64$"
12+
name_regex = "^spacelift-\\d{10}-${var.ami_architecture}$"
1113
owners = [local.ami_owner_ids[data.aws_partition.current.partition]]
1214

1315
filter {
@@ -22,6 +24,6 @@ data "aws_ami" "this" {
2224

2325
filter {
2426
name = "architecture"
25-
values = ["x86_64"]
27+
values = [var.ami_architecture]
2628
}
2729
}

asg.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ module "asg" {
2828
name = local.base_name
2929

3030
iam_instance_profile_arn = aws_iam_instance_profile.this.arn
31-
image_id = var.ami_id != "" ? var.ami_id : data.aws_ami.this.id
31+
image_id = var.ami_id != "" ? var.ami_id : data.aws_ami.this[0].id
3232
instance_type = var.ec2_instance_type
3333
security_groups = var.security_groups
3434
enable_monitoring = var.enable_monitoring

examples/arm64/main.tf

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -36,27 +36,6 @@ data "aws_subnets" "this" {
3636
}
3737
}
3838

39-
data "aws_ami" "this" {
40-
most_recent = true
41-
name_regex = "^spacelift-\\d{10}-arm64$"
42-
owners = ["643313122712"]
43-
44-
filter {
45-
name = "root-device-type"
46-
values = ["ebs"]
47-
}
48-
49-
filter {
50-
name = "virtualization-type"
51-
values = ["hvm"]
52-
}
53-
54-
filter {
55-
name = "architecture"
56-
values = ["arm64"]
57-
}
58-
}
59-
6039
#### Spacelift worker pool ####
6140

6241
module "this" {
@@ -69,7 +48,7 @@ module "this" {
6948
SPACELIFT_TOKEN = "<token-here>"
7049
SPACELIFT_POOL_PRIVATE_KEY = "<private-key-here>"
7150
}
72-
ami_id = data.aws_ami.this.id
51+
ami_architecture = "arm64"
7352
# t4g.micro is just for using the random provider and a few resources.
7453
# If you are using more than a few resources as well as memory intensive providers it's recommended to use a t4g.medium or at least a t4g.small
7554
# https://docs.spacelift.io/concepts/worker-pools#hardware-recommendations

moved.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
moved {
2+
from = data.aws_ami.this
3+
to = data.aws_ami.this[0]
4+
}

variables.tf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@ variable "ami_id" {
44
default = ""
55
}
66

7+
variable "ami_architecture" {
8+
type = string
9+
description = "Architecture of the Spacelift AMI. Currently, only x86_64 or arm64 are supported."
10+
default = "x86_64"
11+
validation {
12+
condition = contains(["x86_64", "arm64"], var.ami_architecture)
13+
error_message = "Currently, only x86_64 or arm64 are supported."
14+
}
15+
}
16+
717
variable "secure_env_vars" {
818
type = map(string)
919
sensitive = true

0 commit comments

Comments
 (0)