LoginSignup
5
5

More than 5 years have passed since last update.

Packer + Chef で Encrypted Data Bag を使用する方法

Last updated at Posted at 2013-11-09

2013/11/20 追記

Packer 0.4.0 で Encrypted Data Bag がサポートされたようです。

追記ここまで

Chef-solo で Encrypted Data Bag を使っています。
Packer でこの Chef の recipe を適用した AMI を作成しようと思ったのでやってみました。

環境

  • Ubuntu 12.04.3 LTS
  • Packer v0.3.11
  • Chef 11.8
  • recipe のパス: ./cookbooks
  • data_bag のパス: ./data_bags
  • Encrypted Data Bag のキーのパス: ./data_bag_key

ゴール

  • Packer を使って Ubutu に Chef のレシピが適用された AMI を作成すること。

Packer 用の json ファイル

packer.json
{
  "variables": {
    "aws_access_key": "",
    "aws_secret_key": ""
  },
  "builders": [{
    "type": "amazon-ebs",
    "access_key": "{{user `aws_access_key`}}",
    "secret_key": "{{user `aws_secret_key`}}",
    "region": "ap-northeast-1",
    "source_ami": "ami-c933a8c8",
    "instance_type": "t1.micro",
    "ssh_username": "ubuntu",
    "ami_name": "ubuntu-{{timestamp}}"
  }],
  "provisioners": [
    {
      "type": "file",
      "source": "data_bag_key",
      "destination": "/tmp/secret"
    },
    {
      "type": "shell",
      "inline": [
        "sudo mkdir -p /etc/chef",
        "sudo mv /tmp/secret /etc/chef/encrypted_data_bag_secret"
      ]
    },
    {
      "type": "chef-solo",
      "cookbook_paths": ["cookbooks"],
      "data_bags_path": "data_bags",
      "run_list": [
        "recipe[my_app_recipe]"
      ]
    }
  ]
}

/etc/chef/encrypted_data_bag_secret がデフォルトで Chef に読まれる Encypted Data Bag Key のファイルになります。

実行コマンド

普通に packer を実行すればいいだけです。

$ packer build --var-file=variables.json packer.json

参考にしたもの

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