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?

Logic Apps(Standard)をBicepでデプロイする

0
Posted at

はじめに

Logic AppsのStandardプランをBicepでデプロイする公式ページがなかったので、備忘録として残しておきます。
特別な作業はなく、Azure Functionsを作るときと同様に
①App Service PlanとStorage Accountを事前に作成
②それをLogic Apps(Microsoft.Web/sites)の定義で指定
する必要がある程度でした。

準備

ディレクトリ構成

リソースグループからBicepで作成します。
各パラメータはbicepparamに保持し、デプロイ系のコマンドをまとめたスクリプトを準備しています。(GitHub Copilotが吐いたやつそのまま使ってるから良いスクリプトか不明)

infra/
├── scripts/
│   └── deploy.ps1
├── env/
│   └── dev.bicepparam
├── main.bicep
└── modules/
     ├── logicapps.bicep
     └── recourceGroup.bicep

Bicep

main.bicep

targetScope = 'subscription'

@description('prefix for naming all resources')
param prefix string

@description('location for all resources')
param location string = 'japaneast'

@allowed(['dev', 'stg', 'prod'])
@description('environment name')
param environment string

@description('Instance number for unique naming')
param instanceNumber string = '001'

@description('Customer name for tagging resources')
param tagCustomerName string

@description('Project name for tagging resources')
param tagProjectName string

@description('Registration number for tagging resources')
param tagRegistrationNumber string

@description('Owner name for tagging resources')
param tagOwnerName string

var commonTags = {
  Environment: environment
  CustomerName: tagCustomerName
  ProjectName: tagProjectName
  RegistrationNumber: tagRegistrationNumber
  OwnerName: tagOwnerName
}

var resourceGroupName = 'rg-${prefix}-${environment}-${location}-${instanceNumber}'

module resourceGroupModule 'modules/resourceGroup.bicep' = {
  name: 'deployResourceGroup'
  params: {
    resourceGroupName: resourceGroupName
    location: location
    tags: commonTags
  }
}

module logicappsModule 'modules/logicapps.bicep' = {
  name: 'deployLogicApps'
  scope: resourceGroup(resourceGroupName)
  params: {
    prefix: prefix
    location: location
    environment: environment
    instanceNumber: instanceNumber
    tags: commonTags
  }
}

resouceGroup.bicep

targetScope = 'subscription'

@description('Name of the resource group to create')
param resourceGroupName string

@description('Location for the resource group')
param location string

@description('Tags to apply to the resource group')
param tags object

resource resourceGroup 'Microsoft.Resources/resourceGroups@2021-04-01' = {
  name: resourceGroupName
  location: location
  tags: tags
}

output resourceGroupName string = resourceGroup.name
output resourceGroupId string = resourceGroup.id

logicapps.bicep

@description('prefix for naming all resources')
param prefix string

@description('Location for the resource group')
param location string

@allowed(['dev', 'stg', 'prod'])
@description('environment name')
param environment string

@description('Instance number for unique naming')
param instanceNumber string

@description('Tags to apply to the resource group')
param tags object

param appServicePlanSkuName string = 'WS1'
param appServicePlanSkuTier string = 'WorkflowStandard'

resource appServicePlan 'Microsoft.Web/serverfarms@2024-11-01' = {
  name: 'asp-${prefix}-${environment}-${location}-${instanceNumber}'
  location: location
  tags: tags
  kind: 'elastic'
  sku: {
    name: appServicePlanSkuName
    tier: appServicePlanSkuTier
  }
  properties: {
    perSiteScaling: false
    elasticScaleEnabled: true
    maximumElasticWorkerCount: 20
    isSpot: false
    reserved: false
    isXenon: false
    hyperV: false
    targetWorkerCount: 0
    targetWorkerSizeId: 0
    zoneRedundant: false
    asyncScalingEnabled: false
  }
}

var logicAppStorageName = toLower(take(replace('stlogic${prefix}${environment}${instanceNumber}','-', ''), 24))

resource logicAppStorage 'Microsoft.Storage/storageAccounts@2025-01-01' = {
  name: logicAppStorageName
  location: location
  sku: {
    name: 'Standard_LRS'
  }
  kind: 'StorageV2'
  properties: {
    allowBlobPublicAccess: false
    allowSharedKeyAccess: true
    minimumTlsVersion: 'TLS1_2'
    supportsHttpsTrafficOnly: true
  }
  tags: tags
}

