LoginSignup
12
12

More than 5 years have passed since last update.

PackerでAMIを自動生成し、画像リサイズサーバを構築

Last updated at Posted at 2015-08-31

はじめに

今回、nginxサーバを構築、SmallLightモジュールの導入をPackerで自動化する実装のご紹介します。

Packerとは

Packerは、マシン・イメージの自動生成や管理をするコマンドラインツールであり、今回はAMIの自動生成を行い、ProvisioningでSmallLightモジュールを導入したnginxサーバをEC2インスタンスに構築します。

Packerのインストール

Packerのインストールに関しては下記をご参照ください。
インストール

Packerの構成

Template:Json形式でPackerの設定ファイルです。
Builders:作成するイメージの種類や設定の詳細を記載します。今回は、EC2インスタンスからAMIを作成します。
Provisioners:イメージを生成する際にイメージ内の設定を行います。シェルスクリプトや Chef や Puppetなどで設定できます。
今回は、シェルスクリプトで設定します。

Packerを使う為にまず、Templateを作成します。

packer.json
{
  "builders": [{
    "type": "amazon-ebs",
    "region": "us-west-2",
    "vpc_id": "×××",
    "subnet_id" : "subnet-×××",
    "source_ami": "ami-×××",
    "security_group_ids": ["×××"],
    "instance_type": "t2.micro",
    "ssh_username": "ec2-user",
    "ssh_timeout": "5m",
    "ami_name": "×××"
  }],
"provisioners": [{
    "type": "shell",
    "inline": [
        "yum -y update"
        ] },
    {
    "type": "shell",
    "scripts": [
        "install_package.sh",
        "install_nginx.sh",
        "install_nginx_SmallLight.sh",
        "set_nginx_SmallLight.sh",
        "set_nginx_configure.sh",
        "nginx_chkconfig.sh"
        ]
  }]
}

SmallLightモジュールを導入したnginxサーバ構築のシェルスクリプトを作成します。

SmallLightとは
動的にサムネイル画像を生成したりできるnginxモジュールです(Apacheも可)。
画像を指定した大きさにリサイズ処理をしてくれます。
nginx+SmallLigthをビルドするのに必要なモジュールのインストール

install_package.sh
#!/bin/bash 
yum install -y pcre-devel zlib-devel openssl-devel gd-devel
yum install -y ImageMagick ImageMagick-devel
yum install http://pkgs.repoforge.org/imlib2/imlib2-1.4.4-1.el6.rf.x86_64.rpm http://pkgs.repoforge.org/imlib2/imlib2-devel-1.4.4-1.el6.rf.x86_64.rpm
yum install -y libunwind perl-ExtUtils-Embed gperftools gperftools-libs gperftools-devel libxslt libxslt-devel GeoIP GeoIP-devel

nginx ダウンロード

install_nginx.sh
#!/bin/bash 
wget http://nginx.org/download/nginx-1.6.2.tar.gz
tar zxfv nginx-1.6.2.tar.gz

SmallLight ダウンロード

install_nginx_SmallLight.sh
#!/bin/bash 
git clone https://github.com/cubicdaiya/ngx_small_light.git /home/ec2-user/nginx-1.6.2./ngx_small_light
cd /home/ec2-user/nginx-1.6.2./ngx_small_light

SmallLightのセットアップ

set_nginx_SmallLight.sh
#!/bin/bash
./set up

nginx のコンパイル

set_nginx_configure.sh
#!/bin/bash 
cd /home/ec2-user/nginx-1.6.2./
./configure --with-pcre --add-module=./ngx_small_light --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/var/run/nginx.pid --lock-path=/var/lock/subsys/nginx --user=nginx --group=nginx --with-file-aio --with-ipv6 --with-http_ssl_module --with-http_spdy_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_geoip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module
make
make install

で、SmallLightモジュールを導入したnginxを構築できるシェルスクリプトが完成。

Provisionerの記述

作成したシェルスクリプトをTemplateの"Provisioners"に配置します。(packer.json参照)
Packerがビルドする際にシェルスクリプトが実行されます。

Packerコマンド

Packerビルドを行います。
packer build packer.json
イメージ作成が実行されます。

AMI確認

packer buildの実行結果の最後に、作成されたAMIのIDが表示されます。
AWSのManagement Consoleで確認できたらカスタムAMIの完了です。

12
12
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
12
12