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?

Bicep Tips - Bicep事始め2(リソースグループとリソースを一緒に作成)

Posted at

概要

前回の続き。
前回、Bicepでリソースグループを作成するところまでできました。
今回は、作成したリソースグループの中に、App Serviceを作成するところまでしてみたいと思います。

環境

・MacBook Air
・Power Shell
・Azure PowerShell
・Visual Studio Code

手順

まずは、Microsoft Learnの記事を確認します。
Bicep を使用してリソース グループを作成する - リソース グループとリソースを作成する

上記の記事では、リソースグループ作成後に、Azure Storage Accountを作成する例が記載されています。
これを読んだ感じだと、リソースグループ作成後に、App Serviceを作成するBicepファイルを呼び出せばいいみたいですね。
ということで、先にApp Serviceを作成するBicepファイルを作成してみたいと思います。

App Serviceを作成するBicepファイル

最初に、前回作成したmain.bicepと同じフォルダに、
appservice.bicepファイルを作成します。

App Serviceの構築ですが、Microsoft Learnに記事があるので、
こちらを参考に作成していきます。
※あ、今回作成するApp ServiceのプラットフォームはLinuxです。

Bicep を使用して App Service アプリを作成する

以下は、上記のページに記載されているサンプルの転記です。

param webAppName string = uniqueString(resourceGroup().id) // Generate unique String for web app name
param sku string = 'F1' // The SKU of App Service Plan
param linuxFxVersion string = 'node|14-lts' // The runtime stack of web app
param location string = resourceGroup().location // Location for all resources
param repositoryUrl string = 'https://github.com/Azure-Samples/nodejs-docs-hello-world'
param branch string = 'main'
var appServicePlanName = toLower('AppServicePlan-${webAppName}')
var webSiteName = toLower('wapp-${webAppName}')

resource appServicePlan 'Microsoft.Web/serverfarms@2020-06-01' = {
  name: appServicePlanName
  location: location
  properties: {
    reserved: true
  }
  sku: {
    name: sku
  }
  kind: 'linux'
}

resource appService 'Microsoft.Web/sites@2020-06-01' = {
  name: webSiteName
  location: location
  properties: {
    serverFarmId: appServicePlan.id
    siteConfig: {
      linuxFxVersion: linuxFxVersion
    }
  }
}

resource srcControls 'Microsoft.Web/sites/sourcecontrols@2021-01-01' = {
  name: '${appService.name}/web'
  properties: {
    repoUrl: repositoryUrl
    branch: branch
    isManualIntegration: true
  }
}

当社で開発しているSaaSのデプロイ用のベースにしたいので、
ここから、以下の項目を変更してBicepファイルを作成していきます。

Webアプリの名前

1行目で、webAppNameというパラメーターが設定されていて、
この値を使ってApp Service Plan名とWebサイトのプレフィックスが生成されています。
ここでは、生成時の引数で指定したいので、イコールから先を削除しました。

param webAppName string

また、webAppNameで指定された文字列をそのままWebサイト名に使いたいので、
webSiteNameという変数の行を書き換えました。

var webSiteName = toLower('${webAppName}')

SKUの指定

2行目でApp Service PlanのSKUが指定されています。
ここは用途によっていろいろ変わるかと思いますが、
開発用ということで一旦B1にしてみます。

param sku string = 'B1' // The SKU of App Service Plan

ランタイムスタックの指定

3行目で、Webアプリ用のランタイムスタックが指定されています。
当社では、Webアプリの開発にASP .NET Coreを使用していて、
.NET 8を指定したいところ。

param linuxFxVersion string = 'DOTNETCORE|8.0' 

最終的にこんな感じ。

param webAppName string // Generate unique String for web app name
param sku string = 'B1' // The SKU of App Service Plan
param linuxFxVersion string = 'DOTNETCORE|8.0' // The runtime stack of web app
param location string = resourceGroup().location // Location for all resources
var appServicePlanName = toLower('AppServicePlan-${webAppName}')
var webSiteName = toLower('${webAppName}')

resource appServicePlan 'Microsoft.Web/serverfarms@2020-06-01' = {
  name: appServicePlanName
  location: location
  properties: {
    reserved: true
  }
  sku: {
    name: sku
  }
  kind: 'linux'
}

resource appService 'Microsoft.Web/sites@2020-06-01' = {
  name: webSiteName
  location: location
  properties: {
    serverFarmId: appServicePlan.id
    siteConfig: {
      linuxFxVersion: linuxFxVersion
    }
  }
}

Webアプリのソースコード

33行目以降で、Webアプリの継続的デプロイの設定が行われるようです。
一旦削除。

リソースグループ作成用のBicepファイルから、App Service作成用Bicepを呼び出す

appservice.bicepを呼び出す定義を追加します。
appservice.bicepのパラメーターであるwebAppNameを、実行時の引数で指定されるようにします。

targetScope='subscription'

param resourceGroupName string
param resourceGroupLocation string
param webAppName string

resource newRG 'Microsoft.Resources/resourceGroups@2024-11-01' = {
  name: resourceGroupName
  location: resourceGroupLocation
}

module newAppService 'appservice.bicep' = {
  scope: newRG
  params: {
    webAppName: webAppName
  }
}

Bicepファイルを実行する

Azue Power Shellで、以下のコマンドを実行します。
※カレントディレクトリの移動およびAzureとの接続が完了している前提。

New-AzSubscriptionDeployment -location japanwest -TemplateFile main.bicep -resourceGroupName demoResourceGroup -resourceGroupLocation japanwest -webAppName HapikuroTestWebApp

Azure PowerShell上には実行結果として以下のような表示が出ました。
image.png

Azue Portal上でリソースを確認したところ、
ちゃんと生成されていますね。

image.png

SKU(料金プラン)もちゃんと変更されていました。
image.png

次回は、Azure App Serviceの生成について、もう少し深掘りしたいと思います。

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?