LoginSignup
2
1

Azure Functions でPython関数を作成してデプロイする

Posted at

この記事の目的

Azure Functions を作成し、Azure Functions Core Toolsを利用してデプロイする手順を解説します。

👇これより先は下記内容を前提とします

Azure Functions Core Tools のインストール

Azure Functions Core Toolsをインストールします。
(参考)
Azure Functions Core Tools のインストール

ローカル関数の作成

func initコマンドを実行して、LocalFunctionProj というフォルダーに関数プロジェクトを作成します。

func init LocalFunctionSampleProj --python -m V2

プロジェクトフォルダーに移動します。

cd LocalFunctionSampleProj

関数を作成します。

func new

下記のようにtemplateの選択プロンプトが表示されるので、ここではTimer Triggerを選択します。

Use the up/down arrow keys to select a template:
Blob trigger
CosmosDB trigger
EventGrid trigger
EventHub trigger
HTTP trigger
Queue trigger
ServiceBus Queue trigger
ServiceBus Topic trigger
Timer Trigger

関数名を入力します。ここではtimer_trigger_func01と入力します。

Use the up/down arrow keys 
to select a template:Function Name: [timer_trigger] timer_trigger_func01

CRON式を入力します。ここではデフォルトのままにしますので、何も入力せずにEnterを入力します。

Schedule: [0 */5 * * * *]

関数が作成されます。
最終的に下記内容が出力されます。

Use the up/down arrow keys 
to select a template:Function Name: [timer_trigger] timer_trigger_func01
Schedule: [0 */5 * * * *]
Appending to C:\****\local_projects\LocalFunctionSampleProj\function_app.py
The function "timer_trigger_func01" was created successfully
 from the "Timer Trigger" template.

関数用の関連 Azure リソースを作成する

Azure ログインを実施し、サブスクリプションを設定します。

az login
az account set --subscription "*****-****-****-****-****"

リソースグループresource_group_for_pythonに、ストレージアカウントsasample09を作成します。

az storage account create^
 --name sasample09^
 --location japaneast^
 --resource-group resource_group_for_python^
 --sku Standard_LRS

リソースグループresource_group_for_pythonに、App Service プランAppServicePlan01を作成します。

az functionapp plan create^
 --resource-group resource_group_for_python^
 --name AppServicePlan01^
 --sku B1^
 --is-linux true

リソースグループresource_group_for_pythonに、関数アプリappsample09を作成します。
Pythonバージョンは本記事記載時点の最新バージョン3.11を指定します。

az functionapp create^
 --resource-group resource_group_for_python^
 --plan AppServicePlan01^
 --runtime python^
 --runtime-version 3.11^
 --functions-version 4^
 --name appsample09^
 --os-type linux^
 --storage-account sasample09

Azure に関数プロジェクトをデプロイする

プロジェクトフォルダーに移動します。

cd LocalFunctionSampleProj

Azure の関数アプリappsample09に関数プロジェクトをデプロイします。
※ 関数アプリ作成直後にデプロイすると失敗します。数分程待ってから再度デプロイしてみて下さい。

func azure functionapp publish appsample09

下記のような内容が出力されればデプロイ成功です。

Generating summary of Oryx build
Deployment Log file does not exist in /tmp/oryx-build.log
The logfile at /tmp/oryx-build.log is empty. Unable to fetch the summary of build
Triggering recycle (preview mode disabled).
Deployment successful. deployer = Push-Deployer deploymentPath = Functions App ZipDeploy. Extract zip. Remote build.
Remote build succeeded!

👇関連記事

👇参考URL

[keywords]
AzureFunctions Python Core Tools deploy

Azure Functions でPython関数を作成してデプロイする

更新日:2023/11/09

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