|
| 1 | +--- |
| 2 | +title: "BicepリンターでBicepコードを最新化:効率的なリファクタリング手法" |
| 3 | +date: 2025/05/09 00:00:01 |
| 4 | +postid: b |
| 5 | +tag: |
| 6 | + - Azure |
| 7 | + - AzureCLI |
| 8 | + - AzureBicep |
| 9 | +category: |
| 10 | + - Infrastructure |
| 11 | +thumbnail: /images/20250509b/thumbnail.png |
| 12 | +author: 三浦克之 |
| 13 | +lede: "Azure Bicepは、Microsoft Azureのリソースを効率的に管理するために開発された宣言型の言語です。Azure上のインフラストラクチャをコードとして定義し、デプロイすることを可能にします。Azure Bicepは便利で強力なツールですが..." |
| 14 | +--- |
| 15 | +<div class="note warn" style="background: #fdf9e2; padding:16px; margin:24px 12px; border-radius:8px;"><span class="fa fa-fw fa-check-circle"></span> |
| 16 | + |
| 17 | +グループ会社であるFutureOneの Qiita Organizationで公開された [記事](https://qiita.com/Miura597/items/b83e3371ec97b8dea74c) をクロスポストで公開しています。 |
| 18 | + |
| 19 | +</div> |
| 20 | + |
| 21 | +# はじめに |
| 22 | + |
| 23 | +Azure Bicepは、Microsoft Azureのリソースを効率的に管理するために開発された宣言型の言語です。Azure上のインフラストラクチャをコードとして定義し、デプロイが可能です。 |
| 24 | + |
| 25 | +Azure Bicepは便利で強力なツールですが、Azureの急速な進化に追従する必要があるため、どのようにコードを最新化していくかが悩みどころです。Azureは頻繁に新しいサービスやAPIバージョンをリリースするため、Bicepコードを最新の状態に保つためには、これらの変更に迅速に対応する必要があります。 |
| 26 | + |
| 27 | +本記事では、Microsoft社のツールを使ったBicepコードの効率的な最新化する方法を紹介します。 |
| 28 | + |
| 29 | +# Bicep リンター |
| 30 | + |
| 31 | +Bicepコードの最新化に役立つのが、[Bicepリンター](https://learn.microsoft.com/ja-jp/azure/azure-resource-manager/bicep/linter)です。 |
| 32 | + |
| 33 | +>Bicep リンターは、Bicep ファイルに構文エラーとベスト プラクティス違反がないかチェックします。 リンターを使用すると、開発時のガイダンスが提供され、コーディング標準を適用できます。 ファイルのチェックに使用するベスト プラクティスをカスタマイズできます。 |
| 34 | +
|
| 35 | +こちらのツールを使うことで、コードの最新化が効率的に実施できます。 |
| 36 | + |
| 37 | +# 環境情報 |
| 38 | + |
| 39 | +今回は、`C:\Bicep` に配置した `main.bicep`* と呼び出されるモジュール群を対象にコードの最新化を行います。 |
| 40 | + |
| 41 | +<img src="/images/20250509b/image.png" alt="image.png" width="981" height="413" loading="lazy"> |
| 42 | + |
| 43 | +## 事前準備 |
| 44 | + |
| 45 | +最新のAPIバージョンが表示されない場合もあるため、以下のアップデートを事前に行っておきます。 |
| 46 | + |
| 47 | +```sh Bicepのアップデート |
| 48 | +az bicep upgrade |
| 49 | +``` |
| 50 | + |
| 51 | +# リンターの実行 |
| 52 | + |
| 53 | +以下コマンドでリンターを実行できます。 |
| 54 | + |
| 55 | +```sh |
| 56 | +az bicep lint |
| 57 | +``` |
| 58 | + |
| 59 | +今回は実施環境に合わせて以下のコマンドを実行します。 |
| 60 | + |
| 61 | +```sh |
| 62 | +az bicep lint -f .\main.bicep |
| 63 | +``` |
| 64 | + |
| 65 | +- `-f` によるbicepファイルの指定は必須です。 |
| 66 | +- `main.bicep` が参照しているモジュールファイルも対象にしてくれます。 |
| 67 | + |
| 68 | +以下のようにチェック結果が表示されます。 |
| 69 | + |
| 70 | +<img src="/images/20250509b/image_2.png" alt="" width="1200" height="306" loading="lazy"> |
| 71 | + |
| 72 | +デフォルトでは様々なチェック項目に従って結果が表示されます。 |
| 73 | + |
| 74 | +チャック内容をカスタマイズしたい場合は、`bicepconfig.json` を利用します。 |
| 75 | + |
| 76 | +- 参考: [Bicep 構成のリンター設定 - Azure Resource Manager | Microsoft Learn](https://learn.microsoft.com/ja-jp/azure/azure-resource-manager/bicep/bicep-config-linter) |
| 77 | + |
| 78 | +今回はAPIバージョンのみに限定したいので、`bicepconfig.json` を以下のようにします。 |
| 79 | + |
| 80 | +```json bicepconfig.json |
| 81 | +{ |
| 82 | + "analyzers": { |
| 83 | + "core": { |
| 84 | + "enabled": true, |
| 85 | + "rules": { |
| 86 | + "adminusername-should-not-be-literal": { |
| 87 | + "level": "off" |
| 88 | + }, |
| 89 | + "artifacts-parameters": { |
| 90 | + "level": "off" |
| 91 | + }, |
| 92 | + "decompiler-cleanup": { |
| 93 | + "level": "off" |
| 94 | + }, |
| 95 | + "explicit-values-for-loc-params": { |
| 96 | + "level": "off" |
| 97 | + }, |
| 98 | + "max-asserts": { |
| 99 | + "level": "off" |
| 100 | + }, |
| 101 | + "max-outputs": { |
| 102 | + "level": "off" |
| 103 | + }, |
| 104 | + "max-params": { |
| 105 | + "level": "off" |
| 106 | + }, |
| 107 | + "max-resources": { |
| 108 | + "level": "off" |
| 109 | + }, |
| 110 | + "max-variables": { |
| 111 | + "level": "off" |
| 112 | + }, |
| 113 | + "nested-deployment-template-scoping": { |
| 114 | + "level": "off" |
| 115 | + }, |
| 116 | + "no-conflicting-metadata" : { |
| 117 | + "level": "off" |
| 118 | + }, |
| 119 | + "no-deployments-resources" : { |
| 120 | + "level": "off" |
| 121 | + }, |
| 122 | + "no-hardcoded-env-urls": { |
| 123 | + "level": "off" |
| 124 | + }, |
| 125 | + "no-hardcoded-location": { |
| 126 | + "level": "off" |
| 127 | + }, |
| 128 | + "no-loc-expr-outside-params": { |
| 129 | + "level": "off" |
| 130 | + }, |
| 131 | + "no-unnecessary-dependson": { |
| 132 | + "level": "off" |
| 133 | + }, |
| 134 | + "no-unused-existing-resources": { |
| 135 | + "level": "off" |
| 136 | + }, |
| 137 | + "no-unused-params": { |
| 138 | + "level": "off" |
| 139 | + }, |
| 140 | + "no-unused-vars": { |
| 141 | + "level": "off" |
| 142 | + }, |
| 143 | + "outputs-should-not-contain-secrets": { |
| 144 | + "level": "off" |
| 145 | + }, |
| 146 | + "prefer-interpolation": { |
| 147 | + "level": "off" |
| 148 | + }, |
| 149 | + "prefer-unquoted-property-names": { |
| 150 | + "level": "off" |
| 151 | + }, |
| 152 | + "protect-commandtoexecute-secrets": { |
| 153 | + "level": "off" |
| 154 | + }, |
| 155 | + "secure-parameter-default": { |
| 156 | + "level": "off" |
| 157 | + }, |
| 158 | + "secure-params-in-nested-deploy": { |
| 159 | + "level": "off" |
| 160 | + }, |
| 161 | + "secure-secrets-in-params": { |
| 162 | + "level": "off" |
| 163 | + }, |
| 164 | + "simplify-interpolation": { |
| 165 | + "level": "off" |
| 166 | + }, |
| 167 | + "simplify-json-null": { |
| 168 | + "level": "off" |
| 169 | + }, |
| 170 | + "use-parent-property": { |
| 171 | + "level": "off" |
| 172 | + }, |
| 173 | + "use-recent-api-versions": { |
| 174 | + "level": "warning", |
| 175 | + "maxAllowedAgeInDays": 365 |
| 176 | + }, |
| 177 | + "use-recent-module-versions": { |
| 178 | + "level": "off" |
| 179 | + }, |
| 180 | + "use-resource-id-functions": { |
| 181 | + "level": "off" |
| 182 | + }, |
| 183 | + "use-resource-symbol-reference": { |
| 184 | + "level": "off" |
| 185 | + }, |
| 186 | + "use-safe-access": { |
| 187 | + "level": "off" |
| 188 | + }, |
| 189 | + "use-secure-value-for-secure-inputs": { |
| 190 | + "level": "off" |
| 191 | + }, |
| 192 | + "use-stable-resource-identifiers": { |
| 193 | + "level": "off" |
| 194 | + }, |
| 195 | + "use-stable-vm-image": { |
| 196 | + "level": "off" |
| 197 | + }, |
| 198 | + "what-if-short-circuiting": { |
| 199 | + "level": "off" |
| 200 | + } |
| 201 | + } |
| 202 | + } |
| 203 | + } |
| 204 | +} |
| 205 | +``` |
| 206 | + |
| 207 | +作成したファイルを配置します。 |
| 208 | + |
| 209 | +<img src="/images/20250509b/image_3.png" alt="" width="1200" height="421" loading="lazy"> |
| 210 | + |
| 211 | +その上で、コマンドを再実行すると表示がフィルタリングされています。 |
| 212 | + |
| 213 | +<img src="/images/20250509b/image_4.png" alt="" width="1200" height="158" loading="lazy"> |
| 214 | + |
| 215 | +# リファクタリング方法 |
| 216 | + |
| 217 | +警告が出たリソースのコードを確認します。 |
| 218 | + |
| 219 | +VSCode用のBicep拡張機能をインストールしている場合はそのリソースで使えるバージョン一覧がインテリセンスとして表示されます。 |
| 220 | + |
| 221 | +最新のAPIバージョンが表示された場合は、最新バージョンへの適用を検討します。 |
| 222 | + |
| 223 | +<img src="/images/20250509b/image_5.png" alt="" width="1200" height="277" loading="lazy"> |
| 224 | + |
| 225 | +# さいごに |
| 226 | + |
| 227 | +以上が最新APIバージョンへの効率的な対応方法のご紹介でした。 |
| 228 | + |
| 229 | +APIバージョンが変わることで仕様の変更などが加わる可能性があるため、デプロイテストの実施はお忘れなく。 |
| 230 | + |
| 231 | +リンターツールは、コード上でセキュリティ的に問題のある個所を表示するなど、コードの最新化以外にBicepコードのリファクタリングに使えるので便利です。用途に合わせて活用をお勧めします。 |
0 commit comments