Skip to content

Commit ff48e8f

Browse files
authored
Merge pull request #1551 from buildkite/SUP-4516-configure-ami-visibility
Make custom AMI builds have private visibility, by default
2 parents 1a597df + 878ce42 commit ff48e8f

File tree

5 files changed

+68
-5
lines changed

5 files changed

+68
-5
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_LIST = $(if $(AMI_USERS),[$(shell echo '$(AMI_USERS)' | $(SED) 's/[[:space:]]//g' | $(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_LIST)' \
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_LIST)' \
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_LIST)' \
174187
buildkite-ami.pkr.hcl | tee $@
175188

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

README.md

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,19 +115,37 @@ aws-vault exec some-profile -- make create-stack
115115
```
116116

117117
If you need to build your own AMI (because you've changed something in the
118-
`packer` directory), run packer with AWS credentials in your shell environment:
118+
`packer` directory), run packer with AWS credentials in your shell environment.
119+
120+
By default, AMIs are built as private (only accessible to the AWS account that created them) for security. You can control AMI visibility and build location using these variables:
121+
122+
- **`AMI_PUBLIC`** - Set to `true` to make AMIs publicly accessible to all AWS users, or `false` (default) for private AMIs
123+
- **`AMI_USERS`** - Comma-separated list of AWS account IDs that should have access to private AMIs (ignored when `AMI_PUBLIC=true`)
124+
- **`AWS_REGION`** - AWS region where AMIs should be built (defaults to `us-east-1`)
119125

120126
```bash
127+
# Build private AMIs (default - recommended for security)
121128
make packer
129+
130+
# Build public AMIs (available to all AWS users)
131+
make packer AMI_PUBLIC=true
132+
133+
# Build private AMIs with access for specific AWS accounts
134+
make packer AMI_USERS="123456789012,987654321098,555666777888"
135+
136+
# Combined: private AMIs with specific account access in a different region
137+
make packer AMI_PUBLIC=false AMI_USERS="123456789012,987654321098" AWS_REGION=us-west-2
122138
```
123139

124-
This will boot and image three AWS EC2 instances in your accounts `us-east-1`
125-
default VPC:
140+
This will boot and image three AWS EC2 instances in your account's `us-east-1`
141+
default VPC (or the region specified by `AWS_REGION`):
126142

127143
- Linux (64-bit x86)
128144
- Linux (64-bit Arm)
129145
- Windows (64-bit x86)
130146

147+
**Security Note:** Making AMIs public (`AMI_PUBLIC=true`) can expose any secrets accidentally baked into the image. The default private setting helps prevent accidental exposure of sensitive information.
148+
131149
## Support Policy
132150

133151
We provide support for security and bug fixes on the current major release only.

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"] : []
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"] : []
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)