[アップデート] Amazon CloudWatch で AWS CodeArtifact のメトリクスが取得出来るようになったので NuGet パッケージの公開と利用を行ってみた

2024.01.01

いわさです。

先日 12 月 29 日のアップデートで、CloudWatch で AWS CodeArtifact ののメトリクスが取得出来るようになりました。

CodeArtifact のメトリクスって何に使うのだろうと思った方が多いと思うのですが、ユースケースとしてはサービスクォータ監視のために使うことが想定されています。

CodeArtifact のサービスクォータ

CodeArtifact は AWS 上でユーザー独自のパッケージ管理リポジトリを提供出来るマネージドサービスで、組織内のプライベートパッケージリポジトリを運用したい時などに特に活用出来ます。
利用方法の一つとして、公開リポジトリのダウンストリームとしても利用出来るので、大規模に使用している場合だとアクセスが集中する場合があります。

そして CodeArtifact には次のようなクォータが存在し、上限に達すると API の実行に失敗します。

CI/CD パイプラインなどが失敗する可能性もあり、上限緩和が必要かどうか API の使用量を把握しておきたいところだったのですが、これまでは EventBridge を利用した監視の仕組みを構築する必要がありました。

今回のアップデートでメトリクスをそのまま使うことが出来るようになったので、使用量の把握という意味だとより簡単に監視の仕組みを構築出来るようになりました。

私は AWS CodeArtifact を使ったことが無かったので、今回は導入から利用・メトリクスの確認までを一通りためしてみましたので紹介します。

リポジトリの作成

今回は私が使い慣れている NuGet パッケージマネージャーを使いたいと思います。
.NET で使われるパッケージマネージャーです。

まずは CodeArtifact のメニューから新規リポジトリを作成します。
パブリックアップストリームリポジトリに nuget-store を指定します。

リポジトリの作成はこれまです。簡単です。
作成されたリポジトリを開くと、接続手順を表示することが出来るので、こちらに従って接続を行います。

いくつか方法があるのですが、今回は Mac 上へ AWS CLI を使ってリポジトリ参照情報の登録を行います。
Mac 上へインストール済みの NuGet ツールへ CodeArtifact で作成したリポジトリを認識させる感じです。

% aws codeartifact login --tool nuget --repository hoge0101repo --domain hoge0101 --domain-owner 123456789012 --region ap-northeast-1 --profile hoge
admin
Added source hoge0101/hoge0101repo to the user level NuGet.Config
Successfully configured nuget to use AWS CodeArtifact repository https://hoge0101-123456789012.d.codeartifact.ap-northeast-1.amazonaws.com/nuget/hoge0101repo/ 
Login expires in 12 hours at 2024-01-01 17:42:25+09:00

% nuget sources
Registered Sources:
  1.  nuget.org [Enabled]
      https://api.nuget.org/v3/index.json
  2.  hoge0101/hoge0101repo [Enabled]
      https://hoge0101-123456789012.d.codeartifact.ap-northeast-1.amazonaws.com/nuget/hoge0101repo/v3/index.json

nuget sourcesコマンドで構成したソースとして CodeArtifact リポジトリが登録されていることが確認出来ましたね。

登録するパッケージを用意する

せっかくプライベートリポジトリが用意出来たので、何か適当なパッケージを作って登録してみましょう。
NuGet パッケージの作成方法は次のマイクロソフト公式ドキュメントから始めるのが良いでしょう。

クラスライブラリを作成し、プロジェクトファイルに情報を追記のうえパッケージ化します。

% dotnet new classlib
The template "Class Library" was created successfully.

Processing post-creation actions...
Restoring /Users/iwasa.takahito/work/hoge0101artifact/hoge0101artifact.csproj:
  Determining projects to restore...
  Restored /Users/iwasa.takahito/work/hoge0101artifact/hoge0101artifact.csproj (in 31 ms).
Restore succeeded.

:

<ここでプロジェクトファイルに以下を追記>

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net7.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <PackageId>iwasa.hoge.Test</PackageId>
    <Version>1.0.0</Version>
    <Authors>hogeiwasa</Authors>
    <Company>hogeclassmethod</Company>
  </PropertyGroup>

</Project>

:

% dotnet pack
MSBuild version 17.5.1+f6fdcf537 for .NET
  Determining projects to restore...
  Restored /Users/iwasa.takahito/work/hoge0101artifact/hoge0101artifact.csproj (in 43 ms).
  hoge0101artifact -> /Users/iwasa.takahito/work/hoge0101artifact/bin/Debug/net7.0/hoge0101artifact.dll
  Successfully created package '/Users/iwasa.takahito/work/hoge0101artifact/bin/Debug/iwasa.hoge.Test.1.0.0.nupkg'.

おー、作成出来ましたね。NuGet パッケージこれまでは使うことしかしてなかったので、作るのは初めてです。
クラスライブラリを作成することはあったのですが、MSI とか作って配布してましたね。
NuGet パッケージ化して、CodeArtifact などで組織内に配布するというのもありだな。

