はじめに
こんにちは、ACS事業部の安藤です。
関東圏でも雪が降りしきる中、個人的にホットな話題が上がってきました。
TerraformのAzureRM Providerのv.3.43.0で、ついに念願のAzure Container Appsがサポートされました!
Terraform Registryのドキュメントにもすでに追加されており、リソースとしては以下の5つが利用可能になっています。
- azurerm_container_app
- azurerm_container_app_environment
- azurerm_container_app_environment_certificate
- azurerm_container_app_environment_dapr_component
- azurerm_container_app_environment_storage
早速これらを使ってTerraformでAzure Container Appsをデプロイしてみようと思います。
実践
適当な作業ディレクトリを作ってmain.tf
にコードを書きます。
main.tf
provider "azurerm" { features {} } resource "azurerm_resource_group" "example" { name = "example-resources" location = "Japan East" } resource "azurerm_log_analytics_workspace" "example" { name = "acctest-01" location = azurerm_resource_group.example.location resource_group_name = azurerm_resource_group.example.name sku = "PerGB2018" retention_in_days = 30 } resource "azurerm_container_app_environment" "example" { name = "Example-Environment" location = azurerm_resource_group.example.location resource_group_name = azurerm_resource_group.example.name log_analytics_workspace_id = azurerm_log_analytics_workspace.example.id } resource "azurerm_container_app" "example" { name = "example-app" container_app_environment_id = azurerm_container_app_environment.example.id resource_group_name = azurerm_resource_group.example.name revision_mode = "Single" template { container { name = "examplecontainerapp" image = "mcr.microsoft.com/azuredocs/containerapps-helloworld:latest" cpu = 0.25 memory = "0.5Gi" } } ingress { target_port = 80 external_enabled = true traffic_weight { latest_revision = true percentage = 100 } } } output "fqdn" { value = "https://${azurerm_container_app.example.latest_revision_fqdn}" }
azurerm_container_appのページにあるサンプルにIngressを追加した程度の簡単なコードになります。
ポイントとしてはazurerm_container_app_environment
リソースにlog_analytics_workspace_id
パラメータが必須になっているので、
一緒にazurerm_log_analytics_workspace
を作ってIDを参照させています。
またazurerm_container_app
はシングルリビジョンモードであってもingress.traffic_weight
の設定が必須となっており、
そのブロック内ではlatest_revision = true
としておくか、revision_suffix
のパラメータでリビジョンIDをハードコーディングしないと、
Applyの度に毎回以下のような差分が検出されて変更が走ってしまいます。 (Ingressが置き換わるわけではないのでURL等は変更されないようです)
Terraform will perform the following actions: # azurerm_container_app.example will be updated in-place ~ resource "azurerm_container_app" "example" { id = "/subscriptions/************************************/resourceGroups/example-resources/providers/Microsoft.App/containerApps/example-app" name = "example-app" tags = {} # (8 unchanged attributes hidden) ~ ingress { # (5 unchanged attributes hidden) ~ traffic_weight { ~ percentage = 0 -> 100 # (1 unchanged attribute hidden) } - traffic_weight { - latest_revision = false -> null - percentage = 100 -> null - revision_suffix = "abcdefg" -> null } } # (1 unchanged block hidden) } # (1 unchanged block hidden) } Plan: 0 to add, 1 to change, 0 to destroy.
早速デプロイしてみます。
$ terraform init Initializing the backend... Initializing provider plugins... - Finding latest version of hashicorp/azurerm... - Installing hashicorp/azurerm v3.43.0... - Installed hashicorp/azurerm v3.43.0 (signed by HashiCorp) ...
azurermのバージョンがv.3.43.0
以上であることを確認できたらApplyしてみましょう。
$ terraform apply -auto-approve ... Apply complete! Resources: 0 added, 0 changed, 0 destroyed. Outputs: fqdn = "https://example-app--******.******-******.japaneast.azurecontainerapps.io"
Environmentの作成に4分、アプリの作成に1分ぐらいのトータル5分程度でContainer Appsがデプロイされました。
OutputにアプリのHTTPS URLが表示されるようにしてあるのでそちらにアクセスしてみましょう。
以下のページが表示されたら無事アプリがデプロイされていることが確認できます。
おわりに
2ヶ月前の吉川さんの爆速デプロイの記事に比べるとコード量やコマンド数が多かったですが、
今回追加したingressブロックの他にもdaprやsecret, registryブロックもありますし、
今回使わなかったazurerm_container_app_environment_dapr_component
リソースなどもありますので、これらを組み合わせればかなりの拡張性が期待できますね!
リリースしたばかりのリソースなので何かしらのアップデートや変更があるかもしれないので、
今後も追いかけてご紹介したいと思います。