LoginSignup
4
2

More than 3 years have passed since last update.

Terraformでazurerm_role_assignmentするために必要な準備の手順

Last updated at Posted at 2020-10-04

はじめに

Terraformのazurerm_role_assignmentを使用すれば、アプリの各種権限を付与することができる。
https://www.terraform.io/docs/providers/azurerm/r/role_assignment.html

しかし、下記の権限を付与していないと、Terraformからのリソース作成が失敗してしまう。
Microsoft.Authorization/roleAssignments/write
Terraform公式ドキュメントには記載されていないので、対処法を記載する。

準備:Powershell 7 &AzurePowerShellAzのインストール

まず、Powershell 7 のインストールを行う。

次に、AzurePowerShellAzコマンドを実行できるよう、インストールを行う。

install.ps1
# AzurePowerShellAzのインストール
if ($PSVersionTable.PSEdition -eq 'Desktop' -and (Get-Module -Name AzureRM -ListAvailable)) {
    Write-Warning -Message ('Az module not installed. Having both the AzureRM and ' +
      'Az modules installed at the same time is not supported.')
} else {
    Install-Module -Name Az -AllowClobber -Scope CurrentUser
}

インストールバージョンを確認する

checkVersion.ps1
# powershell
$PSVersionTable.PSVersion
# AzurePowerShellAz
Get-InstalledModule -Name Az -AllVersions

#-------------------------
Major  Minor  Patch  PreReleaseLabel BuildLabel
-----  -----  -----  --------------- ----------
7      0      3
Version              Name                                Repository           Description
-------              ----                                ----------           -----------
4.7.0                Az                                  PSGallery            Microsoft Azure PowerShell - Cmdlets to 

[詳細]
https://docs.microsoft.com/ja-jp/powershell/azure/new-azureps-module-az?view=azps-4.7.0&viewFallbackFrom=azps-3.3.0

Terraformを実行しているサービスプリンシパルへのロールの付与

[Azure PowerShell を使用して Azure ロールの割り当てを追加または削除する]
https://docs.microsoft.com/ja-jp/azure/role-based-access-control/role-assignments-powershell

setup.ps1
# ログインする
Az login
#Azureサービスに接続する
Connect-AzAccount

# 組み込みのロール定義を確認する。内容を知っているなら、実行しなくてもよい。
Get-AzRoleDefinition
#「Terraformに使用しているサービスプリンシパルのObjectID」
$objID = "Hoge"
# AzureサブスクリプションのID
$subID = "Hoge"
New-AzRoleAssignment -ObjectId $objID -RoleDefinitionName "User Access Administrator" -Scope $subID
4
2
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
4
2