0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Azure Functions×Playwrightでバッチ自動化(HTTPトリガー対応)構築手順

Last updated at Posted at 2025-04-28

※本エントリはAIツールにて生成されています

Azure Functions(Linux Consumption)上でPlaywrightと@sparticuz/chromiumを使い、Webページを自動でPDF化するバッチ処理を作った記録です。当初はTimer Triggerによるスケジュール実行を考えていましたが、運用の柔軟さを重視してHTTPトリガー運用に切り替えました。生成したPDFはAzure Blob Storageへ自動アップロードされます。

技術スタック・開発環境

使った主な技術・ツールは次の通りです。

  • Node.js 20.x
  • TypeScript
  • Azure Functions Core Tools 4.x
  • Azure CLI 2.60+
  • Playwright-core、@sparticuz/chromium
  • @azure/functions
  • Git

プロジェクト初期化と依存パッケージ導入

まず、Azure FunctionsのTypeScriptプロジェクトを作成し、必要なパッケージを導入します。

# プロジェクト作成
func init AzureFunctionsPlaywrightBatch --worker-runtime node --language typescript

# Function作成(例: MonthlyJob)
func new --name MonthlyJob --template "HTTP trigger"

# 依存パッケージ追加
npm install playwright-core @sparticuz/chromium @azure/storage-blob
npm install -D typescript @azure/functions

Function実装とトリガー設定

MonthlyJob/index.tsでPlaywrightと@sparticuz/chromiumを使いWebページをPDF化し、生成したPDFをAzure Blob Storageへアップロードします。一時ファイルの保存先は/tmpディレクトリです。

index.ts(抜粋):

import chromium from "@sparticuz/chromium";
import { chromium as pw } from "playwright-core";
import { BlobServiceClient } from "@azure/storage-blob";
import fs from "fs";

export default async function (context, req) {
  const browser = await pw.launch({
    executablePath: await chromium.executablePath(),
    args: chromium.args,
    headless: true
  });
  const page = await browser.newPage();
  await page.goto("https://www.yahoo.co.jp", { waitUntil: "networkidle" });
  const pdfPath = `/tmp/report.pdf`;
  await page.pdf({ path: pdfPath, format: "A4" });
  await browser.close();

  // Blob Storageアップロード処理...
}

function.json(HTTPトリガー設定例):

{
  "bindings": [
    {
      "authLevel": "function",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [ "get", "post" ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "res"
    }
  ],
  "scriptFile": "../dist/MonthlyJob/index.js"
}

設定ファイルのポイント

  • local.settings.jsonでAzure Storageの接続文字列などを管理
  • tsconfig.jsonでTypeScriptのビルド設定
  • .funcignore.gitignoreも必要に応じて整備

ビルド・ローカルテスト

TypeScriptのビルドとローカルでのFunction実行は次の通りです。

# TypeScriptビルド
npm run build

# ローカルでFunction実行
func start

ローカル環境ではhttp://localhost:7071/api/MonthlyJobへcurl等でリクエストし、PDF生成やBlobアップロードの動作を確認できます。

Azureリソース準備とデプロイ

Azure上でのリソース作成とデプロイ手順は次の通りです。

# Azureリソース作成(リソースグループ、ストレージ、Function App等)
az group create ...
az storage account create ...
az functionapp create ...

# デプロイ
func azure functionapp publish <FunctionApp名>

デプロイ後はAzure PortalでFunctionのエンドポイントやfunction keyを取得し、curl等で本番環境のFunctionを実行して動作確認します。

注意点・トラブルシューティング

  • Playwright/Chromiumの実行には@sparticuz/chromiumが必須
  • 書き込み可能なディレクトリは/tmpのみ
  • Azure Functions(Linux Consumption)ではリソース制限や依存バイナリの配置に注意
  • 実行時エラーはAzure Portalの「モニター」や「ログ」で確認
  • 必要に応じてApp Serviceプランへの切り替えやバイナリ配置方法の見直しも検討
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?