ELBをコード管理するKelbimを使ってみた

2017.04.17

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

Kelbimとは?

Kelbimとは、AWSアカウントのELB情報をコード管理出来るツールです。 作業前バックアップやヘルスチェック確認を行うを行いたいAWSアカウントでは有効なツールと思います。

GitHub

kelbim

使い方

Kelbimをインストール

% git clone https://github.com/winebarrel/kelbim.git

% gem build kelbim.gemspec

% gem install kelbim-0.3.1.beta.gem

% gem list kelbim

*** LOCAL GEMS ***

kelbim (0.3.1.beta)

※本記事執筆時

kelbimオプション

% kelbim
Usage: kelbim [options]
-p, --profile PROFILE_NAME
--credentials-path PATH
-k, --access-key ACCESS_KEY
-s, --secret-key SECRET_KEY
-r, --region REGION
-a, --apply
-f, --file FILE
-n, --elb-name REGEXP
--exclude-elb-name REGEXP
--dry-run
--ec2s VPC_IDS
--without-deleting-policy
-e, --export
-o, --output FILE
--split
--split-more
-t, --test
--show-load-balancers
--show-policies
--no-color
--debug

ELB情報のエクスポート

ELB情報のエクスポートには3種類あり、管理し易い形でアウトプットファイルが作成されます。

ELB情報をエクスポート(東京リージョン)

ELB情報を一つのDSLファイルでエクスポートします。

% kelbim -r ap-northeast-1 -e -o <エクスポートファイル>
# Export ELB
% tree
.
└── kelbim-export
% kelbim -e -r ap-northeast-1 -o kelbim_test.elb
Export ELB to `kelbim-export`

% cat kelbim_test.elb
ec2 "vpc-xxxxxx" do
load_balancer "Test-ELB" do
spec do
# DNS Name: Test-ELB-xxxxxxxxxx.ap-northeast-1.elb.amazonaws.com
pending('This is an example')
url = URI.parse('http://www.example.com/')
res = Net::HTTP.start(url.host, url.port) {|http| http.get(url.path) }
expect(res).to be_a(Net::HTTPOK)
end

instances(
"Test-ap-northeast-1a"
)

listeners do
listener [:http, 80] => [:http, 80]
end

health_check do
target "HTTP:80/index.html"
timeout 5
interval 30
healthy_threshold 10
unhealthy_threshold 2
end

attributes do
additional_attributes []
connection_settings :idle_timeout=>60
cross_zone_load_balancing :enabled=>true
access_log :enabled=>false
connection_draining :timeout=>300, :enabled=>true
end

subnets(
"subnet-xxxxxxx"
)

security_groups(
"HTTP_Port_Allow"
)
end
end

各ELB情報をエクスポート

ELB単位でELB情報を取得します。

% kelbim -r ap-northeast-1 -e -o kelbim-export-split --split
Export ELB
write `./vpc-xxxxxxxxx.elb`
write `kelbim-export-split`
% tree
.
├── kelbim-export-split
└── vpc-xxxxxxxxx.elb
% cat kelbim-export-split
require 'vpc-xxxxxxxxx.elb'

% cat vpc-xxxxxxxxx.elb
ec2 "vpc-xxxxxxxxx" do
load_balancer "Test-ELB" do
spec do
# DNS Name: Test-ELB-xxxxxxxxx.ap-northeast-1.elb.amazonaws.com
pending('This is an example')
url = URI.parse('http://www.example.com/')
res = Net::HTTP.start(url.host, url.port) {|http| http.get(url.path) }
expect(res).to be_a(Net::HTTPOK)
end

instances(
"Test-ap-northeast-1a"
)

listeners do
listener [:http, 80] => [:http, 80]
end

health_check do
target "HTTP:80/index.html"
timeout 5
interval 30
healthy_threshold 10
unhealthy_threshold 2
end

attributes do
additional_attributes []
connection_settings :idle_timeout=>60
cross_zone_load_balancing :enabled=>true
access_log :enabled=>false
connection_draining :timeout=>300, :enabled=>true
end

subnets(
"subnet-xxxxxxxxx"
)

security_groups(
"HTTP_Port_Allow"
)
end
end

詳細な各ELB要素で情報をエクスポート

vpc毎に各ELBを項目レベルで情報を取得します。

% kelbim -r ap-northeast-1 -e -o kelbim-export --split-more
Export ELB
write `./vpc-xxxxxxxxx/Test-ELB.elb`
write `kelbim-export-split-more`
.
├── kelbim-export-split-more
├── vpc-xxxxxxxxx
│   └── Test-ELB.elb
% cat kelbim-export-split-more
require 'vpc-xxxxxxxxx/Test-ELB.elb'

% cat vpc-xxxxxxxxx/Test-ELB.elb
ec2 "vpc-xxxxxxxxx" do
load_balancer "Test-ELB" do
spec do
# DNS Name: Test-ELB-xxxxxxxxx.ap-northeast-1.elb.amazonaws.com
pending('This is an example')
url = URI.parse('http://www.example.com/')
res = Net::HTTP.start(url.host, url.port) {|http| http.get(url.path) }
expect(res).to be_a(Net::HTTPOK)
end

instances(
"Test-ap-northeast-1a"
)

listeners do
listener [:http, 80] => [:http, 80]
end

health_check do
target "HTTP:80/index.html"
timeout 5
interval 30
healthy_threshold 10
unhealthy_threshold 2
end

attributes do
additional_attributes []
connection_settings :idle_timeout=>60
cross_zone_load_balancing :enabled=>true
access_log :enabled=>false
connection_draining :timeout=>300, :enabled=>true
end

subnets(
"subnet-xxxxxxxxx"
)

security_groups(
"HTTP_Port_Allow"
)
end
end

ELB変更作業

ELBの変更作業をDSLから実施してみます。 今回使用するDSLファイルは以下のファイルを使用します。

% cat kelbim-export
ec2 "vpc-xxxxxxxxx" do
load_balancer "Test-ELB" do
spec do
# DNS Name: Test-ELB-xxxxxxxxx.ap-northeast-1.elb.amazonaws.com
pending('This is an example')
url = URI.parse('http://www.example.com/')
res = Net::HTTP.start(url.host, url.port) {|http| http.get(url.path) }
expect(res).to be_a(Net::HTTPOK)
end

instances(
"Test-ap-northeast-1a"
)

listeners do
listener [:http, 80] => [:http, 80]
end

health_check do
target "HTTP:80/index.html"
timeout 5
interval 30
healthy_threshold 10
unhealthy_threshold 2
end

attributes do
additional_attributes []
connection_settings :idle_timeout=>60
cross_zone_load_balancing :enabled=>true
access_log :enabled=>false
connection_draining :timeout=>300, :enabled=>true
end

subnets(
"subnet-xxxxxxxxx"
)

security_groups(
"HTTP_Port_Allow"
)
end
end

DSLファイルをテスト実行する(dry-run)

% kelbim --dry-run -a -f <DSLファイル>

DSLファイルを適用する

% keibim -a -f <DSLファイル>

最後に

使い方を記載していきましたが、多くのIAMユーザーやロールが作成されている環境で 確かにアタッチ済みポリシーの確認などの情報整理には大きく貢献してくれるツールと思いました。 複数のELBが動作している環境でEC2等のメンテナンスを実施する際、作業前にDSLファイルを取得⇛作業後にDSLファイルを取得⇛diffコマンドで比較を実施することで作業漏れが少なくなると思いますので、今後利用して行きたいと思います。