解決

"builders": [{
  "source_ami_filter": {
    "filters": {
      "name": "amzn-ami-hvm-*-x86_64-gp2"
    },
    "owners": ["137112412989"],
    "most_recent": true
  },

10分で理解するPacker - Qiita

メモ

-gp2とは?

ボリュームの種類。他に、 gp2 io1 st1 sc1

Amazon EBS ボリュームの種類 - Amazon Elastic Compute Cloud

ownersを取得するには?

ownersとは aws ec2 describe-images --image-ids ami-xxx

$ aws ec2 describe-images --image-ids ami-06cd52961ce9f0d85
{
    "Images": [
        {
            "VirtualizationType": "hvm",
            "Description": "Amazon Linux AMI 2018.03.0.20180811 x86_64 HVM GP2",
            "Hypervisor": "xen",
            "ImageOwnerAlias": "amazon",
            "EnaSupport": true,
            "SriovNetSupport": "simple",
            "ImageId": "ami-06cd52961ce9f0d85",
            "State": "available",
            "BlockDeviceMappings": [
                {
                    "DeviceName": "/dev/xvda",
                    "Ebs": {
                        "Encrypted": false,
                        "DeleteOnTermination": true,
                        "VolumeType": "gp2",
                        "VolumeSize": 8,
                        "SnapshotId": "snap-0be43cb27ae60c978"
                    }
                }
            ],
            "Architecture": "x86_64",
            "ImageLocation": "amazon/amzn-ami-hvm-2018.03.0.20180811-x86_64-gp2",
            "RootDeviceType": "ebs",
            "OwnerId": "137112412989", #<= ★コレ
            "RootDeviceName": "/dev/xvda",
            "CreationDate": "2018-08-11T02:30:08.000Z",
            "Public": true,
            "ImageType": "machine",
            "Name": "amzn-ami-hvm-2018.03.0.20180811-x86_64-gp2"
        }
    ]
}
137112412989 = AWS(アジアパシフィック東京?)

他の企業(Microsoft、Redhatなど)の管理する物を使用したい場合は変更する。

packer source_ami_filterメモ - Qiita

packer実行時

ssh接続できない

Build 'amazon-ebs' errored: ssh: handshake failed: ssh: unable to authenticate, attempted methods [none publickey], no supported methods remain builders"ssh_username": "ec2-user" を設定。

ansibleが見つからない

Build 'amazon-ebs' errored: Error executing Ansible: ANSIBLE_FORCE_COLOR=1 PYTHONUNBUFFERED=1 ansible-playbook could not be found.

"provisioners": [
 {
   "type": "shell",
   "inline": [
     "sudo sed -i 's%^PATH=%&:/usr/local/bin%' /root/.bash_profile",
     "sudo sed -i 's%^.*secure_path.*$%&:/usr/local/bin%' /etc/sudoers",
     "sudo pip install --upgrade pip",
     "sudo pip install --upgrade cloud-init",
     "sudo pip install ansible"
   ]
  },

タグにAMI情報を付与

"tags": {
  "Base_AMI_ID": "",
  "Base_AMI_Name": ""
  .
  .
  .
}

【小ネタ】PackerでAMIを作成する時にAMI_IDとAMI名を付けませんか? | DevelopersIO