0
0

bicepparamファイルを試してAzureFunctionsの環境変数を設定したメモ

Posted at

概要

bicepparam形式でパラメータファイルを設定したメモ。
ソースコード

ソースコード

infra/biceps/core/host/functions.bicep
+ param functionEnvironments array 

// 省略
resource functionApp 'Microsoft.Web/sites@2022-09-01' = {
  name: functionAppName
  location: location
  kind: kind
  properties: {
    reserved: true
    siteConfig: {
      cors: {
        allowedOrigins: [staticSites_my_first_static_web_app_name_resource.properties.defaultHostname]
      }
      linuxFxVersion: linuxFxVersion
      appSettings: [
      // 省略
        {
          name:'CONNECTION_STRING'
          value: connectionString
        }
+        ...functionEnvironments
      ]
    }
  }
}

Bicep のスプレッド演算子

minfra/biceps/main.bicep
param location string = resourceGroup().location
param keyVaultName string
@description('The runtime version of the Azure Functions app.')
param functionsRuntime object
+ param functionEnvironments array 
param staticSites_pl_static_web_app_name string

// 省略

module myFunctions 'core/host/functions.bicep' = {
  name: 'myFunctions'
  params: {
    location: location
    staticSites_pl_static_web_app_name: staticSites_pl_static_web_app_name
    databaseUrl: keyVault.getSecret('AsyncTrpgDatabaseURL')
    storageAccountName: storageAccountName
    kind: functionsRuntime.kind
    runtime: functionsRuntime.runtime
    linuxFxVersion: functionsRuntime.linuxFxVersion
    applicationInsightsInstrumentationKey: myFunctionsApplicationInsights.outputs.applicationInsightsInstrumentationKey
    extensionVersion: functionsRuntime.extensionVersion
    connectionString: keyVault.getSecret('AsyncTrpgConnectionString')
+    functionEnvironments: functionEnvironments
  }
}
infra/bicep/main.bicepparam
using 'main.bicep'

param functionsRuntime = {
  runtime: 'node'
  linuxFxVersion: 'Node|20'
  kind: 'functionapp,linux'
  extensionVersion: '~4'
}
param staticSites_pl_static_web_app_name = readEnvironmentVariable('PL_STATIC_WEB_APP_NAME','')
param keyVaultName = readEnvironmentVariable('KEY_VAULT_NAME','')
param functionEnvironments = [
  {
    name: 'HOGE'
    value: 'hoge'
  }
]

readEnvironmentVariableから環境変数を取得しようとしたがうまく行かなかった。「変数の読み込みは、実行時ではなくコンパイル中に発生します。」の一文が気になる。どの環境の環境変数を参照しているのか。。。

infra/bin/createFunctions.bash
#!/bin/bash

BIN_DIR=$(cd $(dirname $0) && pwd)
source $BIN_DIR/common.bash

cd $BICEP_DIR && az deployment group create \
  --name functionsDeployment \
  --template-file main.bicep \
  --parameters main.bicepparam \
  --parameters \
    keyVaultName=$KEY_VAULT_NAME \
    staticSites_pl_static_web_app_name=$PL_STATIC_WEB_APP_NAME \
  -g $RESOURCE_GROUP_NAME

パラメータの説明より、あとに設定したパラメータが優先される。

過去のBicep記事

Azure Functions の Bicep を node linux構成でできるだけシンプルに書いてみたメモ
Bicep デプロイ時に Azure Key Vault を使用して、セキュリティで保護されたパラメーター値を渡したメモ
BicepでAzure Container RegisterとAzure Container Appsを作ってAzure Functions をのせてみたメモ
Bicepで クラシック Application Insightsは2024年2月29日に廃止されました と出力されたことに対応したメモ

参考

省略可能のパラメーター --parameters
パラメーターの優先順位
readEnvironmentVariable
Bicep のスプレッド演算子

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