0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

AzureAlertをterraformでサクッと作りたい

Last updated at Posted at 2024-11-12

はじめに

Azure Alertを手作りでひとつずつ作ったら頭おかしくなりそうだったので
terraformでさくっと作りたくなったのでメモ残す

構成

  • VMを1台用意 ← これはアラートを作るために用意したもの
  • VMのCPUアラートを1つ作る

参考にした仕様

terraformの公式を参考にしました

(Alertを作成するために用意したVMの定義)
https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/virtual_machine.html

アクショングループの定義
https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/monitor_action_group

メトリックアラートの定義
https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/monitor_metric_alert

作ったコード

providers.tf
# providers.tf
terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "~> 3.0"
    }
  }
}
main.tf
provider "azurerm" {
  features {}
}

variable "prefix" {
  default = "tfvmex"
}

resource "azurerm_resource_group" "example" {
  name     = "${var.prefix}-resources"
  location = "West Europe"
}

resource "azurerm_virtual_network" "main" {
  name                = "${var.prefix}-network"
  address_space       = ["10.0.0.0/16"]
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name
}

resource "azurerm_subnet" "internal" {
  name                 = "internal"
  resource_group_name  = azurerm_resource_group.example.name
  virtual_network_name = azurerm_virtual_network.main.name
  address_prefixes     = ["10.0.2.0/24"]
}

resource "azurerm_network_interface" "main" {
  name                = "${var.prefix}-nic"
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name

  ip_configuration {
    name                          = "testconfiguration1"
    subnet_id                     = azurerm_subnet.internal.id
    private_ip_address_allocation = "Dynamic"
  }
}

resource "azurerm_virtual_machine" "main" {
  name                  = "${var.prefix}-vm"
  location              = azurerm_resource_group.example.location
  resource_group_name   = azurerm_resource_group.example.name
  network_interface_ids = [azurerm_network_interface.main.id]
  vm_size               = "Standard_DS1_v2"

  # Uncomment this line to delete the OS disk automatically when deleting the VM
  # delete_os_disk_on_termination = true

  # Uncomment this line to delete the data disks automatically when deleting the VM
  # delete_data_disks_on_termination = true

  storage_image_reference {
    publisher = "Canonical"
    offer     = "0001-com-ubuntu-server-jammy"
    sku       = "22_04-lts"
    version   = "latest"
  }
  storage_os_disk {
    name              = "myosdisk1"
    caching           = "ReadWrite"
    create_option     = "FromImage"
    managed_disk_type = "Standard_LRS"
  }
  os_profile {
    computer_name  = "hostname"
    admin_username = "testadmin"
    admin_password = "Password1234!"
  }
  os_profile_linux_config {
    disable_password_authentication = false
  }
  tags = {
    environment = "staging"
  }
}

### ↓↓ここからがAzure Alert↓↓
resource "azurerm_monitor_action_group" "main" {
  name                = "example-actiongroup"
  resource_group_name = azurerm_resource_group.example.name
  short_name          = "exampleact"

  email_receiver {
    name          = "sendtoadmin"
    email_address = "hogehoge@gmail.com"
  }
}

resource "azurerm_monitor_metric_alert" "cpu_alert" {
  name                     = "cpu_alert"
  resource_group_name      = azurerm_virtual_machine.main.resource_group_name
  scopes                   = [azurerm_virtual_machine.main.id]

  criteria {
    metric_namespace  = "Microsoft.Compute/virtualMachines"
    metric_name      = "Percentage CPU"
    aggregation = "Average"
    threshold         = 80
    operator         = "GreaterThan"
  }

  action {
    action_group_id = azurerm_monitor_action_group.main.id
  }
}

実行方法

az cliでログインしてからterraformを動かすのがポイントです

az login
terraform init
terraform plan
terraform apply

実行結果

上記のコマンドは、ローカルPCよりVSCode上のターミナルから実行しました。
実行は数分で完了、確認のためポータルへ行ってみたらちゃんとできてました
image.png

おわりに

今後Azure Alertを用意するときはこれを使ったらよさそうでよかった。
次は、アラートの追加・変更・削除をやってみて検証してみよう~と思いました

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?