2.6. Microsoft Azure での関数の作成
Azure で関数を作成すると、データがフィルター処理され、Red Hat との共有用に作成したストレージアカウントに追加されます。このセクションのサンプル Python スクリプトを使用して、エクスポートからフィルタリングされたコストデータを収集して共有できます。
前提条件
- デバイスに Visual Studio Code がインストールされている必要がある。
- Visual Studio Code に Microsoft Azure Functions エクステンションがインストールされている必要がある。Azure 関数を作成する場合、Microsoft は、Microsoft Visual Studio Code IDE を使用してコードを開発およびデプロイすることを推奨しています。Visual Studio Code の設定に関する詳細は、Quickstart: Create a function in Azure with Python using Visual Studio Code を参照してください。
-
検索バーに
functionsと入力し、Function App を選択します。 - をクリックします。
- 関数のホスティングオプションを選択し、 をクリックします。
Create Function App ページで、リソースグループを追加します。
- Instance Details セクションで、関数アプリケーションに名前を付けます。
- Runtime stack で Python を選択します。
- Version で latest を選択します。
Review + create をクリックします。
- をクリックします。
- リソースが作成されるまで待ってから、 をクリックして表示します。
Visual Studio Code の場合:
Microsoft Azure タブをクリックし、Azure にサインインします。
- Workspaces ドロップダウンで、オレンジ色の稲妻のアイコンとして表示される をクリックします。
- をクリックします。
- 指示に従ってローカルの場所を設定し、関数の言語とバージョンを選択します。この例では、Python、Model 2、および利用可能な最新バージョンを選択します。
- 関数の Select a template ダイアログで、Timer trigger を選択し、関数に名前を付けて、Enter キーを押します。
関数の実行のタイミングを制御する cron 式を設定します。この例では、
0 9 * * *を使用して、関数を毎日 9 AM で実行します。- をクリックします。
- をクリックします。
requirements.txt ファイルの場合:
- 開発環境で関数を作成したら、requirements.txt ファイルを開き、以下の要件を追加してファイルを保存します。
azure-functions pandas requests azure-identity azure-storage-blob
azure-functions
pandas
requests
azure-identity
azure-storage-blob
function_app.py の場合:
- Python スクリプト をコピーして `function_app.py` に貼り付けます。
# Required vars to updateとマークされたセクションの値を変更して、環境に対応する値に更新します。以前に設定した cron タイマーがスクリプト内の@app.timer_triggerの値と一致していることを確認します。-
サンプルスクリプトでは、Azure Key Vault のシークレットを使用して、サービスアカウントの
client_idとclient_secretを環境変数として設定します。認証情報をスクリプトに直接入力することもできますが、これはベストプラクティスではありません。 デフォルトのスクリプトには、データのフィルタリングや RHEL サブスクリプションのフィルタリングを行うためのオプションが組み込まれています。使用するフィルタリングのタイプのコメントを解除するか、独自のカスタムフィルタリングを記述する必要があります。次のどちらか一方からコメントを削除してください。
-
filtered_data = hcs_filtering(df) -
filtered_data = rhel_filtering(df)
-
よりカスタマイズされたフィルタリングを記述する場合は、次の必須列を含める必要があります。
'additionalinfo', 'billingaccountid', 'billingaccountname', 'billingcurrencycode', 'billingperiodenddate', 'billingperiodstartdate', 'chargetype', 'consumedservice', 'costinbillingcurrency', 'date', 'effectiveprice', 'metercategory', 'meterid', 'metername', 'meterregion', 'metersubcategory', 'offerid', 'productname', 'publishername', 'publishertype', 'quantity', 'reservationid', 'reservationname', 'resourcegroup', 'resourceid', 'resourcelocation', 'resourcename', 'servicefamily', 'serviceinfo1', 'serviceinfo2', 'subscriptionid', 'tags', 'unitofmeasure', 'unitprice'
'additionalinfo', 'billingaccountid', 'billingaccountname', 'billingcurrencycode', 'billingperiodenddate', 'billingperiodstartdate', 'chargetype', 'consumedservice', 'costinbillingcurrency', 'date', 'effectiveprice', 'metercategory', 'meterid', 'metername', 'meterregion', 'metersubcategory', 'offerid', 'productname', 'publishername', 'publishertype', 'quantity', 'reservationid', 'reservationname', 'resourcegroup', 'resourceid', 'resourcelocation', 'resourcename', 'servicefamily', 'serviceinfo1', 'serviceinfo2', 'subscriptionid', 'tags', 'unitofmeasure', 'unitprice'Copy to Clipboard Copied! Toggle word wrap Toggle overflow 一部の列はレポートの種類によって異なります。サンプルスクリプトはこれらの列を正規化し、フィルター処理されたすべてのレポートはこの例に従う必要があります。
column_translation = {"billingcurrency": "billingcurrencycode", "currency": "billingcurrencycode", "instanceid": "resourceid", "instancename": "resourceid", "pretaxcost": "costinbillingcurrency", "product": "productname", "resourcegroupname": "resourcegroup", "subscriptionguid": "subscriptionid", "servicename": "metercategory", "usage_quantity": "quantity"}column_translation = {"billingcurrency": "billingcurrencycode", "currency": "billingcurrencycode", "instanceid": "resourceid", "instancename": "resourceid", "pretaxcost": "costinbillingcurrency", "product": "productname", "resourcegroupname": "resourcegroup", "subscriptionguid": "subscriptionid", "servicename": "metercategory", "usage_quantity": "quantity"}Copy to Clipboard Copied! Toggle word wrap Toggle overflow データをフィルタリングするには、データフレームのフィルタリングを追加する必要があります。以下に例を示します。
-
完全一致: df.loc[(df["publishertype"] == "Marketplace")]
publisherTypeは、Marketplace 以外のすべてのデータを除外します。 -
Contains: df.loc[df["publishername"].astype (str).str.contains ("Red Hat")] は、
publisherNameに Red Hat が含まれていないすべてのデータをフィルタリングします。 - df.loc 句で & (AND の場合) と | (OR の場合) を使用してフィルターをスタックできます。
さらに便利なフィルター:
subscriptionid- 特定のサブスクリプションをフィルタリングします。
resourcegroup- 特定のリソースグループをフィルタリングします。
resourcelocation- 特定のリージョンでデータをフィルタリングします。
-
特定のサービスタイプをフィルタリングするには、
servicename、servicetier、metercategory、およびmetersubcategoryを使用できます。
-
完全一致: df.loc[(df["publishertype"] == "Marketplace")]
-
サンプルスクリプトでは、Azure Key Vault のシークレットを使用して、サービスアカウントの
-
カスタムクエリーを作成したら、
# custom filtering basic example #の下にあるサンプルスクリプトでカスタムクエリーを更新します。 - ファイルを保存します。
Visual Studio Code の場合:
- Function ウィンドウを右クリックし、 をクリックします。
- 前の手順で作成した関数アプリケーションを選択します。