From bb7346bee8340af03a61e85a3cb129da68543612 Mon Sep 17 00:00:00 2001 From: Bryan Shelton Date: Thu, 9 Jan 2025 04:05:04 -0800 Subject: [PATCH] feat(packer): Allow passing in an instance_profile to packer (#4278) We ran into needing to pass a custom instance profile to packer in order to get AWS IAM credentials for pre-pulling some ECR images. I noticed what I think was a missing `instance_type` override variable in the windows-core-2022 file. I'm not sure if that was an oversight, or if it was omitted for a reason. It's been a long time since I've been proficient in Windows, so I wasn't able to test that part of this very well. Thanks so much for this project, this has been immensely beneficial for us. --------- Co-authored-by: Niek Palm --- images/linux-al2023/github_agent.linux.pkr.hcl | 7 +++++++ images/ubuntu-focal/github_agent.ubuntu.pkr.hcl | 8 ++++++++ .../github_agent.ubuntu.pkr.hcl | 8 ++++++++ images/ubuntu-jammy/github_agent.ubuntu.pkr.hcl | 8 ++++++++ .../github_agent.windows.pkr.hcl | 8 ++++++++ .../github_agent.windows.pkr.hcl | 15 ++++++++++++++- 6 files changed, 53 insertions(+), 1 deletion(-) diff --git a/images/linux-al2023/github_agent.linux.pkr.hcl b/images/linux-al2023/github_agent.linux.pkr.hcl index ac8c4dff40..de72a9466f 100644 --- a/images/linux-al2023/github_agent.linux.pkr.hcl +++ b/images/linux-al2023/github_agent.linux.pkr.hcl @@ -42,6 +42,12 @@ variable "instance_type" { default = "m3.medium" } +variable "iam_instance_profile" { + description = "IAM instance profile Packer will use for the builder. An empty string (default) means no profile will be assigned." + type = string + default = "" +} + variable "root_volume_size_gb" { type = number default = 8 @@ -98,6 +104,7 @@ locals { source "amazon-ebs" "githubrunner" { ami_name = "github-runner-al2023-x86_64-${formatdate("YYYYMMDDhhmm", timestamp())}" instance_type = var.instance_type + iam_instance_profile = var.iam_instance_profile region = var.region security_group_id = var.security_group_id subnet_id = var.subnet_id diff --git a/images/ubuntu-focal/github_agent.ubuntu.pkr.hcl b/images/ubuntu-focal/github_agent.ubuntu.pkr.hcl index 56cb65ee5b..9712417274 100644 --- a/images/ubuntu-focal/github_agent.ubuntu.pkr.hcl +++ b/images/ubuntu-focal/github_agent.ubuntu.pkr.hcl @@ -42,6 +42,12 @@ variable "instance_type" { default = "t3.medium" } +variable "iam_instance_profile" { + description = "IAM instance profile Packer will use for the builder. An empty string (default) means no profile will be assigned." + type = string + default = "" +} + variable "root_volume_size_gb" { type = number default = 8 @@ -98,6 +104,7 @@ locals { source "amazon-ebs" "githubrunner" { ami_name = "github-runner-ubuntu-focal-amd64-${formatdate("YYYYMMDDhhmm", timestamp())}" instance_type = var.instance_type + iam_instance_profile = var.iam_instance_profile region = var.region security_group_id = var.security_group_id subnet_id = var.subnet_id @@ -200,6 +207,7 @@ build { "sudo chmod +x /var/lib/cloud/scripts/per-boot/start-runner.sh", ] } + post-processor "manifest" { output = "manifest.json" strip_path = true diff --git a/images/ubuntu-jammy-arm64/github_agent.ubuntu.pkr.hcl b/images/ubuntu-jammy-arm64/github_agent.ubuntu.pkr.hcl index 91e2d7f38e..1536eb5784 100644 --- a/images/ubuntu-jammy-arm64/github_agent.ubuntu.pkr.hcl +++ b/images/ubuntu-jammy-arm64/github_agent.ubuntu.pkr.hcl @@ -42,6 +42,12 @@ variable "instance_type" { default = "t4g.small" } +variable "iam_instance_profile" { + description = "IAM instance profile Packer will use for the builder. An empty string (default) means no profile will be assigned." + type = string + default = "" +} + variable "root_volume_size_gb" { type = number default = 8 @@ -98,6 +104,7 @@ locals { source "amazon-ebs" "githubrunner" { ami_name = "github-runner-ubuntu-jammy-arm64-${formatdate("YYYYMMDDhhmm", timestamp())}" instance_type = var.instance_type + iam_instance_profile = var.iam_instance_profile region = var.region security_group_id = var.security_group_id subnet_id = var.subnet_id @@ -200,6 +207,7 @@ build { "sudo chmod +x /var/lib/cloud/scripts/per-boot/start-runner.sh", ] } + post-processor "manifest" { output = "manifest.json" strip_path = true diff --git a/images/ubuntu-jammy/github_agent.ubuntu.pkr.hcl b/images/ubuntu-jammy/github_agent.ubuntu.pkr.hcl index 311bbc5b22..be23a256ca 100644 --- a/images/ubuntu-jammy/github_agent.ubuntu.pkr.hcl +++ b/images/ubuntu-jammy/github_agent.ubuntu.pkr.hcl @@ -42,6 +42,12 @@ variable "instance_type" { default = "t3.medium" } +variable "iam_instance_profile" { + description = "IAM instance profile Packer will use for the builder. An empty string (default) means no profile will be assigned." + type = string + default = "" +} + variable "root_volume_size_gb" { type = number default = 8 @@ -98,6 +104,7 @@ locals { source "amazon-ebs" "githubrunner" { ami_name = "github-runner-ubuntu-jammy-amd64-${formatdate("YYYYMMDDhhmm", timestamp())}" instance_type = var.instance_type + iam_instance_profile = var.iam_instance_profile region = var.region security_group_id = var.security_group_id subnet_id = var.subnet_id @@ -200,6 +207,7 @@ build { "sudo chmod +x /var/lib/cloud/scripts/per-boot/start-runner.sh", ] } + post-processor "manifest" { output = "manifest.json" strip_path = true diff --git a/images/windows-core-2019/github_agent.windows.pkr.hcl b/images/windows-core-2019/github_agent.windows.pkr.hcl index 5a31604c01..e27ad4a2bc 100644 --- a/images/windows-core-2019/github_agent.windows.pkr.hcl +++ b/images/windows-core-2019/github_agent.windows.pkr.hcl @@ -24,6 +24,12 @@ variable "instance_type" { default = "t3a.medium" } +variable "iam_instance_profile" { + description = "IAM instance profile Packer will use for the builder. An empty string (default) means no profile will be assigned." + type = string + default = "" +} + variable "ebs_delete_on_termination" { description = "Indicates whether the EBS volume is deleted on instance termination." type = bool @@ -64,6 +70,7 @@ source "amazon-ebs" "githubrunner" { ami_name = "github-runner-windows-core-2019-${formatdate("YYYYMMDDhhmm", timestamp())}" communicator = "winrm" instance_type = var.instance_type + iam_instance_profile = var.iam_instance_profile region = var.region associate_public_ip_address = var.associate_public_ip_address temporary_security_group_source_public_ip = var.temporary_security_group_source_public_ip @@ -114,6 +121,7 @@ build { }) ], var.custom_shell_commands) } + post-processor "manifest" { output = "manifest.json" strip_path = true diff --git a/images/windows-core-2022/github_agent.windows.pkr.hcl b/images/windows-core-2022/github_agent.windows.pkr.hcl index 18a5ee93c3..0a85595ec5 100644 --- a/images/windows-core-2022/github_agent.windows.pkr.hcl +++ b/images/windows-core-2022/github_agent.windows.pkr.hcl @@ -18,6 +18,18 @@ variable "region" { default = "eu-west-1" } +variable "instance_type" { + description = "The instance type Packer will use for the builder" + type = string + default = "m4.xlarge" +} + +variable "iam_instance_profile" { + description = "IAM instance profile Packer will use for the builder. An empty string (default) means no profile will be assigned." + type = string + default = "" +} + variable "security_group_id" { description = "The ID of the security group Packer will associate with the builder to enable access" type = string @@ -74,7 +86,8 @@ locals { source "amazon-ebs" "githubrunner" { ami_name = "github-runner-windows-core-2022-${formatdate("YYYYMMDDhhmm", timestamp())}" communicator = "winrm" - instance_type = "m4.xlarge" + instance_type = var.instance_type + iam_instance_profile = var.iam_instance_profile region = var.region security_group_id = var.security_group_id subnet_id = var.subnet_id