LoginSignup
9
7

More than 3 years have passed since last update.

AWS Glueで自作関数のインポート

Last updated at Posted at 2020-10-13

自作関数インポートでつまづく

AWS Glueで共通処理や複雑な関数を別ファイルに記述する際は単純なインポートができません。
といってもジョブの作成時にファイルのパスを追加するだけなのですが、ドキュメントの該当部分がなかなか見つからず苦戦したのでメモしておきます

インポート方法

基本的には
AWS Glue での Python ライブラリの使用
のドキュメントを参考にすれば対応可能です。

pythonファイルをzip化、S3にあげてスクリプトのパス設定が基本の流れとなっています。

今回は以下の簡単な2つの関数をインポートします。1関数1ファイルとなっています。

hello_world.py
def hello_world(name):
    return 'Hello World, ' + str(name)
calcu.py
def sum(x, y):
    return x + y

この2つのファイルをzipでまとめて今回は適当にlib.zipというzipファイルを作成しました。
このzipファイルをs3にあげます。
s3://example_backet/lib.zipというパスに配置したとします。
このパスをジョブの作成時または編集でPython ライブラリパスという選択肢に記載します。
仮に複数のzipファイルが存在する場合はカンマ区切りで記入します。

image.png

これでジョブ作成後はいつものようにimportが可能となります。

sample.py
from hello_world import hello_world
from calcu import sum

hoge = hello_world('hoge') 
sum = sum(1, 2)
9
7
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
9
7