1
5

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+AmazonEC2を触ってみた

Last updated at Posted at 2017-02-26

概要

今更ながらVagrantを使ってAmazonEC2のインスタンスを管理できるようにしてみた
流行はすでに終わっているし、情報もすぐに出てくるが、自分の備忘録として残しておく

環境

  • OS: Windows 10
  • コンソール: Bash on Ubuntu on Windows
  • Vagrant: 1.9.1
  • vagrant-aws: 0.7.2

実施内容

  • Vagrantのインストール
  • vagrant-awsの導入
  • アクセスキーの発行
  • キーペアの設定
  • セキュリティグループの作成
  • Vagrantの認証設定
  • Vagrantfileの作成

Vagrantのインストール

このサイトからDebian 64bitのDEBファイルを取得し、Bash on Ubuntu on Windowsでインストール

$ dpkg -i vagrant_1.9.1_x86_64.deb
$ vagrant -v
Vagrant 1.9.1

vagrant-awsの導入

$ vagrant plugin install vagrant-aws
Installing the 'vagrant-aws' plugin. This can take a few minutes...
Fetching: ipaddress-0.8.3.gem (100%)
Fetching: formatador-0.2.5.gem (100%)
Fetching: excon-0.55.0.gem (100%)
Fetching: fog-core-1.43.0.gem (100%)
Fetching: fog-json-1.0.2.gem (100%)
Fetching: fog-xml-0.1.2.gem (100%)
Fetching: trollop-2.1.2.gem (100%)
Fetching: CFPropertyList-2.3.5.gem (100%)
Fetching: rbvmomi-1.9.4.gem (100%)
Fetching: fission-0.5.0.gem (100%)
Fetching: inflecto-0.0.2.gem (100%)
Fetching: xml-simple-1.1.5.gem (100%)
Fetching: fog-xenserver-0.2.3.gem (100%)
Fetching: fog-vsphere-1.7.1.gem (100%)
Fetching: fog-voxel-0.1.0.gem (100%)
Fetching: fog-vmfusion-0.1.0.gem (100%)
Fetching: fog-terremark-0.1.0.gem (100%)
Fetching: fog-storm_on_demand-0.1.1.gem (100%)
Fetching: fog-softlayer-1.1.4.gem (100%)
Fetching: fog-serverlove-0.1.2.gem (100%)
Fetching: fog-sakuracloud-1.7.5.gem (100%)
Fetching: fog-riakcs-0.1.0.gem (100%)
Fetching: fog-radosgw-0.0.5.gem (100%)
Fetching: fog-rackspace-0.1.4.gem (100%)
Fetching: fog-profitbricks-3.0.0.gem (100%)
Fetching: fog-powerdns-0.1.1.gem (100%)
Fetching: fog-openstack-0.1.20.gem (100%)
Fetching: fog-local-0.3.1.gem (100%)
Fetching: fog-google-0.1.0.gem (100%)
Fetching: fog-ecloud-0.3.0.gem (100%)
Fetching: fog-dynect-0.0.3.gem (100%)
Fetching: fog-cloudatcost-0.1.2.gem (100%)
Fetching: fog-brightbox-0.11.0.gem (100%)
Fetching: fog-aws-1.2.0.gem (100%)
Fetching: fog-atmos-0.1.0.gem (100%)
Fetching: fog-aliyun-0.1.0.gem (100%)
Fetching: iniparse-1.4.2.gem (100%)
Fetching: fog-1.38.0.gem (100%)
Fetching: vagrant-aws-0.7.2.gem (100%)
Installed the plugin 'vagrant-aws (0.7.2)'!
$ vagrant box add dummy https://github.com/mitchellh/vagrant-aws/raw/master/dummy.box
==> box: Box file was not detected as metadata. Adding it directly...
==> box: Adding box 'dummy' (v0) for provider:
    box: Downloading: https://github.com/mitchellh/vagrant-aws/raw/master/dummy.box
==> box: Successfully added box 'dummy' (v0) for 'aws'!

アクセスキーの発行

詳細はAWS IAMのドキュメントを参照
今回は手順だけ

  1. IAMコンソールにアクセス
  2. ユーザタブにて、AmazonEC2を操作できるユーザを選択
  3. 認証情報タブからアクセスキーを発行
  4. CSVファイルをダウンロード(大事に保管すること)

キーペアの設定

詳細はAmazon EC2のドキュメントを参照
今回は手順だけ

  1. Amazon EC2コンソールにアクセス
  2. キーペアタブを選択
  3. キー作成ボタンを選択
  4. キーペア名を入力し、作成ボタンを選択(今回はdefaultと入力)
  5. PEMファイルを~/.ssh以下に配置

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

詳細はAmazon EC2のドキュメントを参照
今回は手順だけ

  1. Amazon EC2コンソールにアクセス
  2. セキュリティグループを選択
  3. セキュリティグループの作成ボタンを選択
  4. セキュリティグループ名を入力(今回はsecurity)
  5. ルール追加にて、自分のグローバルアドレスを使ってアクセスできるように設定

Vagrantの認証設定

Vagrantfileに認証情報を書きたくないので、今回は認証情報を環境変数に設定するスクリプトを作成

privateKeys.sh
#!/bin/sh
export AWS_ACCESS_KEY_ID='AKIXXXXXXXXXXXXXXXXX'
export AWS_SECRET_ACCESS_KEY='fXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'

Vagrantfileの作成

Vagrantfile
Vagrant.configure("2") do |config|
  # Vagrant Box
  config.vm.box = "dummy"

  # Rsync Directory
  config.vm.synced_folder "setup", "/vagrant", type: "rsync"

  config.vm.provider :aws do |aws, override|
    # AWS Account
    aws.access_key_id = ENV['AWS_ACCESS_KEY_ID']
    aws.secret_access_key = ENV['AWS_SECRET_ACCESS_KEY']

    # Instance Configuration
    aws.tags = {
      'Name' => 'DevelopEnvironment'
    }
    aws.instance_type = "t2.micro"
    aws.ami = "ami-c68fc7a1"
    aws.region = "ap-northeast-1"
    aws.availability_zone = "ap-northeast-1c"
    aws.security_groups = ['security']
    aws.elastic_ip = true

    # Login Configuration
    aws.keypair_name = "default"
    override.ssh.username = "ubuntu"
    override.ssh.private_key_path = "~/.ssh/default.pem"

    # SSH accessable
    aws.user_data = "sed -i -e 's/^\\(Defaults.*requiretty\\)/#\\1/' /etc/sudoers"

  end
end

動作確認

$ source privateKeys.sh
$ vagrant up
Bringing machine 'default' up with 'aws' provider...
==> 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-c68fc7a1
==> default:  -- Region: ap-northeast-1
==> default:  -- Availability Zone: ap-northeast-1c
==> default:  -- Keypair: default
==> default:  -- Elastic IP: true
==> default:  -- User Data: yes
==> default:  -- Security Groups: ["security"]
==> default:  -- User Data: sed -i -e 's/^\(Defaults.*requiretty\)/#\1/' /etc/sudoers
==> default:  -- Block Device Mapping: []
==> default:  -- Terminate On Shutdown: false
==> default:  -- Monitoring: false
==> default:  -- EBS optimized: false
==> default:  -- Source Destination check:
==> default:  -- Assigning a public IP address in a VPC: false
==> default:  -- VPC tenancy specification: default
==> 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: /mnt/c/Users/kazuyoshi/aws-training/setup/ => /vagrant

参考

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?