LoginSignup
53
50

More than 5 years have passed since last update.

VagrantでAmazon EC2を操作する

Last updated at Posted at 2014-03-07

試した環境

  • Max OSX 10.9.1
  • Vagrant 1.4.3
  • Vagrant aws plugin 0.4.1

参考にしたサイト

https://github.com/mitchellh/vagrant-aws
http://d.hatena.ne.jp/naoya/20130315/1363340698

事前準備

Vagrantのインストール

インストールや使い方は、こちらを参考に。

Vagrant-AWS pluginのインストール

AWSを扱うためのPluginをインストールする。

$ vagrant plugin install vagrant-aws

Amazon EC2の準備

VagrantfileにEC2の情報を定義するため、事前に準備する。

Access Key と Secret Access Keyの取得

Access Key ID and Secret Access Key

Secret Access Keyは、WEB上から確認できたが、近々確認できなくなるよう。
手元にない場合は、下記ページから再発行しておく。

セキュリティグループの作成

EC2のコンソールから、セキュリティグループを作成する。
Vagrantを実行するクライアントから、SSHが通るグループ設定にしておく。

Key Pairs

EC2のコンソールから、Key Pairsをダウンロードしておく。
未作成の場合は、作成しておく。

Vagrantの実行

dummy boxの追加

Vagrantの仕様上、Boxを登録しておく必要がある。
中身は空?のdummy boxが用意されているのでそれを追加しておく。

$ vagrant box add dummy https://github.com/mitchellh/vagrant-aws/raw/master/dummy.box

Vagrantfileの作成

initコマンドでデフォルトのVagnratfileを生成する。

$ vagrant init

生成されたファイルを以下のように修正する。
詳細は、公式ページを参考に。


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のアクセスキー]"
    aws.secret_access_key = "[AWSのシークレットキー]"
    aws.keypair_name = "[AWSのキーペア名]"

    aws.ami = "ami-0d13700c" # AMIのID
    aws.instance_type = "t1.micro" # インスタンスタイプ
    aws.security_groups = [ 'vagrant-group' ] # セキュリティグループ    
    aws.region = "ap-northeast-1" # 東京リージョン

    override.ssh.username = "ec2-user"
    override.ssh.private_key_path = "[秘密鍵のパス]"
  end
endß
  • AWSによって提供されるAMIのIDは、AMI選択画面で確認できる。

  スクリーンショット 2014-03-05 1.00.13.png

  • リージョンは指定しないと、デフォルトでは東京が指定されない。セキュリティグループがないと言われて嵌った。

インスタンスの作成

vagrant up --provider=aws コマンドでAWSにインスタンスが作成される。

$ vagrant up --provider=aws

・・・
[default] Rsyncing folder: /Users/m/Vagrant/ => /vagrant
An error occurred while executing multiple actions in parallel.
Any errors that occurred are shown below.

An error occurred while executing the action on the 'default'
machine. Please handle this error then try again:

The following SSH command responded with a non-zero exit status.
Vagrant assumes that this means the command failed!

mkdir -p '/vagrant'

Stdout from the command:
Stderr from the command:

sudo: sorry, you must have a tty to run sudo

最後にエラーが出るが、問題なく作成されている。

スクリーンショット 2014-03-07 14.02.22.png

インスタンスへのログイン

vagrant sshでログインできる。

$ vagrant ssh

       __|  __|_  )
       _|  (     /   Amazon Linux AMI
      ___|\___|___|

[ec2-user@ip-10-134-207-199 ~]$ 

インスタンスの停止

vagrant halt でインスタンスを停止できる。

$ vagrant halt

インスタンスの削除

vagrant destroy でインスタンスを削除できる。

$ vagrant destroy
53
50
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
53
50