orenoblog

エンジニアになりたいExcel方眼紙erの物語

VagrantfileにAPIキーを記述しないようにした

ぼーっとvagrant-awsを利用していたせいかVagrantfileにaccess_key_idやsecret_keyをベタ書きしたままgit commitしてしまった。

他の方には読まれたくない情報を誤ってcommitしないようにYAMLからロードするように変更してみた。

複数人がVagrantで共通のAMIを利用して開発するにはいいのかなあ〜と思います。

ハッシュの表記方法等がバージョンで異なるみたいなのでRuby勉強しないと・・

パーフェクトRuby電子書籍版でないかしら..

  • Vagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :
require 'yaml'
# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = "2"

attributes = YAML.load_file(".aws/config.yml")

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

  config.vm.box = "dummy"

  config.vm.provider :aws do |aws, override|
    aws.access_key_id = attributes["aws_provider"]["access_key_id"]
    aws.secret_access_key = attributes["aws_provider"]["secret_access_key"]
    aws.keypair_name = attributes["aws_provider"]["keypair_name"]
    aws.region = attributes["aws_provider"]["region"]
    aws.ami = "ami-99107d98" #suz_lab_centos-core-6.5.1
    aws.security_groups = attributes["aws_provider"]["security_groups"]
    override.ssh.username = attributes["aws_provider"]["ssh_username"]
    override.ssh.private_key_path = attributes["aws_provider"]["ssh_private_key_path"]
  end

end
  • config.yml
aws_provider:
    access_key_id: <your_access_key_id>
    secret_access_key: <your_secret_key>
    keypair_name: <your_keypair_name>
    instance_type: c3.large
    region: ap-northeast-1
    security_groups: default
    ssh_username: root
    ssh_private_key_path: <pemfile>