Skip to content

Commit c5395f6

Browse files
committed
Add AMI build scripts and configuration
Signed-off-by: Manuel Alejandro de Brito Fontes <[email protected]>
1 parent e5bc4a7 commit c5395f6

18 files changed

+1716
-0
lines changed

ami/LICENSE

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of
4+
this software and associated documentation files (the "Software"), to deal in
5+
the Software without restriction, including without limitation the rights to
6+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
7+
the Software, and to permit persons to whom the Software is furnished to do so.
8+
9+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
10+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
11+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
12+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
13+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
14+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
15+

ami/Makefile

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
PACKER_VARIABLES := binary_bucket_name binary_bucket_region eks_version eks_build_date cni_plugin_version root_volume_size data_volume_size hardening_flag http_proxy https_proxy no_proxy
3+
VPC_ID := vpc-0e8cf1ce122b1b059
4+
SUBNET_ID := subnet-0eddf1d7d0f9f9772
5+
AWS_REGION := us-west-2
6+
PACKER_FILE :=
7+
8+
EKS_BUILD_DATE := 2020-11-02
9+
EKS_120_VERSION := 1.20.4
10+
EKS_121_VERSION := 1.21.2
11+
12+
build:
13+
packer build \
14+
--var 'aws_region=$(AWS_REGION)' \
15+
$(foreach packerVar,$(PACKER_VARIABLES), $(if $($(packerVar)),--var $(packerVar)='$($(packerVar))',)) \
16+
$(PACKER_FILE)
17+
18+
# Ubuntu 20.04
19+
#-----------------------------------------------------
20+
# https://docs.aws.amazon.com/eks/latest/userguide/eks-optimized-ami.html
21+
22+
build-ubuntu2004-1.20:
23+
$(MAKE) build PACKER_FILE=amazon-eks-node-ubuntu2004.json eks_version=$(EKS_120_VERSION) eks_build_date=2021-04-12
24+
25+
build-ubuntu2004-1.21:
26+
$(MAKE) build PACKER_FILE=amazon-eks-node-ubuntu2004.json eks_version=$(EKS_121_VERSION) eks_build_date=2021-07-05

