AWS SDK for Python(Boto)でのAWSアカウント管理について

こんにちは!beardです!

AWS SDK for Python(Boto)では、複数のAWSアカウントを使用するために便利な方法があります。

今回は、pythonの機能であるインタラクティブシェルを利用してその使用例をご紹介します。

AmazonLinuxでのBotoのインストールについては過去の記事『 EC2編~Amazon Linux Python Update 2.7~ 』をご覧ください。

  1. botoで操作したいAWSアカウントのアクセスキーとシークレットアクセスキーを設定する。

botoでは、/etc/boto.cfgファイルにAWSアカウントのアクセスキーとシークレットアクセスキーを記述することで読み込んでくれます。

フォーマットは以下のようになっています。

1[root@ip-172-31-29-196 ~]# cat /etc/boto.cfg2[profile accepted-user]3aws_access_key_id = A**************A4aws_secret_access_key = K********************A56[profile denied-user]7aws_access_key_id = A**************Q8aws_secret_access_key = V********************i
  1. 今回の例ではaccepted-userにはIAMでAWSのすべての権限を与えています。

逆に、denied-userにはすべての権限を拒否しています。

これらのプロファイル(=AWSユーザー)をbotoのインタラクティブシェルで操作をしてみます。

1python2>>> import boto.ec234>>> ec2conn = boto.ec2.connect_to_region('ap-northeast-1',profile_name='accepted-user') ←ここで/etc/boto.cfgに設定したプロファイル名を指定することで、このユーザーとしてAWSへ接続をします。5>>> ec2conn.get_all_instances()67[Reservation:r-e9eb28f0, Reservation:r-db2ad4c2, Reservation:r-eef009f7, Reservation:r-27f3363e, Reservation:r-0ca95115, Reservation:r-5b854542, Reservation:r-b820dba1] ←accepted-userですのでインスタンスの一覧が表示されます。(Reservationとなっていますがリザーブドインスタンスではありません。)
  1. 次はプロファイルを「denied-user」に変えて別のユーザーとして接続してみましょう。

上記のインタラクティブシェルの続けて実行します。

1>>> ec2conn = boto.ec2.connect_to_region('ap-northeast-1',profile_name='denied-user') ←denied-userとして接続します。2>>> ec2conn.get_all_instances()3Traceback (most recent call last):4  File "<stdin>", line 1, in <module>5  File "/usr/lib/python2.7/site-packages/boto/ec2/connection.py", line 587, in get_all_instances6    max_results=max_results)7  File "/usr/lib/python2.7/site-packages/boto/ec2/connection.py", line 683, in get_all_reservations8    [('item', Reservation)], verb='POST')9  File "/usr/lib/python2.7/site-packages/boto/connection.py", line 1166, in get_list10    raise self.ResponseError(response.status, response.reason, body)11boto.exception.EC2ResponseError: EC2ResponseError: 403 Forbidden12<?xml version="1.0" encoding="UTF-8"?>1314<Response><Errors><Error><Code>UnauthorizedOperation</Code><Message>You are not authorized to perform this operation.</Message></Error></Errors><RequestID>8976a680-fc7c-41dc-beb1-420403f32db9</RequestID></Response>

▼ 簡単な解説 ▼

botoでは、/etc/.cfgにユーザーを記述することでいちいちアクセスキーとシークレットアクセスキーを入力しなくても、プロファイル名を指定するだけでユーザーを切り替えてAWSに接続することができます。

今回の例ではdenied-userには許可を与えていませんので、インスタンスの一覧を取得しようとしても、「You are not authorized to perform this operation.」と拒否されています。

1require 'spec_helper'23describe command('date') do ←dateコマンド4  it { should return_stdout /JST/ } ←結果にJSTが含まれている5end67describe command("fdisk -l | grep filesys | awk '{print $4}'") do ←ディスク容量を表示させ8    it { should return_stdout /100G/ } ←100Gとなっている9end1011describe 'PHP config' do12  context  php_config('date.timezone') do ←PHPコンフィグのタイムゾーンが13   its(:value) { should eq 'Asia/Tokyo' } ←Asia/Tokyoになっている14end1516context php_config('memory_limit') do ←PHPコンフィグのメモリ制限が17      its(:value) { should eq '512M' } ←512MBになっている18end19end2021describe command('php -v |grep PHP') do ←PHPのバージョンを表示22it { should return_stdout /5.3.*/ } ←バージョンが5.3.*になっている23end
  1. /local/i-incetanceID/以下で「rake spec」コマンドでserverspecを実行します。
1$ rake spec2(in /local/i-instanceID)3/usr/bin/ruby2.0 -S rspec spec/localhost/test_spec.rb4....56Finished in 0.06612 seconds75 examples, 0 failures ←5項目をテストして間違いが0個。

いかがでしたか?

Serverspecは通常のLinuxコマンドと組み合わせることで様々な項目をテストすることもできます。

次回もお楽しみに!!

この記事を書いた人

aws-recipe-user