クイック スタート - Azure Container Instances に Azure Cosmos DB をデプロイする
次の Terraform と Terraform プロバイダーのバージョンでテストされた記事:
Terraform を使用すると、クラウド インフラストラクチャの定義、プレビュー、およびデプロイを行うことができます。 Terraform を使用する際は、HCL 構文を使って構成ファイルを作成します。 HCL 構文では、Azure などのクラウド プロバイダーと、クラウド インフラストラクチャを構成する要素を指定できます。 構成ファイルを作成したら、"実行プラン" を作成します。これにより、インフラストラクチャの変更をデプロイ前にプレビューすることができます。 変更を確認したら、実行プランを適用してインフラストラクチャをデプロイします。
この記事では、Terraform を使用して、Azure Container Instances に Azure Cosmos DB をデプロイする方法を示します。
この記事では、次のことについて説明します。
- Azure Cosmos DB インスタンスを作成する
- Azure Container Instance を作成する
- これら 2 つのリソース間で動作するアプリを作成する
Note
この記事のコード例は、Microsoft Terraform GitHub リポジトリにあります。
前提条件
- Azure サブスクリプション:Azure サブスクリプションをお持ちでない場合は、開始する前に無料アカウントを作成してください。
Terraform の構成: まだ行っていない場合は、次のいずれかのオプションを使用して Terraform を構成します。
Terraform コードを実装する
サンプルの Terraform コードをテストするディレクトリを作成し、それを現在のディレクトリにします。
providers.tf
という名前のファイルを作成し、次のコードを挿入します。terraform { required_version = ">=1.0" required_providers { azurerm = { source = "hashicorp/azurerm" version = "~>3.0" } random = { source = "hashicorp/random" version = "~>3.0" } } } provider "azurerm" { features {} }
main.tf
という名前のファイルを作成し、次のコードを挿入します。resource "azurerm_resource_group" "rg" { name = "${random_pet.rg_name.id}-rg" location = var.resource_group_location } resource "azurerm_cosmosdb_account" "vote_cosmos_db" { name = "${random_pet.rg_name.id}-${random_integer.ri.result}" location = azurerm_resource_group.rg.location resource_group_name = azurerm_resource_group.rg.name offer_type = "Standard" kind = "GlobalDocumentDB" consistency_policy { consistency_level = "BoundedStaleness" max_interval_in_seconds = 10 max_staleness_prefix = 200 } geo_location { location = azurerm_resource_group.rg.location failover_priority = 0 } } resource "random_integer" "ri" { min = 10000 max = 99999 } resource "random_pet" "rg_name" { prefix = var.prefix }
aci.tf
という名前のファイルを作成し、次のコードを挿入します。resource "azurerm_container_group" "main" { name = "${random_pet.rg_name.id}-vote-aci" location = azurerm_resource_group.rg.location resource_group_name = azurerm_resource_group.rg.name ip_address_type = "Public" dns_name_label = "vote-aci-${random_integer.ri.result}" os_type = "Linux" container { name = "vote-aci" image = "mcr.microsoft.com/azuredocs/azure-vote-front:cosmosdb" cpu = "0.5" memory = "1.5" ports { port = 80 protocol = "TCP" } secure_environment_variables = { "COSMOS_DB_ENDPOINT" = azurerm_cosmosdb_account.vote_cosmos_db.endpoint "COSMOS_DB_MASTERKEY" = azurerm_cosmosdb_account.vote_cosmos_db.primary_key "TITLE" = "Azure Voting App" "VOTE1VALUE" = "Cats" "VOTE2VALUE" = "Dogs" } } }
variables.tf
という名前のファイルを作成し、次のコードを挿入します。variable "resource_group_location" { default = "eastus" description = "Location of the resource group." } variable "prefix" { type = string default = "cosmos-db-aci" description = "Prefix of the resource name" }
outputs.tf
という名前のファイルを作成し、次のコードを挿入します。output "resource_group_name" { value = azurerm_resource_group.rg.name } output "cosmosdb_account_name" { value = azurerm_cosmosdb_account.vote_cosmos_db.name } output "dns" { value = azurerm_container_group.main.fqdn }
Terraform を初期化する
terraform init を実行して、Terraform のデプロイを初期化します。 このコマンドによって、Azure リソースを管理するために必要な Azure プロバイダーがダウンロードされます。
terraform init -upgrade
重要なポイント:
-upgrade
パラメーターは、必要なプロバイダー プラグインを、構成のバージョン制約に準拠する最新バージョンにアップグレードします。
Terraform 実行プランを作成する
terraform plan を実行して、実行プランを作成します。
terraform plan -out main.tfplan
重要なポイント:
terraform plan
コマンドは、実行プランを作成しますが、実行はしません。 代わりに、構成ファイルに指定された構成を作成するために必要なアクションを決定します。 このパターンを使用すると、実際のリソースに変更を加える前に、実行プランが自分の想定と一致しているかどうかを確認できます。- 省略可能な
-out
パラメーターを使用すると、プランの出力ファイルを指定できます。-out
パラメーターを使用すると、レビューしたプランが適用内容とまったく同じであることが確実になります。
Terraform 実行プランを適用する
terraform apply を実行して、クラウド インフラストラクチャに実行プランを適用します。
terraform apply main.tfplan
重要なポイント:
terraform apply
コマンドの例は、以前にterraform plan -out main.tfplan
が実行されたことを前提としています。-out
パラメーターに別のファイル名を指定した場合は、terraform apply
の呼び出しで同じファイル名を使用します。-out
パラメーターを使用しなかった場合は、パラメーターを指定せずにterraform apply
を呼び出します。
結果を確認する
リソース グループ名を取得します。
echo "$(terraform output resource_group_name)"
Azure Cosmos DB アカウント名を取得します。
echo "$(terraform output cosmosdb_account_name)"
az cosmosdb sql database list/ を実行します。
az cosmosdb sql database list \ --resource-group <resource_group_name> \ --account-name <cosmosdb_account_name>
アプリケーションをテストする
Azure Cosmos DB アカウント名を取得します。
echo "$(terraform output dns)"
前の手順で示した URL を参照します。 次の出力のような結果が表示されます。
リソースをクリーンアップする
Terraform を使用して作成したリソースが不要になった場合は、次の手順を実行します。
terraform plan を実行して、
destroy
フラグを指定します。terraform plan -destroy -out main.destroy.tfplan
重要なポイント:
terraform plan
コマンドは、実行プランを作成しますが、実行はしません。 代わりに、構成ファイルに指定された構成を作成するために必要なアクションを決定します。 このパターンを使用すると、実際のリソースに変更を加える前に、実行プランが自分の想定と一致しているかどうかを確認できます。- 省略可能な
-out
パラメーターを使用すると、プランの出力ファイルを指定できます。-out
パラメーターを使用すると、レビューしたプランが適用内容とまったく同じであることが確実になります。
terraform apply を実行して、実行プランを適用します。
terraform apply main.destroy.tfplan
Azure での Terraform のトラブルシューティング
Azure で Terraform を使用する場合の一般的な問題のトラブルシューティング
次のステップ
フィードバック
https://aka.ms/ContentUserFeedback」を参照してください。
以下は間もなく提供いたします。2024 年を通じて、コンテンツのフィードバック メカニズムとして GitHub の issue を段階的に廃止し、新しいフィードバック システムに置き換えます。 詳細については、「フィードバックの送信と表示