LoginSignup
23
11

More than 5 years have passed since last update.

AWS LambdaのPythonからLinuxコマンド実行

Last updated at Posted at 2018-05-11

下記の記事を見て、Pythonではどうやるんだろうと思い調べてやってみました。
https://qiita.com/Keisuke69/items/9951a93fd711360a61c5

やったこと

subprocessというモジュールを使用して実行してみました。
AWSLambdaでもPython標準ライブラリなのでアップロードなしで使えます。

ドキュメントによるとrun()の使用が推奨されているのでrun()を使用します。

サブプロセスを起動する推奨手段は、すべての用法を扱える run() 関数を使用することです。

実際にLambdaで試してみたのが以下のコードです。
subprocess.run()に引数としてidコマンドを渡して実行しています。

サンプルコード1
import subprocess
def lambda_handler(event, context):
    cmd = ['id']
    out = subprocess.run(cmd, stdout=subprocess.PIPE)
    print(out.stdout.decode())
実行結果1
START RequestId: 2507e6d7-5433-11e8-b8ee-47552c7b351e Version: $LATEST
uid=477(sbx_user1070) gid=476 groups=476

END RequestId: 2507e6d7-5433-11e8-b8ee-47552c7b351e
REPORT RequestId: 2507e6d7-5433-11e8-b8ee-47552c7b351e    Duration: 54.27 ms    Billed Duration: 100 ms     Memory Size: 128 MB    Max Memory Used: 22 MB    

引数のstdout=subprocess.PIPEは指定しないと出力が取得できないので指定しています。

デフォルトでは、標準出力や標準エラー出力を捕捉しません。捕捉したい場合は stdout または/および stderr 引数に PIPE を渡してください。

変数に値を入れて、引数として渡すこともできます。

サンプルコード2
import subprocess
word = 'hoge'
cmd = ['echo', word]
out = subprocess.run(cmd, stdout=subprocess.PIPE)
print(out.stdout.decode())
実行結果2
START RequestId: 900a0b5e-5434-11e8-a0c3-1985fa1d212b Version: $LATEST
hoge

END RequestId: 900a0b5e-5434-11e8-a0c3-1985fa1d212b
REPORT RequestId: 900a0b5e-5434-11e8-a0c3-1985fa1d212b    Duration: 33.69 ms    Billed Duration: 100 ms     Memory Size: 128 MB    Max Memory Used: 22 MB

オプションや複数の引数もコマンドへ渡すことができます。

サンプルコード3
import subprocess
cmd = ['ls', '-la', '/bin', '/usr/bin']
out = subprocess.run(cmd, stdout=subprocess.PIPE)
print(out.stdout.decode())
実行結果3
START RequestId: 8d33eec4-54d8-11e8-bbc2-7900f548500a Version: $LATEST
/bin:
total 5172
dr-xr-xr-x 2 root root 4096 Apr 12 15:19 .
drwxr-xr-x 21 root root 4096 May 11 04:19 ..
-rwxr-xr-x 1 root root 28448 Feb 25 2016 arch
lrwxrwxrwx 1 root root 4 Apr 12 15:18 awk -> gawk
-rwxr-xr-x 1 root root 28128 Feb 25 2016 basename
-rwxr-xr-x 1 root root 936000 Aug 30 2017 bash
-rwxr-xr-x 1 root root 48000 Feb 25 2016 cat

~~ () ~~

-rwxr-xr-x 1 root root 28448 Feb 25 2016 uname
-rwxr-xr-x 1 root root 2555 Feb 19 2011 unicode_start
-rwxr-xr-x 1 root root 363 Feb 19 2011 unicode_stop
-rwxr-xr-x 1 root root 25344 Feb 25 2016 unlink
-rwxr-xr-x 1 root root 6800 Oct 12 2016 usleep
lrwxrwxrwx 1 root root 8 Apr 12 15:18 ypdomainname -> hostname
-rwxr-xr-x 1 root root 1941 Feb 24 2016 zcat

/usr/bin:
total 39140
dr-xr-xr-x 2 root root 20480 Apr 12 15:20 .
drwxr-xr-x 13 root root 4096 Apr 12 15:18 ..
-rwxr-xr-x 1 root root 36184 Feb 25 2016 [
-rwxr-xr-x 1 root root 105408 Aug 16 2016 a2p
-rwxr-xr-x 1 root root 25528 Dec 8 2015 addr2line

~~ () ~~

-rwxr-xr-x 2 root root 179496 Aug 17 2016 zipinfo
-rwxr-xr-x 1 root root 2041 Feb 24 2016 zless
-rwxr-xr-x 1 root root 2859 Feb 24 2016 zmore
-rwxr-xr-x 1 root root 5343 Feb 24 2016 znew
lrwxrwxrwx 1 root root 6 Apr 12 15:18 zsoelim -> soelim
END RequestId: 8d33eec4-54d8-11e8-bbc2-7900f548500a
REPORT RequestId: 8d33eec4-54d8-11e8-bbc2-7900f548500a  Duration: 142.80 ms Billed Duration: 200 ms Memory Size: 128 MB Max Memory Used: 23 MB

やってみたことは以上です。

参考

17.5. subprocess — サブプロセス管理
https://docs.python.jp/3/library/subprocess.html

23
11
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
23
11