LoginSignup
0
0

More than 5 years have passed since last update.

Mac OSX El Capitanでvirtualenv経由でboto3を動かす

Posted at

AWSを操作するのにboto3+python3で作ってるが、いつも踏み台のLinuxServerから
操作していた。よく考えたら手元のMacbookでやればいいのに今頃気づいたのでメモ。
後で絶対忘れるから。
2系は2系で使いたいので、virtualenvを利用する。
'python3 ./hoge.py'とかやればPython3だけ入れれば良いのですが、
それもなぁ、という感じなので。

前提環境

Mac OSX El Capitan

手順

Homeblewインストール
https://brew.sh/index_ja.html

Python3インストール

$ brew install python3

virtualenvインストール
pipは標準で入ってるのでそのまま利用。

$ pip install virtualenv 
$ pip list

virtualenv環境作成
参考 (https://virtualenv.pypa.io/en/stable/userguide/)

$ mkdir work_python3 
$ virtualenv -p python work_python3
$ cd work_python3
$ source ./bin/activate

今回の作業ではpython3.6がインストールされた

必要なツールをインストール  

標準でpip,pip3,pip3.6が付属してるけど中身は全部一緒なのでどれでもよし。

$ pip install awscli
$ pip install boto3

動作確認
ひとまず最新のAmazonLinuxのAMIIDを引っ張ってくるテストコード

get_ami.py
#!/usr/bin/env python

import boto3
import re
from operator import itemgetter

pattern = re.compile('amzn-ami-hvm-*')

ec2_client = boto3.client('ec2')

response = ec2_client.describe_images(
    Owners = ['amazon']
)
pattern = re.compile('amzn-ami-hvm-.*-ebs')

image_id = sorted(
    [ item for item in response['Images']
      if item.get('Name', None) and pattern.search(item['Name']) ]
#, key=itemgetter(u'CreationDate'))
, key=itemgetter(u'CreationDate'))[-1]['ImageId']

print('AMI: ', image_id)
$ ./get_ami.py
$ AMI ami-b3d2abd4

あとはgitやらと連携してごりごり作業してきますか。

0
0
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
0
0