Packer amazon-ebs Configuration

From NovaOrdis Knowledge Base
Jump to navigation Jump to search

External

Internal

Overview

amazon-ebs creates Amazon AMIs backed by EBS volumes for use in EC2.

The builder launches an EC2 instance from a source AMI, provisions that running VM, and then creates an AMI from that VM, in the AWS account whose credentials are specified as shown here. The instance will be brought up in the default VPC of the user. The builder will create temporary keypairs, security group rules, etc. that provide it temporary access to the instance while the image is being created, which simplifies configuration. The builder does not manage the AMIs.

Example

{
  "variables": {
    "ami_version": "1.0.0",
    "ami_user": "00000000000",
    "ami_base_name": "kubernetes-node",
    "base_image_name": "RHEL-7.7_HVM-20191119-x86_64-2-Hourly2-GP2",
    "ec2_user": "ec2-user",
    "instance_type": "t2.large",
    "root_volume_size_Gi": "30",
    "yq_version": "2.4.1"
  },

  "builders": [
    {
      "name": "kubernetes-node",
      "type": "amazon-ebs",
      "ami_name": "{{user `ami_base_name`}}-{{user `ami_version`}}",
      "instance_type": "{{user `instance_type`}}",
      "ami_users": [
        "{{user `ami_user`}}"
      ],
      "ssh_username": "{{user `ec2_user`}}",
      "source_ami_filter": {
        "filters": {
          "virtualization-type": "hvm",
          "name": "{{user `base_image_name`}}",
          "root-device-type": "ebs"
        },
        "owners": [
          "309956199498"
        ]
      },
      "launch_block_device_mappings": [
        {
          "device_name": "/dev/xvda",
          "volume_size": "{{user `root_volume_size_Gi`}}",
          "volume_type": "gp2",
          "delete_on_termination": true
        }
      ],
      "tags": {
        "function": "kubernetes-node",
        "base": "{{user `base_image_name`}}"
      }
    }
  ],

  "provisioners": [
    {
      "type": "shell",
      "environment_vars": [
        "KUBERNETES_NODE_AMI_VERSION={{ user `ami_version` }}",
        "KUBERNETES_NODE_BASE_IMAGE_NAME={{ user `base_image_name` }}",
        "KUBERNETES_NODE_EC2_USER={{ user `ec2_user` }}",
        "KUBERNETES_NODE_YQ_VERSION={{ user `yq_version` }}"
      ],
      "execute_command": "echo '' | {{ .Vars }} sudo -S -E -u root '{{ .Path }}'",
      "scripts": [
        "scripts/install.sh",
        "scripts/config.sh",
        "scripts/cleanup.sh"
      ]
    }
  ]
}

Root Device

If not specified, a default root device /dev/sda1 with the size of 10Gi is created by default:

/dev/sda1=snap-066b8c74f34223b6d:10:true:gp2

However, when an instance is created from the AMI, the root device is accessible as /dev/xvda:

Filesystem      Size  Used Avail Use% Mounted on
...
dev/xvda2       10G  2.9G  7.2G  29% /

To configure the root device, use launch_block_device_mappings parameter.

For more details about EC2 block device mapping, see:

EC2 Concepts

Builder Parameters

ami_name

Required parameter, which will translate to AMI Name of the resulting AMI to be used when managing AMIs in the AWS console or with the API. Must be unique, even if a unique AMI ID is generated by the build. If a image with the same name exists, Packer will error out.

ssh_username

Required.

instance_type, spot_instance_types

One of them is required.

ami_users

A list of account IDs that have access to launch the resulting AMI(s). By default no additional users other than the user creating the AMI has permissions to launch it.

{
  "builders": [
     "type": "amazon-ebs"
     ...
    "ami_users": [ "000000000000", "111111111111111" ]
  ]
}

ami_regions

{
  "builders": [
     "type": "amazon-ebs"
     ...
    "ami_regions":  ["us-west-2", "us-east-1"]
  ]
}

Note that the region listed in ami_regions are in addition to the default region, and if the default region of the user executing packer is mentioned in the list, the AMI will be copied twice.

launch_block_device_mappings

https://www.packer.io/docs/builders/amazon-ebs.html#launch_block_device_mappings
https://www.packer.io/docs/builders/amazon-ebs.html#block-devices-configuration

Can be used to configure the root device, and specify a custom size, like in the example below:

{
  "builders": [
    {
      "type": "amazon-ebs",
      ...
      "launch_block_device_mappings": [
        {
          "device_name": "/dev/sda1",
          "volume_size": 30,
          "volume_type": "gp2",
          "delete_on_termination": true
        }
      ],
  ],
  ...
}

The device name, /dev/sda1, seems to be essential to indicate we want to configure the root device, even though after boot, the same device will be available as /dev/xvda1.

volume_type

A string specifying the volume type:

  • "gp2" General Purpose SSD volumes
  • "io1" Provisioned IOPS SSD volumes
  • "st1" Throughput Optimized HDD
  • "sc1" Cold HDD
  • standard Magnetic volumes

volume_size

An int64 representing the size of the volume in GiB. It is required if not specifying a snapshot_id.

ami_block_device_mappings

https://www.packer.io/docs/builders/amazon-ebs.html#ami-block-device-mappings-example

ami_block_device_mappings can be used to specify that we want to attach additional volumes (e.g. /dev/sdb) when the instance is booted from the AMI.

{
  "builders": [
    {
      "type": "amazon-ebs",
      ...
      "launch_block_device_mappings": [ 
        {
          "device_name": "/dev/sda1",
          ...
        }
      ],
      "ami_block_device_mappings": [ 
        {  
          "device_name": "/dev/sdb",
          "volume_size": 30,
          "volume_type": "gp2",
          "delete_on_termination": true
        }
     ],
  ...
}

Note that the device name is "/dev/sdb" which designates a different block device than the root block device (/dev/sda).