LoginSignup
3
2

More than 5 years have passed since last update.

awspec使ってみた

Posted at

試しにawspecを触ってみたのでメモ

初期セットアップ(ディレクトリ等)

# awspec init

下記の構成になる
# tree -a
├── .rspec
├── Gemfile
├── Gemfile.lock
├── README.md
├── Rakefile
└── spec
    ├── .gitignore
    └── spec_helper.rb

Profile(secrets.yml)の作成

secrets.ymlを使用することによりアカウントの指定が楽になる
個人的には --profile よりこっちが安全だと思うのでこっちを推奨
作成しない場合はawspec実行時に --profile=PROFILE で指定が可能

# vi spec/secrets.yml

region: REGION
aws_access_key_id: ACCESSKEY
aws_secret_access_key: SECRETKEY

現在の設定からgenerate

vpc上に存在してるEC2の情報を出力する
generateしたファイルをベースにspecファイルを作るのが楽だった

結果がec2_spec.rbとして出力する。
その他のServiceについても同様に出力を行う

# awspec generate ec2 vpc-xxxxxx >> spec/ec2_spec.rb

generateしたままでは使用できないのでファイルを編集

# vi spec/ec2_spec.rb
1行目にrequireを追加
require 'spec_helper'

describe ec2('INSTANCE_NAME') do
  it { should exist }
  it { should be_running }
  its(:instance_id) { should eq 'i-xxxxxxxx' }
  its(:image_id) { should eq 'ami-xxxxxxxx' }
  its(:private_dns_name) { should eq 'ip-10-0-0-1.ap-northeast-1.compute.internal' }
  its(:public_dns_name) { should eq '' }
  its(:instance_type) { should eq 'INSTANCE_TYPE' }
  its(:private_ip_address) { should eq '10.0.0.1' }
  its(:public_ip_address) { should eq 'xx.xx.xx.xxx' }
  it { should have_security_group('SG_NAME') }
  it { should have_security_group('SG_NAME') }
  it { should belong_to_vpc('VPC_NAME') }
  it { should have_eip('xx.xx.xx.xxx') }
  it { should have_ebs('vol-xxxxxxxx') }
end

ファイルの書き方は基本的にServerspecと同様

it { should exist } とか it { hould have_xxx('xxxxxx') } といった形
Serverspec書いていたらわかりやすいはず
詳細はServerspecawspec を参照

Spec実施

コマンドを実行する成功していれば緑表示、失敗している部分は赤表示になります

# rake spec
ec2 'web01'
  should exist
  should be running
  should have security group "web"
  should have security group "prod"
  should have security group "ssh"
  should belong to vpc "web-vpc"
  should belong to subnet "subnet-xxxxxxxx"
  should have eip "xx.xx.xx.xxx"
  should have ebs "vol-xxxxxxx"
  instance_id
    should eq "i-xxxxxxxx"
  image_id
    should eq "ami-xxxxxxxx"
  private_dns_name
    should eq "ip-10-1-0-10.ap-northeast-1.compute.internal"
  public_dns_name
    should eq ""
  instance_type
    should eq "t2.micro"
  private_ip_address
    should eq "10.1.0.10"
  public_ip_address
    should eq "xx.xx.xx.xxx"

Finished in 0.17717 seconds (files took 1.55 seconds to load)
16 examples, 0 failures

エラーとか

  • subnetでFailuresになる generateで作成した場合はbelong_to_subnetにSubnet名が入力されているはず、 subnetIDに変更してみたら動いた。 自分の時はpublic-aというsubnet名で指定していたがsubentIDにしないと動かなかった

思ったこと

今思っただけなので明日、来週、来月には考え変わってるかもというメモ
ベストプラクティスがあればいいけど設計思想や状況によるじゃないかと思って書いておく

  • 一度作った環境からgenerateするべきかspec書いてから構築するべきか
  1. terraform等で構築後にgenerate -> そのterraformで間違ってたらダメじゃん
  2. spec書いてから構築 -> 恐らくこれが正解なんだろうけど、手間だなと思ったり。
  • 既存の環境化でどうやってspecファイルを作るか

  • そもそも作る段階でミスってたら気が付かないんじゃね

おわり

awspecもServerspecも便利なんで可能な限りcode化するようにしよう

3
2
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
3
2