PackerでWindowsのAMI(Amazon Machine Image)を作成する #Packer

2015.07.07

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

こんにちは、コカコーラ好きのカジです。

先日、Packerの0.8のUpdateで、追加された「Windows AWS Images」が気になり試してみました。 Packerを初めて聞いた方は、ここ読むとわかりやすいと思います。

packer 0.8.1のインストール

私のMacOSXのbrew installでは0.7系のままだったので、手動でダウンロードして展開したフォルダで実行しました。

% unzip -d packer08 packer_0.8.1_darwin_amd64.zip
% ./packer --version
0.8.1

Packer 0.8 Windows Example on AWSのExampleの内容で試してみましたが、作成したAMIを使ってlaunchするとWindowsはセットアップ時に設定したパスワードが不明のため、Windowsパスワードが取得できずログイン出来ませんでした。 kaji-windows-ami-by-packer-01-failure

launch時に初期化することも可能だと思いますが、初期化せずにすむAMIを作成できないか、自分なりに手を加えてみました。

試した内容

以下のAMIを利用してIISが起動するWindows AMIを作成してみました。 Windows_Server-2012-R2_RTM-Japanese-64Bit-Base-2015.06.10 - ami-eab46dea

試した Packer Template

上記のexampleから、IISを起動するため、setup_winrm.txtへ1行追記と、provisionersを修正しました。 provisionersで、sysprepを起動して、Windows AMI パスワードなどの初期化を行ってます。

% cat packer-win-iis.json
{
"builders": [{
"type": "amazon-ebs",
"access_key": "AXXXXXXXXXXXXXXXXXXX",
"secret_key": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"region": "ap-northeast-1",
"source_ami": "ami-eab46dea",
"instance_type": "t2.micro",
"ami_name": "windows-ami {{timestamp}}",
"user_data_file": "{{template_dir}}/setup_winrm.txt",

"communicator": "winrm",
"winrm_username": "Administrator"
}],

"provisioners": [
{
"type": "windows-shell",
"scripts": [
"sysprep.bat"
]
}
]
}
% cat setup_winrm.txt
winrm quickconfig -q
winrm set winrm/config/winrs '@{MaxMemoryPerShellMB="300"}'
winrm set winrm/config '@{MaxTimeoutms="1800000"}'
winrm set winrm/config/service '@{AllowUnencrypted="true"}'
winrm set winrm/config/service/auth '@{Basic="true"}'

netsh advfirewall firewall add rule name="WinRM 5985" protocol=TCP dir=in localport=5985 action=allow
netsh advfirewall firewall add rule name="WinRM 5986" protocol=TCP dir=in localport=5986 action=allow

net stop winrm
sc config winrm start=auto
net start winrm

Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope LocalMachine
Add-WindowsFeature Web-Server ->ExampleからIIS起動を追記
% cat sysprep.bat
"C:Program FilesAmazonEc2ConfigServiceec2config.exe" -sysprep

packerを実行

% ./packer build packer-win-iis.json
amazon-ebs output will be in this color.

==> amazon-ebs: Prevalidating AMI Name...
==> amazon-ebs: Inspecting the source AMI...
==> amazon-ebs: Creating temporary keypair: packer 559bb82b-d468-7bc4-eabf-e558194ebb58
==> amazon-ebs: Creating temporary security group for this instance...
==> amazon-ebs: Authorizing access to port 5985 the temporary security group...
==> amazon-ebs: Launching a source AWS instance...
amazon-ebs: Instance ID: i-2f5b90dd
==> amazon-ebs: Waiting for instance (i-2f5b90dd) to become ready...
==> amazon-ebs: Waiting for auto-generated password for instance...
amazon-ebs: It is normal for this process to take up to 15 minutes,
amazon-ebs: but it usually takes around 5. Please wait.
amazon-ebs:
amazon-ebs: Password retrieved!
==> amazon-ebs: Waiting for WinRM to become available...
==> amazon-ebs: Connected to WinRM!
==> amazon-ebs: Provisioning with windows-shell...
==> amazon-ebs: Provisioning with shell script: sysprep.bat
amazon-ebs:
amazon-ebs: C:UsersAdministrator>"C:Program FilesAmazonEc2ConfigServiceec2config.exe" -sysprep
amazon-ebs: Running in foreground...
amazon-ebs: SysprepUtils: Setting bundle/Sysprep operations for Vista/2008.
amazon-ebs: SysprepUtils: Reading Bundle Properties from C:Program FilesAmazonEc2ConfigServiceSettingsBundleConfig.xml
amazon-ebs: SysprepUtils: Processing property: AutoSysprep
amazon-ebs: SysprepUtils: Processing property: SetRDPCertificate
amazon-ebs: SysprepUtils: Changing plugin Ec2ConfigureRDP state to Disabled
amazon-ebs: SysprepUtils: Changing plugin Ec2OutputRDPCert state to Enabled
amazon-ebs: SysprepUtils: Processing property: SetPasswordAfterSysprep
amazon-ebs: SysprepUtils: Changing plugin Ec2SetPassword state to Enabled
amazon-ebs: SysprepUtils: Changing plugin Ec2WindowsActivate state to Enabled
amazon-ebs: SysprepUtils: Changing plugin Ec2HandleUserData state to Enabled
amazon-ebs: SysprepUtils: Changing plugin Ec2DynamicBootVolumeSize state to Enabled
amazon-ebs: SysprepUtils: Sysprep command: 'C:windowssystem32sysprepsysprep.exe'
amazon-ebs: SysprepUtils: Sysprep args: '"/unattend:C:Program FilesAmazonEc2ConfigServicesysprep2008.xml" /oobe /shutdown /generalize'
==> amazon-ebs: Stopping the source instance...
==> amazon-ebs: Waiting for the instance to stop...
==> amazon-ebs: Creating the AMI: windows-ami 1436268587
amazon-ebs: AMI: ami-9064cb90
==> amazon-ebs: Waiting for AMI to become ready...
==> amazon-ebs: Terminating the source AWS instance...
==> amazon-ebs: Cleaning up any extra volumes...
==> amazon-ebs: Deleting temporary security group...
==> amazon-ebs: Deleting temporary keypair...
Build 'amazon-ebs' finished.

==> Builds finished. The artifacts of successful builds are:
--> amazon-ebs: AMIs were created:

ap-northeast-1: ami-9064cb90

作成したWindows AMIをLaunch

RDPでログインできて、IISが起動してました。 kaji-windows-ami-by-packer-02-launch kaji-windows-ami-by-packer-03-iis

まとめ

sysprepをスクリプト実行できることを知らなかったので、勉強になりました。 Windows EC2や、Packerについてもっと知識をつけていきたいと思います。