LoginSignup
3
2

More than 3 years have passed since last update.

packerでdockerがインストール済みのamazon linux2のAMIを作成する

Posted at
amazon-linux2-docker.json
{
  "min_packer_version": "1.4.2",
  "variables": {
    "aws_region": "ap-northeast-1"
  },
  "builders": [
    {
      "ami_name": "docker-amazon-linux2-{{isotime | clean_ami_name}}",
      "ami_description": "An Amazon Linux 2 AMI that has Docker installed.",
      "instance_type": "t2.micro",
      "name": "amazon-linux-ami",
      "region": "{{user `aws_region`}}",
      "type": "amazon-ebs",
      "associate_public_ip_address": true,
      "source_ami_filter": {
        "filters": {
          "virtualization-type": "hvm",
          "architecture": "x86_64",
          "name": "*amzn2-ami-hvm-*",
          "block-device-mapping.volume-type": "gp2",
          "root-device-type": "ebs"
        },
        "owners": [
          "amazon"
        ],
        "most_recent": true
      },
      "ssh_username": "ec2-user"
    }
  ],
  "provisioners": [
    {
      "type": "shell",
      "script": "{{template_dir}}/setup_amazon-linux.sh",
      "only": [
        "amazon-linux-ami"
      ]
    }
  ]
}
setup_amazon-linux.sh
#!/bin/sh
set -e

SCRIPT=`basename "$0"`

echo "[INFO] [${SCRIPT}] Setup git"
sudo yum install -y git

echo "[INFO] [${SCRIPT}] Setup docker"
sudo yum install -y docker
sudo service docker start
sudo usermod -a -G docker ec2-user
sudo systemctl enable docker

ビルド

packer build amazon-linux2-docker.json
3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2