インスタンスプロファイルでビルドするには、
https://www.packer.io/docs/builders/amazon.html
上記URLからポリシーをコピーしてIAMポリシーを作っておく
IAMロールを作成するとき作ったポリシーをアタッチする。
templateのjsonファイルに"aws_iam_instance_profile"としてロール名を設定する。
{
"variables": {
"aws_access_key": "",
"aws_secret_key": "",
"aws_source_ami": "ami-eec1c380",
"aws_region_name": "ap-northeast-1",
"aws_instance_type": "t2.micro",
"aws_iam_instance_profile": "★ここにIAMロール名を設定★",
"aws_vpc_id": "vpc-xxxxx",
"aws_subnet_id": "subnet-xxxxxx",
"aws_create_date": "",
"aws_ami_basename": "AMI_xxxx_",
"commit_hash": ""
},
"builders": [{
"type": "amazon-ebs",
"access_key": "{{user `aws_access_key`}}",
"secret_key": "{{user `aws_secret_key`}}",
"region": "{{user `aws_region_name`}}",
"source_ami": "{{user `aws_source_ami`}}",
"instance_type": "{{user `aws_instance_type`}}",
"ssh_username": "centos",
"ssh_timeout": "10m",
"ssh_pty": "true",
"subnet_id": "{{user `aws_subnet_id`}}",
"vpc_id": "{{user `aws_vpc_id`}}",
"associate_public_ip_address": true,
"ami_name": "AMI_standard_{{user `aws_create_date`}}",
"ami_regions": ["ap-northeast-1"],
"ami_users": [
"xxxx許可したいアカウント番号xxxxxx"
],
"iam_instance_profile": "{{user `aws_iam_instance_profile`}}",
"tags": {
"Name": "{{user `aws_ami_basename`}}{{user `aws_create_date`}}",
"CommitHash": "{{user `commit_hash`}}"
},
"run_tags": {
"CommitHash": "{{user `commit_hash`}}"
},
"run_volume_tags": {
"CommitHash": "{{user `commit_hash`}}"
}
}],
"provisioners": [{
"type": "shell",
"execute_command": "sudo bash {{.Path}}",
"script": "provision/install_chef.sh"
},{
"type": "chef-solo",
"skip_install": true,
"cookbook_paths": ["provision/cookbooks"],
"run_list": ["base"]
},{
"type": "shell",
"execute_command": "sudo sh -x {{ .Path }} {{user `aws_region_name`}}",
"script": "ami-xxxx.sh"
},{
"type": "shell",
"execute_command": "sudo -E -S sh '{{ .Path }}'",
"script": "provision/cloud-init.sh"
},{
"type": "file",
"source": "tests",
"destination": "/tmp"
},{
"type": "shell",
"execute_command": "sudo sh -x {{ .Path }}",
"script": "scripts/serverspec.sh"
}]
}
みたとおりprovisionersに複数登録できる。chefとbashとserverspecとか続けて実行可能。
templateとスクリプト群をgitlabにプロジェクト登録しとく
jenkinsにgitlabからcloneしてpacker動かすjobを作る
ソースコードの管理、GitでcloneするURLを指定、
クレデンシャルを管理から登録しといて選択するか、
/var/lib/jenkins/.ssh/config直に書いてつなげられるようにしとく
$ sudo cat /var/lib/jenkins/.ssh/config
Host *
StrictHostKeyChecking no
UserKnownHostsFile=/dev/null
Host 10.xx.xx.xx
HostName git01.hoge.net
User jenkins
IdentityFile ~/.ssh/id_rsa
Port 22
Webhookの仕込み
webhookとはgitlabにコミットした瞬間にjenkinsのビルドが走るように仕込むようなのです。
”Build when a change is pushed to GitLab. GitLab CI Service”にチェックを入れて出てきたURLを
プロジェクトの画面の歯車マークあたりからWebhookの設定画面にいき、URLを貼って保存。ssl_verifyは無効にしとく。
これをすることでコミットしたのをリモートリポジトリにプッシュした瞬間にjenkinsのビルドが開始される。(Webhookが開始される処理はpushとかmergeとか指定できる)
パスワードマスクするMask passwordsプラグイン使ってAWSの鍵とクレデンシャルを変数登録しとく
シェルの実行でPackerビルド処理を書く
#XXX: Packerをインストールする
rm -f packer.zip
rm -rf .packer
curl -L -o packer.zip https://releases.hashicorp.com/packer/0.10.1/packer_0.10.1_linux_amd64.zip
mkdir -p .packer
(cd .packer && unzip ../packer.zip)
./.packer/packer build \
-var "aws_access_key=${AWS_ACCESS_KEY_ID}" \
-var "aws_secret_key=${AWS_SECRET_ACCESS_KEY}" \
-var "aws_create_date=`date +%Y%m%d`-`date +%s`" \
-var "commit_hash=`git rev-parse HEAD`" \
-color=false \
template_chef.json
ansibleでprovisionさんこう
http://dev.classmethod.jp/server-side/ansible/build_ami_with_packer_using_ansible/