Skip to content

Commit b06e164

Browse files
committed
AMI builds private by default
1 parent 1a597df commit b06e164

File tree

4 files changed

+47
-2
lines changed

4 files changed

+47
-2
lines changed

.buildkite/pipeline.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ steps:
5656
command: .buildkite/steps/packer.sh windows
5757
timeout_in_minutes: 60
5858
retry: { automatic: { limit: 3 } }
59+
env:
60+
AMI_PUBLIC: true
5961
agents:
6062
queue: "${BUILDKITE_AGENT_META_DATA_QUEUE}"
6163
depends_on:
@@ -122,6 +124,8 @@ steps:
122124
command: .buildkite/steps/packer.sh linux
123125
timeout_in_minutes: 60
124126
retry: { automatic: { limit: 3 } }
127+
env:
128+
AMI_PUBLIC: true
125129
agents:
126130
queue: "${BUILDKITE_AGENT_META_DATA_QUEUE}"
127131
depends_on:
@@ -187,6 +191,8 @@ steps:
187191
command: .buildkite/steps/packer.sh linux arm64
188192
timeout_in_minutes: 60
189193
retry: { automatic: { limit: 3 } }
194+
env:
195+
AMI_PUBLIC: true
190196
agents:
191197
queue: "${BUILDKITE_AGENT_META_DATA_QUEUE}"
192198
depends_on:

Makefile

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ WIN64_INSTANCE_TYPE ?= m7i.xlarge
2020
BUILDKITE_BUILD_NUMBER ?= none
2121
BUILDKITE_PIPELINE_DEFAULT_BRANCH ?= main
2222

23+
# AMI visibility configuration
24+
AMI_PUBLIC ?= false
25+
AMI_USERS ?=
26+
27+
# Convert comma-separated AMI_USERS to JSON array format
28+
AMI_USERS_JSON = $(if $(AMI_USERS),[$(shell echo '$(AMI_USERS)' | $(SED) 's/[^,][^,]*/"&"/g')],[])
29+
2330
IS_RELEASED ?= false
2431
ifeq ($(BUILDKITE_BRANCH),$(BUILDKITE_PIPELINE_DEFAULT_BRANCH))
2532
IS_RELEASED = true
@@ -109,6 +116,8 @@ packer-linux-amd64.output: $(PACKER_LINUX_FILES) build/fix-perms-linux-amd64
109116
-var 'instance_type=$(AMD64_INSTANCE_TYPE)' \
110117
-var 'build_number=$(BUILDKITE_BUILD_NUMBER)' \
111118
-var 'is_released=$(IS_RELEASED)' \
119+
-var 'ami_public=$(AMI_PUBLIC)' \
120+
-var 'ami_users=$(AMI_USERS_JSON)' \
112121
buildkite-ami.pkr.hcl | tee $@
113122

114123
build/linux-arm64-ami.txt: packer-linux-arm64.output env-AWS_REGION
@@ -144,6 +153,8 @@ packer-linux-arm64.output: $(PACKER_LINUX_FILES) build/fix-perms-linux-arm64
144153
-var 'build_number=$(BUILDKITE_BUILD_NUMBER)' \
145154
-var 'is_released=$(IS_RELEASED)' \
146155
-var 'agent_version=$(CURRENT_AGENT_VERSION_LINUX)' \
156+
-var 'ami_public=$(AMI_PUBLIC)' \
157+
-var 'ami_users=$(AMI_USERS_JSON)' \
147158
buildkite-ami.pkr.hcl | tee $@
148159

149160
build/windows-amd64-ami.txt: packer-windows-amd64.output env-AWS_REGION
@@ -171,6 +182,8 @@ packer-windows-amd64.output: $(PACKER_WINDOWS_FILES)
171182
-var 'build_number=$(BUILDKITE_BUILD_NUMBER)' \
172183
-var 'is_released=$(IS_RELEASED)' \
173184
-var 'agent_version=$(CURRENT_AGENT_VERSION_WINDOWS)' \
185+
-var 'ami_public=$(AMI_PUBLIC)' \
186+
-var 'ami_users=$(AMI_USERS_JSON)' \
174187
buildkite-ami.pkr.hcl | tee $@
175188

176189
# -----------------------------------------

packer/linux/buildkite-ami.pkr.hcl

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,18 @@ variable "is_released" {
3737
default = false
3838
}
3939

40+
variable "ami_public" {
41+
type = bool
42+
description = "Whether to make the AMI publicly available to all AWS users. Defaults to false for security."
43+
default = false
44+
}
45+
46+
variable "ami_users" {
47+
type = list(string)
48+
description = "List of AWS account IDs that should have access to the AMI when ami_public is false."
49+
default = []
50+
}
51+
4052
data "amazon-ami" "al2023" {
4153
filters = {
4254
architecture = var.arch
@@ -50,7 +62,8 @@ data "amazon-ami" "al2023" {
5062

5163
source "amazon-ebs" "elastic-ci-stack-ami" {
5264
ami_description = "Buildkite Elastic Stack (Amazon Linux 2023 w/ docker)"
53-
ami_groups = ["all"]
65+
ami_groups = var.ami_public ? ["all"] : ["self"]
66+
ami_users = var.ami_public ? [] : var.ami_users
5467
ami_name = "buildkite-stack-linux-${var.arch}-${replace(timestamp(), ":", "-")}"
5568
instance_type = var.instance_type
5669
region = var.region

packer/windows/buildkite-ami.pkr.hcl

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,18 @@ variable "is_released" {
3737
default = false
3838
}
3939

40+
variable "ami_public" {
41+
type = bool
42+
description = "Whether to make the AMI publicly available to all AWS users. Defaults to false for security."
43+
default = false
44+
}
45+
46+
variable "ami_users" {
47+
type = list(string)
48+
description = "List of AWS account IDs that should have access to the AMI when ami_public is false."
49+
default = []
50+
}
51+
4052
data "amazon-ami" "windows-server-2022" {
4153
filters = {
4254
name = "Windows_Server-2022-English-Full-Base-*"
@@ -49,7 +61,8 @@ data "amazon-ami" "windows-server-2022" {
4961

5062
source "amazon-ebs" "elastic-ci-stack" {
5163
ami_description = "Buildkite Elastic Stack (Windows Server 2022 w/ docker)"
52-
ami_groups = ["all"]
64+
ami_groups = var.ami_public ? ["all"] : ["self"]
65+
ami_users = var.ami_public ? [] : var.ami_users
5366
ami_name = "buildkite-stack-windows-${replace(timestamp(), ":", "-")}"
5467
communicator = "winrm"
5568
instance_type = var.instance_type

0 commit comments

Comments
 (0)