ライブラリをデバッグモードで作成しちゃってますが、今回は検証用途なのでまぁ良いでしょう。
Mac なので Visual Studio が使えません。そのため次の AWS 公式ドキュメントを参考にnuget pushコマンドで登録を行います。

API キーを指定していないので警告が出ていますが、今回は無視します。
マイクロソフト公式の手順だと NuGet から API キーを取得して使う手順になっているのですが、CodeArtifact の場合の必要性がわかってないです。今回はこのままいってみましょう。

% nuget push bin/Debug/iwasa.hoge.Test.1.0.0.nupkg -source hoge0101/hoge0101repo
WARNING: No API Key was provided and no API Key could be found for 'https://hoge0101-123456789012.d.codeartifact.ap-northeast-1.amazonaws.com/nuget/hoge0101repo/v2/package'. To save an API Key for a source use the 'setApiKey' command.
Pushing iwasa.hoge.Test.1.0.0.nupkg to 'https://hoge0101-123456789012.d.codeartifact.ap-northeast-1.amazonaws.com/nuget/hoge0101repo/v2/package'...
  PUT https://hoge0101-123456789012.d.codeartifact.ap-northeast-1.amazonaws.com/nuget/hoge0101repo/v2/package/
  Created https://hoge0101-123456789012.d.codeartifact.ap-northeast-1.amazonaws.com/nuget/hoge0101repo/v2/package/ 1095ms
Your package was pushed.

うまく送信出来ている気がします。
CodeArtifact コンソールのリポジトリ画面でパッケージ一覧を確認してみると作成したパッケージが登録されていることを確認出来ました。

良いですね!

インストールして使ってみる

コンソール上で対象パッケージの使いたいバージョンのパッケージ内容を確認してみると、アセットや依存関係などが確認できます。
公式の NuGet Gallery 相当の情報は揃っているのでないでしょうか。

上記でインストールのコマンドが取得出来るので新規コンソールアプリケーションを作成して、パッケージをインストールしてみます。

% dotnet new console
The template "Console App" was created successfully.

Processing post-creation actions...
Restoring /Users/iwasa.takahito/work/hoge0101artifact/hogeinstall/hogeinstall.csproj:
  Determining projects to restore...
  Restored /Users/iwasa.takahito/work/hoge0101artifact/hogeinstall/hogeinstall.csproj (in 35 ms).
Restore succeeded.

% nuget install iwasa.hoge.test -Source "hoge0101/hoge0101repo"
Feeds used:
  https://hoge0101-123456789012.d.codeartifact.ap-northeast-1.amazonaws.com/nuget/hoge0101repo/v3/index.json

Installing package 'iwasa.hoge.test' to '/Users/iwasa.takahito/work/hoge0101artifact/hogeinstall'.
  GET https://hoge0101-123456789012.d.codeartifact.ap-northeast-1.amazonaws.com/nuget/hoge0101repo/v3/registration5-gz-semver2/iwasa.hoge.test/index.json
  OK https://hoge0101-123456789012.d.codeartifact.ap-northeast-1.amazonaws.com/nuget/hoge0101repo/v3/registration5-gz-semver2/iwasa.hoge.test/index.json 960ms


Attempting to gather dependency information for package 'iwasa.hoge.test.1.0.0' with respect to project '/Users/iwasa.takahito/work/hoge0101artifact/hogeinstall', targeting 'Any,Version=v0.0'
Gathering dependency information took 59 ms

インストール出来ましたね。

メトリクスを確認してみる

ここまででパッケージの登録と利用を行いました。
ここから本題の CloudWatch メトリクスを確認してみましょう。

本日時点でメトリクスに関する情報が全然見当たらずかなり困ったのですが、どうやら次のドキュメントの以下の部分が追記されていることだけ確認出来ました。

You can use Amazon CloudWatch metrics to view CodeArtifact usage by operation. CloudWatch metrics includes all requests made to CodeArtifact, and requests are shown by account. You can view these metrics in CloudWatch metrics by navigating to the Usage/By AWS Resource AWS namespace. For more information, see Use Amazon CloudWatch metrics in the Amazon CloudWatch User Guide.

CloudWatch で使用量メトリクス → AWS リソースを見てみます。

サービス名「CodeArtifact」で 19 のメトリクスを確認しました。
CloudWatch メトリクスは実績のあった使用量に応じて表示された気がするので、使っていくと他のリソースも表示されるかもしれません。

各 API リソースの CallCount が取得出来るようですね。

試しにReadFromRepositoryPublishPackageVersionを取得してみたところ、1 回づつカウントされていることが確認出来ました。

さいごに

本日は Amazon CloudWatch で AWS CodeArtifact のメトリクスが取得出来るようになったので NuGet パッケージの公開と利用を行ってみました。

CodeArtifact を大規模に利用している方、クリティカルな CI/CD パイプラインなどで利用している方は是非今回のメトリクスを活用してみてください。

今回 CodeArtifact を初めて使ったのですが、独自のパッケージリポジトリサービス良いですね。