Skip to content

Commit

Permalink
ADD-AMI-ID
Browse files Browse the repository at this point in the history
  • Loading branch information
kayman-mk committed Dec 5, 2024
1 parent 1de7662 commit 54635f4
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 23 deletions.
27 changes: 13 additions & 14 deletions data.tf
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ data "aws_availability_zone" "runners" {
name = data.aws_subnet.runners.availability_zone
}

data "aws_ami" "runner" {
id = length(var.runner_ami_id) > 0 ? var.runner_ami_id : null
owners = length(var.runner_ami_id) > 0 ? var.runner_ami_owners : null
data "aws_ami" "runner_by_filter" {
count = length(var.runner_ami_id) > 0 ? 0 : 1

owners = var.runner_ami_owners
most_recent = "true"

dynamic "filter" {
for_each = length(var.runner_ami_id) > 0 ? [] : var.runner_ami_filter
for_each = var.runner_ami_filter

content {
name = filter.key
Expand All @@ -27,15 +28,14 @@ data "aws_ami" "runner" {
}
}

data "aws_ami" "docker_machine" {
count = var.runner_worker.type == "docker+machine" ? 1 : 0
data "aws_ami" "docker_machine_by_filter" {
count = var.runner_worker.type == "docker+machine" && length(var.runner_worker_docker_machine_ami_id) == 0 ? 1 : 0

id = length(var.runner_worker_docker_machine_ami_id) > 0 ? var.runner_worker_docker_machine_ami_id : null
owners = length(var.runner_worker_docker_machine_ami_id) > 0 ? var.runner_worker_docker_machine_ami_owners : null
owners = var.runner_worker_docker_machine_ami_owners
most_recent = "true"

dynamic "filter" {
for_each = length(var.runner_worker_docker_machine_ami_id) > 0 ? [] : var.runner_worker_docker_machine_ami_filter
for_each = var.runner_worker_docker_machine_ami_filter

content {
name = filter.key
Expand All @@ -44,15 +44,14 @@ data "aws_ami" "docker_machine" {
}
}

data "aws_ami" "docker-autoscaler" {
count = var.runner_worker.type == "docker-autoscaler" ? 1 : 0
data "aws_ami" "docker_autoscaler_by_filter" {
count = var.runner_worker.type == "docker-autoscaler" && length(var.runner_worker_docker_autoscaler_ami_id) == 0 ? 1 : 0

id = length(var.runner_worker_docker_autoscaler_ami_id) > 0 ? var.runner_worker_docker_autoscaler_ami_id : null
owners = length(var.runner_worker_docker_autoscaler_ami_id) > 0 ? var.runner_worker_docker_autoscaler_ami_owners : null
owners = var.runner_worker_docker_autoscaler_ami_owners
most_recent = "true"

dynamic "filter" {
for_each = length(var.runner_worker_docker_autoscaler_ami_id) > 0 ? [] : var.runner_worker_docker_autoscaler_ami_filter
for_each = var.runner_worker_docker_autoscaler_ami_filter

content {
name = filter.key
Expand Down
2 changes: 1 addition & 1 deletion docker_autoscaler.tf
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ resource "aws_launch_template" "this" {

name = "${local.name_runner_agent_instance}-worker-launch-template"
user_data = base64gzip(var.runner_worker_docker_autoscaler_instance.start_script)
image_id = data.aws_ami.docker-autoscaler[0].id
image_id = length(var.runner_worker_docker_autoscaler_ami_id) > 0 ? var.runner_worker_docker_autoscaler_ami_id : data.aws_ami.docker_autoscaler_by_filter[0].id
instance_type = var.runner_worker_docker_autoscaler_asg.types[0]
key_name = aws_key_pair.autoscaler[0].key_name
ebs_optimized = var.runner_worker_docker_autoscaler_instance.ebs_optimized
Expand Down
6 changes: 3 additions & 3 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ locals {
runners_iam_instance_profile_name = var.runner_worker_docker_machine_role.profile_name
runners_root_size = var.runner_worker_docker_machine_instance.root_size
runners_volume_type = var.runner_worker_docker_machine_instance.volume_type
runners_ami = var.runner_worker.type == "docker+machine" ? data.aws_ami.docker_machine[0].id : ""
runners_ami = var.runner_worker.type == "docker+machine" ? (length(var.runner_worker_docker_machine_ami_id) > 0 ? var.runner_worker_docker_machine_ami_id : data.aws_ami.docker_machine_by_filter[0].id) : ""
use_fleet = var.runner_worker_docker_machine_fleet.enable
launch_template = var.runner_worker_docker_machine_fleet.enable == true ? aws_launch_template.fleet_gitlab_runner[0].name : ""
docker_machine_options = length(local.docker_machine_options_string) == 1 ? "" : local.docker_machine_options_string
Expand Down Expand Up @@ -258,7 +258,7 @@ resource "aws_launch_template" "gitlab_runner_instance" {
# checkov:skip=CKV_AWS_79:User can decide to enable Metadata service V2. V2 is the default.
name_prefix = "${local.name_runner_agent_instance}-"

image_id = data.aws_ami.runner.id
image_id = length(var.runner_ami_id) > 0 ? var.runner_ami_id : data.aws_ami.runner_by_filter[0].id
user_data = base64gzip(local.template_user_data)
instance_type = var.runner_instance.type
update_default_version = true
Expand Down Expand Up @@ -376,7 +376,7 @@ resource "aws_launch_template" "fleet_gitlab_runner" {
name_prefix = "${local.name_runner_agent_instance}-worker-"

key_name = aws_key_pair.fleet[0].key_name
image_id = data.aws_ami.docker_machine[0].id
image_id = length(var.runner_worker_docker_machine_ami_id) > 0 ? var.runner_worker_docker_machine_ami_id : data.aws_ami.docker_machine_by_filter[0].id
user_data = base64gzip(var.runner_worker_docker_machine_instance.start_script)
instance_type = var.runner_worker_docker_machine_instance.types[0] # it will be override by the fleet
update_default_version = true
Expand Down
10 changes: 5 additions & 5 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,9 @@ variable "runner_ami_owners" {
}

variable "runner_ami_id" {
description = "The AMI ID of the Runner instance."
type = string
default = ""
description = "The AMI ID of the Runner instance."
type = string
default = ""
}

variable "runner_networking" {
Expand Down Expand Up @@ -794,8 +794,8 @@ variable "runner_worker_docker_machine_ami_owners" {

variable "runner_worker_docker_machine_ami_id" {
description = "The ID of the AMI to use for the Runner Worker (docker-machine)."
type = string
default = ""
type = string
default = ""
}

variable "runner_worker_docker_autoscaler_ami_filter" {
Expand Down

0 comments on commit 54635f4

Please sign in to comment.