LoginSignup
2
1

More than 3 years have passed since last update.

Azure Functions PowerShellで blobコンテナへの書き込み

Posted at

概要

気がつけばAzure FunctionsでPowershellがGAされていて、
Azure Functions の PowerShell 開発者向けガイドを読んでたら依存関係にAzライブラリの記載があったので、
Functions上でAzコマンドが使えるのか確認。

目次

  1. 今回やること
  2. 準備
  3. コード
  4. 結果
  5. 次にやること(予定)

今回やること

Azure Functions(PowerShell)をデプロイして、
FunctionsからAzure Storage AccountのBlobコンテナにファイルをアップロードする。

また、Azコマンドを使う際にUser/Passは使いたくないので、
Managed Identity(MSI?)でFunctionsに対して権限を付与する。

準備

  1. こんな感じに設定してAzure functionsをデプロイ
    image.png

    image.png

  2. ID機能?を有効にする
    image.png

  • statusをoff→onへ
    image.png

  • 確認がでるのでYesを
    image.png

  1. さっき作ったSubscriptionからFunctionsに権限を割り振る。
    今回はとりあえずOwnerに
    (Functionsを作り直したので名前が変わっています) image.png

コード

適当に書く。
確認をし易いようにHttpTrrigerで
適当に現在日時をBlobコンテナに書き込む

using namespace System.Net

# Input bindings are passed in via param block.
param($Request, $TriggerMetadata)

# Write to the Azure Functions log stream.
Write-Host "PowerShell HTTP trigger function processed a request."

# Interact with query parameters or the body of the request.

(get-date) > .\date.txt

Connect-AzAccount -Identity

(Get-AzSubscription)[0]| Select-AzSubscription 

$storageName = "hogehoge"
$storageAccount = Get-AzStorageAccount | ?{$_.StorageAccountName -eq $storageName}  
$storageAccount | Get-AzStorageContainer  | ?{$_.name -eq "logs"} |Set-AzStorageBlobContent -force -File .\date.txt


# Associate values to output bindings by calling 'Push-OutputBinding'.
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
    StatusCode = [HttpStatusCode]::OK
    Body = "end"
})

結果

書き込みができた
image.png

結構簡単に書けたのでローカルで動かしてるPowerShellをFunctionsに移植できそう。

次にやりたいこと

  • CustomRoleの作成
    FunctionsにOwner権限を割り当てているので必要最低限の権限だけつけたい
  • ARM templateの記述
    Storageとか停止できないサービスを使うときだけ使いたい。
  • Functions上でAzコマンド実行時のNW経路 調査方法の検討つかないけど、いつか確認したい。
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