0
0

Azure FunctionsでPrismaを使おうとしたらcli からのデプロイでドハマりしたメモ

Last updated at Posted at 2024-07-24

概要

前回の続き。Azure Functionsはfunc startで動いてもfunc azure functionapp publish hogeで何も言わずに死んでいくの本当によくないと思う。
設定ミスの箇所を探すのに苦労したのでメモ。

結論からいうとDeploy to Azure Functionsに従ってbinaryTargets = ["native", "debian-openssl-1.1.x"]をschema.prismaのgenerator clientブロックに追加してあげればよかった。

ソースコード

設定ミスをしたときに出る表示

  • package.jsonに必要なライブラリが足りていない ( honoのswagger がinstallされていない状態でもテスト実行で動いてしまったのが罠。)
  • package.jsonのmainにエントリポイントとなるファイルが指定されていない
  • typescriptのエイリアスが正しく認識されていない

などだと下のように静かに死んでいく。

Uploading package...
Uploading 39.01 MB [##############################################################################]
Upload completed successfully.
Deployment completed successfully.
[2024-07-24T23:25:48.451Z] Syncing triggers...
Functions in hoge:

成功するときは最後の行が下記のような表示になる。失敗していてもファイルのアップロードはするのでとても分かりにくい。なんか言って。

Functions in hoge:
    testTrigger - [httpTrigger]
        Invoke url: https://hoge.azurewebsites.net/test

Prismaの環境設定ミスによるエラーのメモ

下記のようにError calling sync triggers (BadRequest)のエラーが起こる。

Uploading 39.01 MB [##############################################################################]
Upload completed successfully.
Deployment completed successfully.
[2024-07-24T22:38:17.072Z] Syncing triggers...
[2024-07-24T22:38:25.363Z] Syncing triggers...
[2024-07-24T22:38:27.736Z] Syncing triggers...
[2024-07-24T22:38:30.124Z] Syncing triggers...
[2024-07-24T22:38:32.611Z] Syncing triggers...
[2024-07-24T22:38:34.986Z] Syncing triggers...
Error calling sync triggers (BadRequest). Request ID = 'a000abcd-0a0a-000b-a00b-00c0df0fff00'.

Deploy to Azure Functionsをちゃんと読んでなかった私が悪いのだけど、こうなったときにprisma generateに問題があるって気づかないよ。下記のようにbinaryTargets を設定すると解決する。

apps/api/schema.prisma
(省略)
generator client {
  provider = "prisma-client-js"
  output   = "./generated/client"
+  binaryTargets = ["native", "debian-openssl-1.1.x"]
}
(省略)

outputのディレクトリを変更しているのは、turborepoの都合でフォルダのコピーをしてからデプロイしているためで今回の本題とは関係ない。(前回参照)

ソースコード

apps/api/src/shared/prisma.ts
import { PrismaClient } from '../../generated/client';
export const prisma = new PrismaClient();
apps/api/src/functions/testTrigger.ts
import { app } from '@azure/functions';
import { prisma } from '@api/shared/prisma';

app.http('testTrigger', {
  methods: ['GET'],
  authLevel: 'anonymous',
  route: 'test',
  handler: async (_r, _c) => ({
    body: JSON.stringify(await prisma.character.findMany()),
  }),
});
apps/api/schema.prisma
generator zod {
  provider = "zod-prisma-types"
}

generator client {
  provider = "prisma-client-js"
  output   = "./generated/client"
  binaryTargets = ["native", "debian-openssl-1.1.x"]
}

datasource db {
  provider = "sqlserver"
  url      = env("DATABASE_URL")
}

model Character {
  CharacterID   String @id(map: "PK__Characte__757BCA40D91FB5C1") @db.NVarChar(128)
  CharacterName String @db.NVarChar(128)
}

local.settings.json
{
  "IsEncrypted": false,
  "Values": {
    "TZ": "UTC",
    "AzureWebJobsStorage":"",
    "FUNCTIONS_WORKER_RUNTIME": "node",
    "AzureWebJobsFeatureFlags": "EnableWorkerIndexing",
    "DATABASE_URL":"sqlserver://hoge.database.windows.net:1433;database=hogehoge;user=piyo;password=piyopiyo;encrypt=true;schema=pdo"
  },
  "Host": {
    "CORS": "*"
  }
}

デプロイ・確認

デプロイ先のFunctionsの環境変数にもDATABASE_URLは設定しておく。

デプロイ後、testにアクセスしてデータが取得できることを確認した。
https://hoge.azurewebsites.net/test

参考

Deploy to Azure Functions

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