Skip to content

How do I create custom Amazon Linux AMIs for Amazon EKS?

5 minute read
0

I want to create a custom Amazon Linux Amazon Machine Image (AMI) to deploy with an Amazon Elastic Kubernetes Service (Amazon EKS) cluster.

Resolution

Prerequisites

  • Install Packer either on your local machine or on an Amazon Linux Amazon Elastic Compute Cloud (EC2) instance. For instructions, see Install Packer on the Hashicorp website.
  • Then, install the Amazon Plugin to use with Hashicorp Packer to create custom images on AWS. For instructions, see Installation on the Hashicorp website.
  • Make sure that make and git are installed:
    sudo yum install make -y
    sudo yum install git -y

Create a custom Amazon Linux AMI for Amazon EKS

Complete the following steps:

  1. To clone the amazon-eks-ami AWS Labs repository, run the following command:
    sudo git clone https://github.com/awslabs/amazon-eks-ami && cd amazon-eks-ami
    For more information, see amazon-eks-ami on the GitHub website.
  2. To create a new file named variables-custom.json in the amazon-eks-ami directory, run the following command:
    sudo vi variables-custom.json
    Note: The default variables file that the variables-custom.json overrides is located in the variables-default.json file. If you use Amazon Linux 2 (AL2), then the file path for variables-default.json is /amazon-eks-ami/templates/al2/variables-default.json. If you use Amazon Linux 2023 (AL2023), then the file path is /amazon-eks-ami/templates/al2023/variables-default.json.
  3. In the variables-custom.json file, add your values:
    { "aws_region": "AWS_Region", "source_ami_filter_name": "AMI_Name", "source_ami_id": "Source_AMI_ID", "source_ami_owners": "AMI_Owner_Account_ID", "temporary_security_group_source_cidrs": "Source_Machine_IP", #example: local_machine_IP /32 or NAT_GW_IP /32 or EC2_PublicIP /32 "launch_block_device_mappings_volume_size": "Integer", #Root volume size of the instance to be launched by packer "iam_instance_profile": "IAM_Instance_Profile_Name" #Optional
    }
    When you use an existing AMI as the source to create a customized AMI for EKS, set the launch_block_device_mappings_volume_size value. The value must be equal to or more than the AMI snapshot volume size. The default value is "4" in the variables-default.json file.
    If you use the iam_instance_profile option in Packer, then you must provide the instance profile name of the IAM role that you want to attach. Don't provide the IAM role ARN or instance profile ARN.
    If the temporary_security_group_source_cidrs parameter isn't provided, then Packer opens the SSH port to all IP addresses (0.0.0.0/0). To prevent a security risk, it's a best practice to specify your source machine's IP address in the temporary_security_group_source_cidrs parameter.
  4. To initiate the custom AMI creation process, run the following command:
    sudo make PACKER_OPTIONAL_K8S_VARIABLE_FILE=variables-custom.json k8s=1.29 os_distro=al2 arch=x86_64 -d
    Or, to turn on packer logging, run the following command:
    sudo PACKER_LOG_PATH=packer_logs.txt`TZ=":Asia/Kolkata" date +%Y-%m-%d_%H%M` PACKER_LOG=1 make PACKER_OPTIONAL_K8S_VARIABLE_FILE=variables-custom.json k8s=1.29 os_distro=al2 arch=x86_64 -d
    Note: To set your system's time zone, replace ":Asia/Kolkata" with your time zone. The generated log file of Packer is available in the same directory where you run the Packer command.
  5. Check that the IAM user credentials or IAM role that you use to run the make command has the required IAM permissions. For more information, see IAM task or instance role and Attaching IAM policies to roles on the Hashicorp website. Also, the permissions must allow access to the default Amazon Simple Storage Service (Amazon S3) bucket. You must attach the AmazonS3ReadOnlyAccess policy. If you don't have permissions to access the S3 bucket, then you might receive the following error: "required variable not set: kubernetes_build_date
    required variable not set: kubernetes_version" Note: These permissions allow Packer to make API calls to AWS. Or, you can configure static credentials, such as a secret key and secret access key, an environment variable, or shared credential files.
  6. Verify that your custom AMI is present in your AWS Region of source AMI.

When Packer provisions the instance, default binaries are downloaded from the Amazon EKS public S3 bucket amazon-eks in us-west-2. For more information, see the install-worker.sh file on the GitHub website.

Provide your own Kubernetes binaries (optional)

Complete the following steps:

  1. To examine the available binaries that are provided in the default bucket, run the following command:
    aws s3 ls s3://amazon-eks
    aws s3 ls s3://amazon-eks/kubernetes_version/kubernetes_build_date/bin/linux/arch/
    Note: Replace amazon-eks, kubernetes_version, kubernetes_build_date, and arch with your values.
  2. To download your own binaries to the worker node as it provisions, mirror the amazon-eks bucket folder structure that's used in the install-worker.sh script.
  3. After your binaries are prepared, use the AWS CLI to copy the binaries to your Amazon S3 bucket. The following example uses a custom kubelet binary:
    aws s3 cp kubelet s3://my-custom-bucket/kubernetes_version/kubernetes_build_date/bin/linux/arch/kubelet
    Note: Replace my-custom-bucket, amazon-eks, kubernetes_version, kubernetes_build_date, and arch with your values. You must provide all the binaries that are listed in the default amazon-eks bucket.
  4. To start the build process, use the source AMI that's configured in variables-default.json from directory al2 at path /amazon-eks-ami/templates/al2/variables-default.json. To invoke make with the parameters, run a command similar to the following:
    make k8s binary_bucket_name=Custom_Bucket_Name binary_bucket_region=Region kubernetes_version=Version kubernetes_build_date=Build_Date -d
    Note: Confirm that the binary_bucket_name, binary_bucket_region, kubernetes_version, and kubernetes_build_date parameters match the path to your binaries in Amazon S3.
AWS OFFICIALUpdated 2 years ago