36
34

More than 5 years have passed since last update.

Vagrant でEC2インスタンス構築

Posted at

動機

AWSを触り始めたら、インフラ構築をできる限り自動化したくなった!

目的

Windowsで、Vagrant/Chef Soloを使ってAWS上にいい感じのEC2インスタンスを作成する

この記事の範囲

VagrantでEC2インスタンス(Amazon Linux)を構築。

Vagrant

前提条件

・VirtualBox(https://www.virtualbox.org/wiki/Downloads)
  →特に使わないけど、Vagrantインストールに必要らしい

Vagrantインストール

http://www.vagrantup.com/downloads.html
とりあえず、最新を入れておけばいいんだと思う。
…間違って古いバージョンを入れて、AWSと通信できないって言われる事案が発生(´・ω・`)

EC2に接続するために必要な手順

  • aws用プラグインのインストール

以降、シェル操作はmsysGitについてる「Git Bash」で操作
※最近はmsysGitでなく、Git for Windowsって言うみたいっすね

$ vagrant plugin install vagrant-aws
  • dummyのboxを用意

Vagrantの作者にて、GitHubに用意して頂いているようです。
手順としては公式なんですね。

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

Vagrantfileの用意

個人的に「なんのこっちゃ」その1。
ファイル名が「Vagrantfile」っていう名前なんですね(今更…
作りたい仮想マシン用のフォルダを用意して(例:C:\Vagrant\MyMachine\)、そこに「Vagrantfile」なるファイルを置く。
構成自動化の世界では、Xxxfileなるファイルを置くのは普通なんですね…(´・ω・`)

何を書けばいいか、中身についてはのサイトを参照されたし。
本家:https://github.com/mitchellh/vagrant-aws
参考:http://knowledge.sakura.ad.jp/tech/1552/
 ⇒2ページ目にVagrantfileの詳細な日本語説明があって、理解の助けになりました

参考サイトをほぼそのまま使って作成した、利用中のVagrantfileも晒しておきます。
さすがにキーペア等はマスクしておきますが…

Vagrant.configure("2") do |config|
  # DummyのBoxを指定
  config.vm.box = "aws-dummy"

  config.vm.provider :aws do |aws, override|
    # EC2インスタンス作成権限のあるIAMユーザーのAccess Keys
    aws.access_key_id = "xxxxxxxxx"
    aws.secret_access_key = "xxxxxxxxxxxxxxxxxx"

    # [EC2] → [NETWORK & SECURITY] → [Key Pairs]で設定できるキーペア名。
    # 作成するEC2インスタンスにSSH接続するときに必要になる
    aws.keypair_name = "HogeKey"

    # インスタンスタイプ。一番安いやつ
    aws.instance_type = "t2.micro"

    # リージョンを選択
    aws.region = "ap-northeast-1"

    # AZを選択。2014/8/5時点で「1a or 1c」しか選べないみたい。
    aws.availability_zone = "ap-northeast-1c"

    # VPCのSecurity GroupのGroup Nameを記述。
    aws.security_groups = "securityGroupName"

    # 使用するイメージを選択。下記はAmazon Linux。
    # ほかのイメージも、EC2インスタンスをGUIで作成時に、ちょこっと記載があるのでそれを転記。
    aws.ami = "ami-29dc9228"

    # AMIごとに固定。Amazon Linuxの場合は、ログインユーザーはec2-user固定
    override.ssh.username = "ec2-user"

    # PrivateKeyの在処を指定
    override.ssh.private_key_path = "~/.ssh/HogeKey.pem"

    # Tagを記載。Nameは記載したほうがあとでわかりやすい
    aws.tags = { 'Name' => 'MyMachineTest' }

  end
end

vagrant up!

いよいよ起動。
Vagrantfileが、C:\Vagrant\MyMachineにあると仮定。

$ cd /c/Vagrant/MyMachine
$ vagrant up --provider aws
Bringing machine 'default' up with 'aws' provider...
==> default: HandleBoxUrl middleware is deprecated. Use HandleBox instead.
==> default: This is a bug with the provider. Please contact the creator
==> default: of the provider you use to fix this.
==> default: Warning! The AWS provider doesn't support any of the Vagrant
==> default: high-level network configurations (`config.vm.network`). They
==> default: will be silently ignored.
==> default: Launching an instance with the following settings...
==> default:  -- Type: t2.micro
==> default:  -- AMI: ami-29dc9228
==> default:  -- Region: ap-northeast-1
==> default:  -- Availability Zone: ap-northeast-1c
==> default:  -- Keypair: HogeKey
==> default:  -- Security Groups: ["securityGroupName"]
==> default:  -- Block Device Mapping: []
==> default:  -- Terminate On Shutdown: false
==> default:  -- Monitoring: false
==> default:  -- EBS optimized: false
==> default:  -- Assigning a public IP address in a VPC: false
==> default: Waiting for instance to become "ready"...
==> default: Waiting for SSH to become available...
==> default: Machine is booted and ready for use!
==> default: Rsyncing folder: /cygdrive/c/Vagrant/MyMachine/ => /vagrant
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

※少し上記のログは加工してます

なんか怪しげなエラー出てる…けどでけた。
早速ssh接続。

$ vagrant ssh

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

https://aws.amazon.com/amazon-linux-ami/2014.03-release-notes/
8 package(s) needed for security, out of 19 available
Run "sudo yum update" to apply all updates.
[ec2-user@ip-xx-xx-xx-xx ~]$ 

つながったよう。
用が済んだら破棄。

$ vagrant destroy
    default: Are you sure you want to destroy the 'default' VM? [y/N] y
==> default: Terminating the instance...

これで、EC2上はTerminate状態になります。

残りタスク

  • vagrant up時の謎のエラーを何とかする
  • vagrant+Chef構成自動化まで一気通貫で終わらせる
36
34
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
36
34