resource blobService 'Microsoft.Storage/storageAccounts/blobServices@2025-01-01' = {
  parent: logicAppStorage
  name: 'default'
  properties: {
    deleteRetentionPolicy: {}
  }
}

resource defaultContainer 'Microsoft.Storage/storageAccounts/blobServices/containers@2025-01-01' = {
  parent: blobService
  name: 'default-container'
  properties: {
    publicAccess: 'None'
  }
}

resource logicApp 'Microsoft.Web/sites@2024-11-01' = {
  name: 'logic-${prefix}-${environment}-${location}-${instanceNumber}'
  location: location
  kind: 'functionapp,workflowapp'
  identity: {
    type: 'SystemAssigned'
  }
  properties: {
    serverFarmId: appServicePlan.id
    httpsOnly: true
    siteConfig:{
        numberOfWorkers: 1
        alwaysOn: true
        use32BitWorkerProcess: false
        appSettings:[
            {
                name: 'AzureWebJobsStorage'
                value: 'DefaultEndpointsProtocol=https;AccountName=${logicAppStorage.name};AccountKey=${logicAppStorage.listKeys().keys[0].value};EndpointSuffix=${az.environment().suffixes.storage}'
            }
            {
                name: 'FUNCTIONS_EXTENSION_VERSION'
                value: '~4'
            }
            {
                name: 'FUNCTIONS_WORKER_RUNTIME'
                value: 'dotnet'
            }
            {
                name: 'APP_KIND'
                value: 'workflowapp'
            }
            {
                name: 'WEBSITE_CONTENTAZUREFILECONNECTIONSTRING'
                value: 'DefaultEndpointsProtocol=https;AccountName=${logicAppStorage.name};AccountKey=${logicAppStorage.listKeys().keys[0].value};EndpointSuffix=${az.environment().suffixes.storage}'
            }
            {
                name: 'WEBSITE_CONTENTSHARE'
                value: toLower('logic-${prefix}-${environment}-${location}-${instanceNumber}')
            }
        ]
    }
  }
  tags: tags
}

パラメータ

共通のタグを付与したいのでその設定値含めてパラメータに指定

dev.bicepparam

using '../main.bicep'

param prefix = ''
param location = 'japanwest'
param environment = 'dev'
param instanceNumber = '001'
param tagCustomerName = ''
param tagProjectName = ''
param tagRegistrationNumber = ''
param tagOwnerName = ''

スクリプト

deploy.ps1

param(
    [string]$Location = "japanwest",
    [string]$ParamFile = ".\infra\env\dev.bicepparam",
    [string]$SubscriptionId,
    [switch]$WhatIf,
    [switch]$NoPrompt
)

$ErrorActionPreference = 'Stop'
Set-StrictMode -Version Latest

function Assert-AzCli() {
  if (-not (Get-Command az -ErrorAction SilentlyContinue)) {
    throw "Azure CLI (az) が見つかりません。インストールしてパスを通してください。"
  }
}

function Assert-LoggedIn() {
  try {
    az account show --only-show-errors 1>$null 2>$null
  } catch {
    Write-Host "Azure へのログインが必要です。" -ForegroundColor Yellow
    az login --only-show-errors | Out-Null
  }
}

Assert-AzCli
Assert-LoggedIn

if ($SubscriptionId) {
  Write-Host "切り替え中: サブスクリプション $SubscriptionId" -ForegroundColor Cyan
  az account set --subscription $SubscriptionId --only-show-errors
}

Write-Host "パラメータ: $ParamFile" -ForegroundColor Cyan
if (-not (Test-Path $ParamFile)) {
  throw "パラメータファイルが見つかりません: $ParamFile"
}

if ($WhatIf) {
  Write-Host "What-If を実行します…" -ForegroundColor Green
  az deployment sub what-if --location $Location --parameters $ParamFile --only-show-errors
  if ($LASTEXITCODE -ne 0) { throw "what-if に失敗しました。" }
}

if (-not $NoPrompt) {
  $ans = Read-Host "本番適用を実行しますか? (y/N)"
  if ($ans -notin @('y','Y','yes','YES')) { Write-Host "キャンセルしました。"; exit 0 }
}

Write-Host "デプロイを実行します…" -ForegroundColor Green
az deployment sub create --location $Location --parameters $ParamFile --only-show-errors
if ($LASTEXITCODE -ne 0) { throw "デプロイに失敗しました。" }

Write-Host "デプロイが完了しました。" -ForegroundColor Green

参考記事

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?