Serverlessを使うと簡単なので入れる
npm install -g serverless
Serverlessのプロジェクトを作る
sls create \
--template aws-python3 \
--name hello \
--path hello
(pathはどちらでもいいです。)
kubectlをダウンロード
ここよりLinux版をcurlでダウンロード
https://kubernetes.io/docs/tasks/tools/install-kubectl/
curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl
kubernetesのconfigファイルを連れてくる
cp ~/.kube/config .
フォルダ構成
- handler.py
- config
- kubectl
handler.pyを編集
def hello(event, context):
config_filepath = '/tmp/config'
with open(config_filepath, 'w') as f:
f.write(config)
shutil.copy('kubectl', '/tmp/kubectl')
proc = subprocess.run(
['chmod', '+x', '/tmp/kubectl'],
stdout = subprocess.PIPE,
stderr = subprocess.PIPE
)
proc = subprocess.run(
['/tmp/kubectl', '--kubeconfig', config_filepath, 'get', 'po'],
stdout = subprocess.PIPE,
stderr = subprocess.PIPE
)
print(proc.stdout.decode('utf8'))
print(proc.stderr.decode('utf8'))
deploy
$ sls deploy
...
確認
$ sls invoke
...