9
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

vagrant-awsのアクセスキー管理

Posted at

はじめに

vagrant-awsは凄く便利ですが、Vagrantfileでaccess_key_idsecret_access_keyを平文で設定するとGithubなどで公開できないので面倒です。アクセスキーは、AWS CLIやAWS SDKで共通化された、~/.aws/credentialsに登録するので、ここから持ってくる方法を検討しました。

結果

AWS SDK for Rubyを使って簡単に実現できました。以下の通り、AWS.config.credentialsを使えば、defaultのアクセスキーが取得できます。

VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = "dummy"

  config.vm.provider :aws do |aws, override|

    aws.access_key_id = AWS.config.credentials[:access_key_id]
    aws.secret_access_key = AWS.config.credentials[:secret_access_key]
    	
    aws.keypair_name = "vagrant"
    aws.ami = "ami-b66ed3de"
    aws.instance_type = "t2.micro"
    aws.security_groups = ['webserver']

    override.ssh.username = "ec2-user"
    override.ssh.private_key_path = "~/.aws/vagrant.pem"

  end
end

事前準備

VagrantfileからAWS SDK for Rubyを呼び出せる必要があります。通常のRubyだと、gem install aws-sdkでインストールしますが、Vagrantの場合はVagrantのpluginとしてインストールします。

$ vagrant plugin install aws-sdk

$ vagrant plugin list
aws-sdk (1.59.0)
vagrant-aws (0.5.0)
vagrant-login (1.0.1, system)
vagrant-omnibus (1.4.1)
vagrant-share (1.1.2, system)
9
9
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
9
9

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?