ami/amazon-eks-node-ubuntu2004.json

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
{
2+
"variables":{
3+
"aws_region":"us-east-2",
4+
"ami_description":"EKS Kubernetes Worker AMI on Ubuntu 20.04 (k8s: {{user `eks_version`}})",
5+
"eks_version":"",
6+
"eks_build_date":"",
7+
"cni_plugin_version": "v0.9.1",
8+
"binary_bucket_name": "amazon-eks",
9+
"binary_bucket_region": "us-west-2",
10+
"hardening_flag": "false",
11+
"root_volume_size": "30",
12+
"data_volume_size": "10",
13+
"vpc_id":"",
14+
"subnet_id":"",
15+
"http_proxy": "",
16+
"https_proxy": "",
17+
"no_proxy": "",
18+
19+
"source_ami_owner": "099720109477",
20+
"source_ami_owner_govcloud": "513442679011",
21+
"source_ami_ssh_user": "ubuntu",
22+
"source_ami_arch":"x86_64",
23+
"source_ami_name":"ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*",
24+
"target_ami_name": "amazon-eks-node-{{user `eks_version`}}-ubuntu-20.04-{{ timestamp }}"
25+
},
26+
"builders":[
27+
{
28+
"type":"amazon-ebs",
29+
"region":"{{user `aws_region`}}",
30+
"source_ami_filter":{
31+
"filters":{
32+
"name":"{{user `source_ami_name`}}",
33+
"root-device-type":"ebs",
34+
"state":"available",
35+
"virtualization-type":"hvm",
36+
"architecture":"{{user `source_ami_arch`}}"
37+
},
38+
"owners":[
39+
"{{user `source_ami_owner`}}",
40+
"{{user `source_ami_owner_govcloud`}}"
41+
],
42+
"most_recent":true
43+
},
44+
"instance_type":"m5.xlarge",
45+
"ssh_username":"{{user `source_ami_ssh_user`}}",
46+
"ssh_pty":true,
47+
"subnet_id":"{{user `subnet_id`}}",
48+
"launch_block_device_mappings":[
49+
{
50+
"device_name":"/dev/sda1",
51+
"volume_size": "{{user `root_volume_size`}}",
52+
"volume_type":"gp3",
53+
"delete_on_termination":true
54+
},
55+
{
56+
"device_name":"/dev/sdb",
57+
"volume_size":"{{user `data_volume_size`}}",
58+
"volume_type":"gp3",
59+
"delete_on_termination":true
60+
}
61+
],
62+
"ami_block_device_mappings":[
63+
{
64+
"device_name":"/dev/sdb",
65+
"volume_size":"{{user `data_volume_size`}}",
66+
"volume_type":"gp2",
67+
"delete_on_termination":true
68+
}
69+
],
70+
"tags":{
71+
"Name":"{{user `target_ami_name`}}",
72+
"BuildDate":"{{ isotime }}"
73+
},
74+
"ami_name":"{{user `target_ami_name`}}",
75+
"ami_description":"{{user `ami_description` }}",
76+
"ami_virtualization_type":"hvm",
77+
"run_tags":{
78+
"Name":"packer-{{user `target_ami_name`}}"
79+
}
80+
}
81+
],
82+
"provisioners":[
83+
{
84+
"type": "shell",
85+
"inline": [
86+
"mkdir -p /etc/packer/files",
87+
"chown -R {{user `source_ami_ssh_user`}}:{{user `source_ami_ssh_user`}} /etc/packer/files"
88+
],
89+
"execute_command":"echo 'packer' | {{.Vars}} sudo -S -E bash -eux '{{.Path}}'"
90+
},
91+
{
92+
"type": "file",
93+
"source": "./files/",
94+
"destination": "/etc/packer/files"
95+
},
96+
{
97+
"type":"shell",
98+
"scripts": [
99+
"./scripts/ubuntu2004/boilerplate.sh"
100+
],
101+
"environment_vars": [
102+
"HTTP_PROXY={{user `http_proxy`}}",
103+
"HTTPS_PROXY={{user `https_proxy`}}",
104+
"NO_PROXY={{user `no_proxy`}}",
105+
"KUBERNETES_VERSION={{user `eks_version`}}",
106+
"KUBERNETES_BUILD_DATE={{user `eks_build_date`}}",
107+
"BINARY_BUCKET_NAME={{user `binary_bucket_name`}}",
108+
"BINARY_BUCKET_REGION={{user `binary_bucket_region`}}",
109+
"CNI_PLUGIN_VERSION={{user `cni_plugin_version`}}",
110+
"HARDENING_FLAG={{user `hardening_flag`}}"
111+
],
112+
"execute_command":"echo 'packer' | {{.Vars}} sudo -S -E bash -eux '{{.Path}}'",
113+
"expect_disconnect":true,
114+
"pause_after":"30s"
115+
},
116+
{
117+
"type":"shell",
118+
"scripts":[
119+
"./scripts/shared/docker.sh",
120+
"./scripts/shared/eks.sh",
121+
"./scripts/shared/cis-eks.sh"
122+
],
123+
"environment_vars": [
124+
"HTTP_PROXY={{user `http_proxy`}}",
125+
"HTTPS_PROXY={{user `https_proxy`}}",
126+
"NO_PROXY={{user `no_proxy`}}",
127+
"KUBERNETES_VERSION={{user `eks_version`}}",
128+
"KUBERNETES_BUILD_DATE={{user `eks_build_date`}}",
129+
"BINARY_BUCKET_NAME={{user `binary_bucket_name`}}",
130+
"BINARY_BUCKET_REGION={{user `binary_bucket_region`}}",
131+
"CNI_PLUGIN_VERSION={{user `cni_plugin_version`}}",
132+
"HARDENING_FLAG={{user `hardening_flag`}}"
133+
],
134+
"execute_command":"echo 'packer' | {{.Vars}} sudo -S -E bash -eux '{{.Path}}'",
135+
"expect_disconnect":true,
136+
"pause_after":"30s"
137+
},
138+
{
139+
"type":"shell",
140+
"scripts": [
141+
"./scripts/ubuntu2004/hardening.sh",
142+
"./scripts/ubuntu2004/cleanup.sh"
143+
],
144+
"environment_vars": [
145+
"HTTP_PROXY={{user `http_proxy`}}",
146+
"HTTPS_PROXY={{user `https_proxy`}}",
147+
"NO_PROXY={{user `no_proxy`}}",
148+
"KUBERNETES_VERSION={{user `eks_version`}}",
149+
"KUBERNETES_BUILD_DATE={{user `eks_build_date`}}",
150+
"BINARY_BUCKET_NAME={{user `binary_bucket_name`}}",
151+
"BINARY_BUCKET_REGION={{user `binary_bucket_region`}}",
152+
"CNI_PLUGIN_VERSION={{user `cni_plugin_version`}}",
153+
"HARDENING_FLAG={{user `hardening_flag`}}"
154+
],
155+
"execute_command":"echo 'packer' | {{.Vars}} sudo -S -E bash -eux '{{.Path}}'"
156+
}
157+
]
158+
}

ami/copy-image.sh

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/usr/bin/env bash
2+
3+
SOURCE_AMI=$1
4+
if [ -z "$SOURCE_AMI" ]; then
5+
echo -e "Please provider a valid AMI image"
6+
exit 1
7+
fi
8+
9+
set -euo pipefail
10+
11+
SOURCE_REGION=us-west-2
12+
TARGET_REGIONS=(
13+
us-west-1
14+
eu-west-1
15+
eu-west-2
16+
eu-central-1
17+
us-east-1
18+
us-east-2
19+
)
20+
21+
if ! aws ec2 describe-images --region us-west-2 --image-ids "${SOURCE_AMI}" >/dev/null 2>&1; then
22+
echo "The AMI image with ID ${SOURCE_AMI} does not exist."
23+
exit 1
24+
fi
25+
26+
NAME=$(aws ec2 describe-images --region us-west-2 --image-ids "${SOURCE_AMI}" --query 'Images[*].[Name]' --output text)
27+
28+
for TO_REGION in ${TARGET_REGIONS[*]};do
29+
aws ec2 copy-image \
30+
--name "$NAME" \
31+
--source-image-id "${SOURCE_AMI}" \
32+
--source-region "${SOURCE_REGION}" \
33+
--region "${TO_REGION}" \
34+
--output text
35+
done

0 commit comments

Comments
 (0)