LoginSignup
2

More than 5 years have passed since last update.

体育会系 Amazon EC2 を Vagrant 経由で起動する手順

Last updated at Posted at 2016-04-06

参考

有難うございます。

事前の準備

  • IAM ユーザーを作成し Managed Policy の AmazonEC2FullAccess をアタッチしておく
  • IAM ユーザーのアクセスキーID を確認しておく
  • IAM ユーザーのシークレットアクセスキーを確認しておく
  • SSH key にて鍵を発行しておく
  • セキュリティグループの ID を確認しておく
  • VPC のサブネット ID を確認しておく
  • Vagrant の導入、vagrant-aws の導入

本記事の環境について

vagrant を実行する環境

vagrantを実行する環境
ProductName:    Mac OS X
ProductVersion: 10.11.4
BuildVersion:   15E49a

vagrant のバージョン

vagrantのバージョン
Installed Version: 1.8.1
Latest Version: 1.8.1

vagrant-aws の導入

command
vagrant plugin install vagrant-aws

Vagrantfile の作成

プロジェクトディレクトリの作成

command
mkdir ~/path/to/vagrant_ec2

vagrant init

command
cd ~/path/to/vagrant_ec2
vagrant init

vagrant init を実行すると Vagrantfile が生成される。

Vagrantfile

Vagrantfile
#
# - ami-a21529cc を利用して Ubuntu 14.04 インスタンスを t2.micro で起動する
# - VPC 内にインスタンスを起動する
#
Vagrant.configure(2) do |config|
  config.vm.box = "dummy"
  config.vm.box_url = "https://github.com/mitchellh/vagrant-aws/raw/master/dummy.box"

  config.vm.provider :aws do |aws, override|
    aws.access_key_id = "${アクセスキー ID}"
    aws.secret_access_key = "${シークレットアクセスキー}"
    aws.region = "ap-northeast-1"
    aws.keypair_name = "${KEYNAME}"
    aws.ami = "ami-a21529cc"
    override.ssh.username = "ubuntu"
    override.ssh.private_key_path = "/path/to/${KEYNAME}.pem"
    aws.security_groups = ["${SECURITY_GROUP_ID}", "${SECURITY_GROUP_ID}", "${SECURITY_GROUP_ID}", "${SECURITY_GROUP_ID}", "${SECURITY_GROUP_ID}"] 
    aws.subnet_id = "${SUBNET_ID}"
    aws.instance_type = "t2.micro"  
  end  
end

本来はアクセスキー ID やシークレットアクセスキーを直接 Vagrantfile に書くのはイケてないので環境変数に登録しておくと良い。

command
export AWS_ACCEESS_KEY_ID="AKxxxxxxxxxxxxxxxxxxxxxxxxxx"
export AWS_SECRET_ACCESS_KEY="xxxxxxxxxxxxxxxxxxxxxxxxxxxx"

環境変数に登録した場合には Vagrantfile は以下のように書いておく。

Vagrantfile
  config.vm.box = "dummy"
  config.vm.box_url = "https://github.com/mitchellh/vagrant-aws/raw/master/dummy.box"

  config.vm.provider :aws do |aws, override|
    aws.access_key_id = ENV["AWS_ACCEESS_KEY_ID"]
    aws.secret_access_key = ENV["AWS_SECRET_ACCESS_KEY"]
    aws.region = "ap-northeast-1"
    aws.keypair_name = "${KEYNAME}"
    aws.ami = "ami-a21529cc"
    override.ssh.username = "ubuntu"
    override.ssh.private_key_path = "/path/to/${KEYNAME}.pem"
    aws.security_groups = ["${SECURITY_GROUP_ID}", "${SECURITY_GROUP_ID}", "${SECURITY_GROUP_ID}", "${SECURITY_GROUP_ID}", "${SECURITY_GROUP_ID}"] 
    aws.subnet_id = "${SUBNET_ID}"
    aws.associate_public_ip = true
    aws.instance_type = "t2.micro"  
  end  
end

仮想マシンの起動、ログイン、停止、破棄

仮想マシンの起動

command
vagrant up --provider aws 

仮想マシンへのログイン

command
vagrant ssh

仮想マシンの停止

command
vagrant halt

仮想マシンの破棄

command
vagrant destroy

以上

Amazon EC2 を Vagrant 経由で起動する手順でした。

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
2