このクイック スタートでは、Terraform を使用して、Azure Functions の Flex Consumption プランで、他の必要な Azure リソースと共に関数アプリを作成します。 Flex Consumption プランは、インフラストラクチャを明示的にプロビジョニングまたは管理することなく、必要に応じてコードを実行できるサーバーレス ホスティングを提供します。 これは、データの処理、システムの統合、モノのインターネット コンピューティング、単純な API とマイクロサービスの構築に使用されます。 この構成で作成されるリソースには、一意のリソース グループ、ストレージ アカウント、BLOB ストレージ コンテナー、Flex 従量課金プラン、関数アプリ自体が含まれます。 関数アプリは Linux 上で実行され、コードのデプロイに BLOB ストレージを使用するように構成されています。
Terraformはクラウドインフラストラクチャの定義、プレビュー、そしてデプロイメントを可能にします。 Terraform を使用する際は、HCL 構文を使って構成ファイルを作成します。 HCL 構文を使用すると、クラウド プロバイダー (Azure など) とクラウド インフラストラクチャを構成する要素を指定できます。 あなたの設定ファイルを作成した後、実行計画を作成します。これにより、インフラストラクチャの変更をデプロイする前にプレビューすることができます。 変更を確認したら、実行プランを適用してインフラストラクチャをデプロイします。
- 一意の名前を持つ Azure リソース グループを作成します。
- リソースに名前を付けるために、13 文字の小文字のランダムな文字列を生成します。
- Azure にストレージ アカウントを作成します。
- ストレージ アカウント内に Blob Storage コンテナーを作成します。
- Azure Functions で Flex 従量課金プランを作成します。
- Azure で Flex Consumption プランを使用して関数アプリを作成します。
- リソース グループ、ストレージ アカウント、サービス プラン、関数アプリ、Azure Functions Flex Consumption プランの名前を出力します。
[前提条件]
- アクティブなサブスクリプションがある Azure アカウントを作成します。 無料でアカウントを作成できます。
- Terraform をインストールおよび構成します。
Terraform コードを実装する
この記事のサンプル コードは、 Azure Terraform GitHub リポジトリにあります。 Terraform の現在および以前のバージョンからのテスト結果を含むログ ファイルを表示できます。 Terraform を使用して Azure リソースを管理する方法を示すその他の記事とサンプル コードを参照してください。
サンプル Terraform コードをテストして実行するディレクトリを作成し、それを現在のディレクトリにします。
main.tf
という名前のファイルを作成し、次のコードを挿入します。# This Terraform configuration creates a Flex Consumption plan app in Azure Functions # with the required Storage account and Blob Storage deployment container. # Create a random pet to generate a unique resource group name resource "random_pet" "rg_name" { prefix = var.resource_group_name_prefix } # Create a resource group resource "azurerm_resource_group" "example" { location = var.resource_group_location name = random_pet.rg_name.id } # Random String for unique naming of resources resource "random_string" "name" { length = 8 special = false upper = false lower = true numeric = false } # Create a storage account resource "azurerm_storage_account" "example" { name = coalesce(var.sa_name, random_string.name.result) resource_group_name = azurerm_resource_group.example.name location = azurerm_resource_group.example.location account_tier = var.sa_account_tier account_replication_type = var.sa_account_replication_type } # Create a storage container resource "azurerm_storage_container" "example" { name = "example-flexcontainer" storage_account_id = azurerm_storage_account.example.id container_access_type = "private" } # Create a Log Analytics workspace for Application Insights resource "azurerm_log_analytics_workspace" "example" { name = coalesce(var.ws_name, random_string.name.result) location = azurerm_resource_group.example.location resource_group_name = azurerm_resource_group.example.name sku = "PerGB2018" retention_in_days = 30 } # Create an Application Insights instance for monitoring resource "azurerm_application_insights" "example" { name = coalesce(var.ai_name, random_string.name.result) location = azurerm_resource_group.example.location resource_group_name = azurerm_resource_group.example.name application_type = "web" workspace_id = azurerm_log_analytics_workspace.example.id } # Create a service plan resource "azurerm_service_plan" "example" { name = coalesce(var.asp_name, random_string.name.result) resource_group_name = azurerm_resource_group.example.name location = azurerm_resource_group.example.location sku_name = "FC1" os_type = "Linux" } # Create a function app resource "azurerm_function_app_flex_consumption" "example" { name = coalesce(var.fa_name, random_string.name.result) resource_group_name = azurerm_resource_group.example.name location = azurerm_resource_group.example.location service_plan_id = azurerm_service_plan.example.id storage_container_type = "blobContainer" storage_container_endpoint = "${azurerm_storage_account.example.primary_blob_endpoint}${azurerm_storage_container.example.name}" storage_authentication_type = "StorageAccountConnectionString" storage_access_key = azurerm_storage_account.example.primary_access_key runtime_name = var.runtime_name runtime_version = var.runtime_version maximum_instance_count = 50 instance_memory_in_mb = 2048 site_config { } }
outputs.tf
という名前のファイルを作成し、次のコードを挿入します。output "resource_group_name" { value = azurerm_resource_group.example.name } output "sa_name" { value = azurerm_storage_account.example.name } output "asp_name" { value = azurerm_service_plan.example.name } output "fa_name" { value = azurerm_function_app_flex_consumption.example.name } output "fa_url" { value = "https://${azurerm_function_app_flex_consumption.example.name}.azurewebsites.net" }
providers.tf
という名前のファイルを作成し、次のコードを挿入します。terraform { required_version = ">=1.0" required_providers { azurerm = { source = "hashicorp/azurerm" version = "~>4.0" } random = { source = "hashicorp/random" version = "~>3.0" } } } provider "azurerm" { features {} }
variables.tf
という名前のファイルを作成し、次のコードを挿入します。variable "resource_group_name" { type = string default = "" description = "The name of the Azure resource group. If blank, a random name will be generated." } variable "resource_group_name_prefix" { type = string default = "rg" description = "Prefix of the resource group name that's combined with a random ID so name is unique in your Azure subscription." } variable "resource_group_location" { type = string default = "eastus" description = "Location of the resource group." } variable "sa_account_tier" { description = "The tier of the storage account. Possible values are Standard and Premium." type = string default = "Standard" } variable "sa_account_replication_type" { description = "The replication type of the storage account. Possible values are LRS, GRS, RAGRS, and ZRS." type = string default = "LRS" } variable "sa_name" { description = "The name of the storage account. If blank, a random name will be generated." type = string default = "" } variable "ws_name" { description = "The name of the Log Analytics workspace. If blank, a random name will be generated." type = string default = "" } variable "ai_name" { description = "The name of the Application Insights instance. If blank, a random name will be generated." type = string default = "" } variable "asp_name" { description = "The name of the App Service Plan. If blank, a random name will be generated." type = string default = "" } variable "fa_name" { description = "The name of the Function App. If blank, a random name will be generated." type = string default = "" } variable "runtime_name" { description = "The name of the language worker runtime." type = string default = "node" # Allowed: dotnet-isolated, java, node, powershell, python } variable "runtime_version" { description = "The version of the language worker runtime." type = string default = "20" # Supported versions: see https://aka.ms/flexfxversions }
重要
4.x azurerm プロバイダーを使用している場合は、Terraform コマンドを実行する前に、Azure に対して認証する Azure サブスクリプション ID を明示的に指定 する必要があります。
azure サブスクリプション ID を providers
ブロックに入れずに指定する 1 つの方法は、 ARM_SUBSCRIPTION_ID
という名前の環境変数にサブスクリプション ID を指定することです。
詳細については、 Azure プロバイダーのリファレンス ドキュメントを参照してください。
Terraform を初期化する
terraform init
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
を呼び出します。
結果を確認してください。
Azure リソース グループ名を取得します。
resource_group_name=$(terraform output -raw resource_group_name)
ストレージ アカウント名を取得します。
sa_name=$(terraform output -raw sa_name)
サービス プラン名を取得します。
asp_name=$(terraform output -raw asp_name)
関数アプリのプラン名を取得します。
fa_name=$(terraform output -raw fa_name)
az functionapp show
を実行して、Azure Functions Flex 従量課金プランを表示します。az functionapp show --name $function_app_name --resource-group $resource_group_name
ブラウザーを開き、次の URL を入力します: https://<fa_name>.azurewebsites.net。 プレースホルダー <fa_name>
を Terraform の出力値に置き換えてください。
リソースをクリーンアップする
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 を使用する場合の一般的な問題のトラブルシューティング。
次のステップ
Azure で作成した関数アプリ リソースにコード プロジェクトをデプロイできるようになりました。
次のローカル環境から、新しい関数アプリにコード プロジェクトを作成、検証、デプロイできます。