Skip to content

Commit c7b2f9a

Browse files
committed
feat: migrate launch template to use SSM for AMI lookup
1 parent 6ac07f5 commit c7b2f9a

File tree

9 files changed

+85
-38
lines changed

9 files changed

+85
-38
lines changed

examples/default/.terraform.lock.hcl

+30-30
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

main.tf

+1
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ module "runners" {
180180
runner_architecture = var.runner_architecture
181181
ami_filter = var.ami_filter
182182
ami_owners = var.ami_owners
183+
ami_id_ssm_parameter_arn = var.ami_id_ssm_parameter_arn
183184
ami_id_ssm_parameter_name = var.ami_id_ssm_parameter_name
184185
ami_kms_key_arn = var.ami_kms_key_arn
185186

modules/multi-runner/runners.tf

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ module "runners" {
2828
runner_architecture = each.value.runner_config.runner_architecture
2929
ami_filter = each.value.runner_config.ami_filter
3030
ami_owners = each.value.runner_config.ami_owners
31+
ami_id_ssm_parameter_arn = each.value.runner_config.ami_id_ssm_parameter_arn
3132
ami_id_ssm_parameter_name = each.value.runner_config.ami_id_ssm_parameter_name
3233
ami_kms_key_arn = each.value.runner_config.ami_kms_key_arn
3334

modules/multi-runner/variables.tf

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
variable "github_app" {
22
description = <<EOF
3-
GitHub app parameters, see your github app.
3+
GitHub app parameters, see your github app.
44
You can optionally create the SSM parameters yourself and provide the ARN and name here, through the `*_ssm` attributes.
5-
If you chose to provide the configuration values directly here,
5+
If you chose to provide the configuration values directly here,
66
please ensure the key is the base64-encoded `.pem` file (the output of `base64 app.private-key.pem`, not the content of `private-key.pem`).
77
Note: the provided SSM parameters arn and name have a precedence over the actual value (i.e `key_base64_ssm` has a precedence over `key_base64` etc).
88
EOF
@@ -67,6 +67,7 @@ variable "multi_runner_config" {
6767
})
6868
ami_filter = optional(map(list(string)), { state = ["available"] })
6969
ami_owners = optional(list(string), ["amazon"])
70+
ami_id_ssm_parameter_arn = optional(string, null)
7071
ami_id_ssm_parameter_name = optional(string, null)
7172
ami_kms_key_arn = optional(string, "")
7273
create_service_linked_role_spot = optional(bool, false)

modules/runners/main.tf

+25-3
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ locals {
3737
"linux" = "${path.module}/templates/start-runner.sh"
3838
}
3939

40-
ami_kms_key_arn = var.ami_kms_key_arn != null ? var.ami_kms_key_arn : ""
41-
ami_filter = merge(local.default_ami[var.runner_os], var.ami_filter)
40+
ami_kms_key_arn = var.ami_kms_key_arn != null ? var.ami_kms_key_arn : ""
41+
ami_filter = merge(local.default_ami[var.runner_os], var.ami_filter)
42+
ami_id_ssm_module_managed = var.ami_id_ssm_parameter_arn == null
4243

4344
enable_job_queued_check = var.enable_job_queued_check == null ? !var.enable_ephemeral_runners : var.enable_job_queued_check
4445

@@ -84,6 +85,27 @@ data "aws_ami" "runner" {
8485
owners = var.ami_owners
8586
}
8687

88+
resource "aws_ssm_parameter" "runner_ami_id" {
89+
count = local.ami_id_ssm_module_managed ? 1 : 0
90+
name = "${var.ssm_paths.root}/${var.ssm_paths.config}/ami_id"
91+
type = "String"
92+
data_type = "aws:ec2:image"
93+
value = data.aws_ami.runner.id
94+
95+
tags = merge(
96+
local.tags,
97+
{
98+
"ghr:ami_name" = data.aws_ami.runner.name
99+
},
100+
{
101+
"ghr:ami_creation_date" = data.aws_ami.runner.creation_date
102+
},
103+
{
104+
"ghr:ami_deprecation_time" = data.aws_ami.runner.deprecation_time
105+
}
106+
)
107+
}
108+
87109
resource "aws_launch_template" "runner" {
88110
name = "${var.prefix}-action-runner"
89111

@@ -140,7 +162,7 @@ resource "aws_launch_template" "runner" {
140162
}
141163

142164
instance_initiated_shutdown_behavior = "terminate"
143-
image_id = data.aws_ami.runner.id
165+
image_id = "resolve:ssm:${local.ami_id_ssm_module_managed ? aws_ssm_parameter.runner_ami_id[0].arn : var.ami_id_ssm_parameter_arn}"
144166
key_name = var.key_name
145167
ebs_optimized = var.ebs_optimized
146168

modules/runners/policies/lambda-scale-up.json

+9
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@
3838
"${ssm_config_path}/*"
3939
]
4040
},
41+
{
42+
"Effect": "Allow",
43+
"Action": [
44+
"ssm:GetParameters"
45+
],
46+
"Resource": [
47+
"${ssm_ami_id_parameter_arn}"
48+
]
49+
},
4150
{
4251
"Effect": "Allow",
4352
"Action": [

modules/runners/scale-up.tf

+1
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ resource "aws_iam_role_policy" "scale_up" {
119119
ssm_config_path = "arn:${var.aws_partition}:ssm:${var.aws_region}:${data.aws_caller_identity.current.account_id}:parameter${var.ssm_paths.root}/${var.ssm_paths.config}"
120120
kms_key_arn = local.kms_key_arn
121121
ami_kms_key_arn = local.ami_kms_key_arn
122+
ssm_ami_id_parameter_arn = local.ami_id_ssm_module_managed ? aws_ssm_parameter.runner_ami_id[0].arn : var.ami_id_ssm_parameter_arn
122123
})
123124
}
124125

modules/runners/variables.tf

+6
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,12 @@ variable "ami_owners" {
130130
default = ["amazon"]
131131
}
132132

133+
variable "ami_id_ssm_parameter_arn" {
134+
description = "ARN of the SSM parameter (of data type aws:ec2:image) that contains the AMI ID to launch runner instances from. Overrides ami_filter"
135+
type = string
136+
default = null
137+
}
138+
133139
variable "ami_id_ssm_parameter_name" {
134140
description = "Externally managed SSM parameter (of data type aws:ec2:image) that contains the AMI ID to launch runner instances from. Overrides ami_filter"
135141
type = string

variables.tf

+9-3
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ variable "enable_organization_runners" {
3333

3434
variable "github_app" {
3535
description = <<EOF
36-
GitHub app parameters, see your github app.
36+
GitHub app parameters, see your github app.
3737
You can optionally create the SSM parameters yourself and provide the ARN and name here, through the `*_ssm` attributes.
38-
If you chose to provide the configuration values directly here,
38+
If you chose to provide the configuration values directly here,
3939
please ensure the key is the base64-encoded `.pem` file (the output of `base64 app.private-key.pem`, not the content of `private-key.pem`).
4040
Note: the provided SSM parameters arn and name have a precedence over the actual value (i.e `key_base64_ssm` has a precedence over `key_base64` etc).
4141
EOF
@@ -383,8 +383,14 @@ variable "ami_owners" {
383383
default = ["amazon"]
384384
}
385385

386+
variable "ami_id_ssm_parameter_arn" {
387+
description = "ARN of the SSM parameter (of data type aws:ec2:image) that contains the AMI ID to launch runner instances from. Overrides ami_filter"
388+
type = string
389+
default = null
390+
}
391+
386392
variable "ami_id_ssm_parameter_name" {
387-
description = "Externally managed SSM parameter (of data type aws:ec2:image) that contains the AMI ID to launch runner instances from. Overrides ami_filter"
393+
description = "(DEPRECATED) Variable is replaced by `ami_id_ssm_parameter_arn` Externally managed SSM parameter (of data type aws:ec2:image) that contains the AMI ID to launch runner instances from. Overrides ami_filter"
388394
type = string
389395
default = null
390396
}

0 commit comments

Comments
 (0)