LoginSignup
2
1

【Lambda】Supabaseに接続する

Last updated at Posted at 2024-05-30

まとめ

zip化したnodejsをレイヤーとして使う

1. Node.jsランタイムを使用する

当初はpythonを使用するつもりだったのですが、pythonでsupabaseライブラリを使うのが結構面倒だったので、nodeを使用しました

2. レイヤーとして使用するnodejsを用意 -> デプロイ

# nodejsディレクトリを作成
mkdir nodejs
cd nodejs

# supabaseライブラリをインストール
npm init
npm install supabase

# zip化
cd ../
zip -r hogehoge.zip nodejs

注意1: 作成するディレクトリ名は必ずnodejsという名前にすること

注意2: ファイルではなくnodejsディレクトリ自体をzip化すること

zipファイル自体の名前はなんでもOKです
作成したzipをlambdaのレイヤーとしてデプロイしましょう

レイヤー登録のわかりやすい手順はこちら

3. レイヤーを使用する

実際に使う場合は、以下のようになります

import { createClient } from '@supabase/supabase-js';
import { getParameter } from '/opt/getParams.mjs';


export const handler = async (event) => {
  const supaUrl = await getParameter('SUPABASE_URL');
  const supaKey = await getParameter('SUPABASE_KEY');

        
  const supabase = createClient(supaUrl, supaKey);

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