azure-appconfiguration-py について
azure-appconfiguration-py は、Azure App Configuration の集中管理されたアプリ設定とフィーチャーフラグを扱う Python SDK です。ラベルや条件による絞り込み、読み取り専用制御、スナップショット、同期・非同期クライアントを提供します。
主な機能
- 01設定の取得と更新
- 02キーまたはラベルによる絞り込み
- 03フィーチャーフラグの管理
- 04設定の読み取り専用化
- 05スナップショットの作成と一覧表示
- 06同期・非同期クライアント
活用シーン
- 01環境ラベルによる設定の分離
- 02アプリ設定の一部取得
- 03フィーチャーフラグ状態の管理
- 04デプロイ前の設定スナップショット作成
インストール
コマンドをコピーしてターミナルで実行します。
$
npx skills add https:/ / github.com/ sickn33/ agentic-awesome-skills --skill azure-appconfiguration-pyインストール後、Claude Code で/azure-appconfiguration-pyを入力して読み込まれたか確認します。
詳細
Python 用 Azure App Configuration SDK
機能フラグと動的設定を備えた、一元的な構成管理です。
インストール
pip install azure-appconfiguration
環境変数
AZURE_APPCONFIGURATION_CONNECTION_STRING=Endpoint=https://<name>.azconfig.io;Id=...;Secret=...
# Or for Entra ID:
AZURE_APPCONFIGURATION_ENDPOINT=https://<name>.azconfig.io
認証
接続文字列
import os
from azure.appconfiguration import AzureAppConfigurationClient
client = AzureAppConfigurationClient.from_connection_string(
os.environ["AZURE_APPCONFIGURATION_CONNECTION_STRING"]
)
Entra ID
from azure.appconfiguration import AzureAppConfigurationClient
from azure.identity import DefaultAzureCredential
client = AzureAppConfigurationClient(
base_url=os.environ["AZURE_APPCONFIGURATION_ENDPOINT"],
credential=DefaultAzureCredential()
)
各例は独立した断片であり、Entra 認証には azure-identity が必要です。また、プロジェクトのエンドポイント変数も必要です。置換や削除を行う前に、ストア、ラベル、キーを確認し、現在の ETag を読み取ってください。同時編集に備え、適切な照合条件を使用してください。機能フラグの変更は実際のユーザーに影響します。また、設定のロック解除には、別途、権限のある変更が必要です。承認済みの合成値を使用し、一覧表示の範囲を最小限に抑え、認証情報や値のペイロードをログに記録しないようにし、失敗時には非同期の認証情報とクライアントの両方を閉じてください。スナップショットについては、実際の作成状況を確認し、復元手順をテストする必要があります。スキーマ上有効なフラグだけでは、段階的なロールアウトや A/B テストは実現されません。
構成設定
設定の取得
setting = client.get_configuration_setting(key="app:settings:message")
print(f"Read setting: {setting.key}") # Do not log secret-bearing values
ラベルを指定して取得
# Labels allow environment-specific values
setting = client.get_configuration_setting(
key="app:settings:message",
label="production"
)
設定の書き込み
from azure.appconfiguration import ConfigurationSetting
setting = ConfigurationSetting(
key="app:settings:message",
value="Hello, World!",
label="development",
content_type="text/plain",
tags={"environment": "dev"}
)
client.set_configuration_setting(setting)
設定の削除
client.delete_configuration_setting(
key="app:settings:message",
label="development"
)
設定の一覧表示
すべての設定
settings = client.list_configuration_settings()
for setting in settings:
print(f"{setting.key} [{setting.label}]") # Values may be sensitive
キープレフィックスで絞り込む
settings = client.list_configuration_settings(
key_filter="app:settings:*"
)
ラベルで絞り込む
settings = client.list_configuration_settings(
label_filter="production"
)
機能フラグ
機能フラグの設定
from azure.appconfiguration import ConfigurationSetting
import json
feature_flag = ConfigurationSetting(
key=".appconfig.featureflag/beta-feature",
value=json.dumps({
"id": "beta-feature",
"enabled": True,
"conditions": {
"client_filters": []
}
}),
content_type="application/vnd.microsoft.appconfig.ff+json;charset=utf-8"
)
client.set_configuration_setting(feature_flag)
機能フラグの取得
setting = client.get_configuration_setting(
key=".appconfig.featureflag/beta-feature"
)
flag_data = json.loads(setting.value)
print(f"Feature enabled: {flag_data['enabled']}")
機能フラグの一覧表示
flags = client.list_configuration_settings(
key_filter=".appconfig.featureflag/*"
)
for flag in flags:
data = json.loads(flag.value)
print(f"{data['id']}: {'enabled' if data['enabled'] else 'disabled'}")
読み取り専用設定
# Make setting read-only
client.set_read_only(
configuration_setting=setting,
read_only=True
)
# Remove read-only
client.set_read_only(
configuration_setting=setting,
read_only=False
)
スナップショット
スナップショットの作成
from azure.appconfiguration import ConfigurationSnapshot, ConfigurationSettingsFilter
snapshot = ConfigurationSnapshot(
filters=[
ConfigurationSettingsFilter(key="app:*", label="production")
]
)
created = client.begin_create_snapshot(
name="v1-snapshot",
filters=snapshot.filters
).result()
スナップショット設定の一覧表示
settings = client.list_configuration_settings(
snapshot_name="v1-snapshot"
)
非同期クライアント
from azure.appconfiguration.aio import AzureAppConfigurationClient
from azure.identity.aio import DefaultAzureCredential
async def main():
credential = DefaultAzureCredential()
client = AzureAppConfigurationClient(
base_url=endpoint,
credential=credential
)
setting = await client.get_configuration_setting(key="app:message")
# Consume the authorized value without logging it.
await client.close()
await credential.close()
クライアントの操作
| 操作 | 説明 |
|---|---|
get_configuration_setting | 単一の設定を取得 |
set_configuration_setting | 設定を作成または更新 |
delete_configuration_setting | 設定を削除 |
list_configuration_settings | フィルターを使用して一覧表示 |
set_read_only | 設定をロックまたはロック解除 |
begin_create_snapshot | 特定時点のスナップショットを作成 |
list_snapshots | すべてのスナップショットを一覧表示 |
ベストプラクティス
- 環境(dev、staging、prod)を分けるためにラベルを使用する
- 論理的にグループ化するためにキープレフィックスを使用する(app:database:、app:cache:)
- 誤った変更を防ぐために本番環境の設定を読み取り専用にする
- ロールバックできるように、デプロイ前にスナップショットを作成する
- 本番環境では接続文字列の代わりにEntra ID を使用する
- 長時間稼働するアプリケーションでは設定を定期的に更新する
- 段階的なロールアウトや A/B テストには機能フラグを使用する
使用する場面
このスキルは、概要に記載されているワークフローまたはアクションを実行する場合に適用できます。
制限事項
- このスキルは、タスクが上記の説明範囲に明確に合致する場合にのみ使用してください。
- 出力を、環境固有の検証、テスト、または専門家によるレビューの代わりとして扱わないでください。
- 必要な入力、権限、安全上の境界、または成功基準が不足している場合は、作業を中止して確認を求めてください。
コメント
Markdown 対応