LoginSignup
2
2

More than 3 years have passed since last update.

Serverless python requirementsのcachingを有効にしてビルド時間短縮

Last updated at Posted at 2019-04-23

Serverless Framework でrequirements.txtに記載したパッケージをインストールしLambdaに同梱してくれる便利プラグイン serverless-python-requirements 。便利だけど、ビルドのたびインストールが走ってしんどいなぁと思ってました。

が、キャッシュを利用できる設定がありました!

キャッシュ利用設定

READMEExtra Config Options > Caching から引用

custom:
  pythonRequirements:
    useDownloadCache: true
    useStaticCache: true

customセクションのpythonRequirementsで有効にするキャッシュタイプをtrueにすることでキャッシュを有効にできます。デフォルトはfalse(=キャッシュ使わない)設定になってます。

詳細はREADME参照
cacheLocationでキャッシュする場所も指定できるようです。

キャッシュ利用結果

2回目以降はキャッシュが使われて、比較するとかなり早くビルドが終わりました。

Serverless: Using static cache of requirements found at /root/.cache/serverless-python-requirements/...

指定バージョンを変更した場合

requirements.txt内の
dnspython==1.15.0

dnspython==1.16.0
に変更して、もう一度ビルド&デプロイしてみたところ

Serverless: Installing requirements from /root/.cache/serverless-python-requirements/...

という感じでインストールが走り直しました。デプロイされたパッケージのdnspythonバージョンも1.16.0になっていました。

備考

キャッシュ設定は去年(2018)の9月ぐらいから使えるようになったぽい
https://github.com/UnitedIncome/serverless-python-requirements/pull/165
https://github.com/UnitedIncome/serverless-python-requirements/commit/137f8e1b1e10579a2b8db88a49c335329a3307dd#diff-04c6e90faac2675aa89e2176d2eec7d8

2
2
1

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