1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Proxy環境下でAWS SDK for Python(boto)を使う

Posted at

AWSをいじるために、botoライブラリを使ってPythonスクリプトを作っていた時に、Proxy環境下で嵌ったのでメモしておく。
なお、ベストプラクティスにはならない、バッドノウハウに近いのでご注意ください。

#環境
・Linux(RHEL6.5)
・Python3.3.6
・AWS SDK for Python(boto2.34.0)
・HTTP Proxy環境下

#事象
Proxy経由だと、APIをCallする際に以下の様なエラーが出る。

# python3
>>> import boto.ec2
>>> conn = boto.ec2.connect_to_region(‘ap-northeast-1’)
>>> conn.get_only_instances(instance_ids=[‘i-0de8dc14’])
Traceback (most recent call last):
  File “<stdin>”, line 1, in <module>
  File “/opt/python3.3.6/lib/python3.3/site-packages/boto/ec2/connection.py”, line 623, in get_only_instances
    next_token=next_token)
  File “/opt/python3.3.6/lib/python3.3/site-packages/boto/ec2/connection.py”, line 682, in get_all_reservations
    [(‘item’, Reservation)], verb=‘POST’)
  File “/opt/python3.3.6/lib/python3.3/site-packages/boto/connection.py”, line 1166, in get_list
    response = self.make_request(action, params, path, verb)
  File “/opt/python3.3.6/lib/python3.3/site-packages/boto/connection.py”, line 1112, in make_request
    return self._mexe(http_request)
  File “/opt/python3.3.6/lib/python3.3/site-packages/boto/connection.py”, line 913, in _mexe
    self.is_secure)
  File “/opt/python3.3.6/lib/python3.3/site-packages/boto/connection.py”, line 705, in get_http_connection
    return self.new_http_connection(host, port, is_secure)
  File “/opt/python3.3.6/lib/python3.3/site-packages/boto/connection.py”, line 747, in new_http_connection
    connection = self.proxy_ssl(host, is_secure and 443 or 80)
  File “/opt/python3.3.6/lib/python3.3/site-packages/boto/connection.py”, line 796, in proxy_ssl
    sock.sendall(“CONNECT %s HTTP/1.0\r\n” % host)
TypeError: ‘str’ does not support the buffer interface

#暫定対処
SSL通信が上手く通過出来てなかったように見えるので、
connectionを張る際に、is_secure=Falseを付けることで接続できた。
ただし、その設定値名の通り非SSL通信となりそうなので、利用シーンは気をつけたほうが良いかも。

>>> conn = boto.ec2.connect_to_region(‘ap-northeast-1’,is_secure=False)
>>> conn.get_only_instances(instance_ids=[‘i-0de8dc14’])
>>> [Instance:i-0de8dc14]

#botoのconfig
botoライブラリには実行時にいくつかの設定を読みこませられるそうです。

  • /etc/boto.cfg - for site-wide settings that all users on this machine > will use
  • ~/.boto - for user-specific settings
  • ~/.aws/credentials - for credentials shared between SDKs

Boto Config — boto v2.35.1

Proxyに関しては、以下のように記述すれば良さそう。

~/.boto
[Boto]
proxy = myproxy.com
proxy_port = 8080

ですが、私の環境ではこれがなぜか上手く読み込めてなさそうだったので、暫定対処としてハードコーディングに至りました。
この辺り、ノウハウある方はぜひコメント頂ければと思います。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?