AWS as code & test
2014年4月11日
JAWSUG 東京勉強会
自己紹介
柳瀬任章(やなせ ひであき)/ @oko_chang
株式会社サーバーワークス
サービス開発チーム [NEW]
http://okochang.hatenablog.jp
退職しませんでした
インフラふりかえり
Infrastructure as Code
構成管理
Chef、Puppet、Ansible
テスト
Test Kitchen、serverspec
AWSの場合は?
構成管理
CloudFormation
AWS SDK
Command Line Interface
テスト
??????????
今日のはなし
AWS、捗りますよね?
ちょっとm3.large10台くらい起動して
キタコレまかせろ
CLI、捗ります
$ aws ec2 run-instances 
--image-id ami-a1bec3a0 
--security-group-ids sg-xxxxxxxx 
--instance-type m3.xlarge 
--subnet-id subnet-xxxxxxxx 
--key-name your-key 
--associate-public-ip-addres
Management Console、捗ります
m3.xlarge!
AWS、テストしたい
AWSのリソースをもっと安心して使いたい
AWS SDK使えばテストは書けそう
プログラマブルなんだからテストあって良い
spec_helper.rb
# -*- coding: utf-8 -*-
require 'aws-sdk'
config = YAML.load_file("config/aws.yml")
AWS.config(
access_key_id: config["access_key_id"],
secret_access_key: config["secret_access_key"],
region: config["region"]
)
ec2_spec.rb
# -*- coding: utf-8 -*-
require_relative "spec_helper"
describe "EC2" do
ec2 = AWS::EC2.new
describe "Instances" do
subject(:instances) do
ec2.instances.select { |i| i.status != :terminated }
end
it { should have(1).instances }
end
end
test-server_spec.rb
# -*- coding: utf-8 -*-
require_relative "./spec_helper"
describe "EC2" do
before :all do
@ec2 = AWS::EC2.new
@instance = @ec2.instances.find { |i| i.tags[:Name] == "test-server" && i.status !
= :terminated }
end
describe "instance" do
it { @instance.should_not be_nil }
end
describe "Instance Type" do
it { @instance.instance_type.should == "m3.large" }
end
end
テスト実行
$ rspec spec/ec2_spec.rb
..
Finished in 1.38 seconds
2 examples, 0 failures
試してみる
感想
出来ていない部分が分かりやすい
テストが簡単、目視つらい
リモートのメンバと作業しやすかった
色々なシーンで使えそう
これから
CIサーバとかで定期的にテスト
いろんなタイプのプロジェクトに使う
実はノウハウ持ってる人いそうなので共有を
サンプルコード
https://github.com/serverworks/aws-spec
ありがとうございました

AWS as code_and_test