1
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?

Azure マネージドID の第一歩

Posted at

インフラ屋としては認証に詳しくなりたい。Azure にはマネージドID という素晴らしい認証の機能がある。
一個ずつ試していきましょう。

概要

概要といいつつ、マネージドID そのものの解説はたくさん見つかると思うので省略します。
親愛なるやまぱんさんの記事がわかりやすく、しかも他の記事もついているので何回も読んでます。
https://qiita.com/aktsmm/items/1a1d8b79f18f0c7a5331

今回やることは、Azure Powershell を使ってマネージドID の権限でリソースグループの作成です。

  1. 仮想マシンの作成
  2. マネージドID 付与
  3. 権限をマネージドID に付与
  4. 仮想マシンに入ってPowershell からAzure Powershell コマンドでマネージドID 認証
  5. リソースグループ作成

作業

リソースの作成

適当にVNet, 仮想マシンを作ります。パブリックIP で直接接続のパターンでやってます。
仮想マシンですが、Windows Server 2022 で作りました。
仮想マシンの作成時にマネージドID を付けることもできますが、ここではつけません。
image.png

Azure Powershell のインストール

仮想マシンにRDP し、Azure Powershell をインストールします。
https://learn.microsoft.com/ja-jp/powershell/azure/install-azps-windows?view=azps-12.5.0&tabs=powershell&pivots=windows-psgallery
Windows Server 2022 で仮想マシンを作った時点では、CurrentUser がRemoteSigned になっていないので、そこから従います。

> Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
> Install-Module -Name Az -Repository PSGallery -Force

1分でインストール完了しました。

マネージドID で認証

マネージドID がついていれば、Azure が内部でよしなにしてくれることで認証が完了します。認証するコマンドはこれです。-Identity オプションがなければ認証を促す画面が出てきます。

> Connect-AzAccount -Identity

まだマネージドID を有効化していないため、エラーが出ます。

PS C:\Users\testuser> Connect-Azaccount -Identity
WARNING: Unable to acquire token for tenant 'organizations' with error 'ManagedIdentityCredential authentication
unavailable. The requested identity has not been assigned to this resource.
Status: 400 (Bad Request)

マネージドID を有効にします。
仮想マシン > セキュリティ > ID で次の画面です。
image.png

オンにして保存します。
image.png

数十秒でマネージドID が作成されました。オブジェクトID が出てますね。

image.png

オブジェクトID と言えばEntra ID です。
探してみましょう。
Azure portal の検索窓でEntra ID で検索して、ディレクトリ内でさっき出たオブジェクトID を検索してみます。
image.png

すると仮想マシンが「エンタープライズ アプリケーション」として見えます。今回はこの深掘りはしません。
image.png

マネージドID 利用

仮想マシンに戻って、マネージドID で認証するコマンドを打ちます。
image.png

認証に成功し、テナントID が表示されます。当然ながら、現時点ではSubscription に何も権限を与えていないので表示されません。

ポータルに戻って、マネージドID に権限を与えます。
ここから、サブスクリプションスコープで共同作成者ロールを付与しました。
image.png

そして仮想マシンで再度入りなおすと次の通り。
image.png

これでAzure Powershell でAzure のリソースを操作する準備が整いました。

リソースグループ作成

リソースグループの作成コマンドを打ちます。

> New-AzResourceGroup -Name exampleGroup -Location japaneast

リソースグループを作成できました。
image.png

この通り、ポータルからも"exampleGroup" が確認できます。
image.png

お疲れさまでした。

1
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
1
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?