{Terraform} RDS/Azure

https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/mssql_server

https://tech-blog.cloud-config.jp/2019-10-18-creting-azure-sql-database-using-terraform
https://learn.microsoft.com/ja-jp/azure/azure-sql/database/single-database-create-terraform-quickstart?view=azuresql&tabs=azure-cli


-- 1. macにTerraformインストール

brew tap hashicorp/tap
brew install hashicorp/tap/terraform
brew update
brew upgrade hashicorp/tap/terraform
terraform -help
terraform -version


-- 2. Azureログイン

az login --use-device-code
az account show

az version

az configure --list-defaults
az configure --defaults location=japaneast
az configure --list-defaults

 

az group list
az upgrade

 


-- 3. tfファイル作成

vim main.tf

terraform{
  required_providers{
    azurerm={
      source  = "hashicorp/azurerm"
      version = "=3.6.0"
    }
  }
}


provider "azurerm" {
  features {}
}


resource "azurerm_resource_group" "rg9999999" {
  name = "rg9999999"
  location = "Japan East"

}


resource "azurerm_mssql_server" "server99999999" {
  name                          = "server99999999"
  resource_group_name           = azurerm_resource_group.rg9999999.name
  location                      = azurerm_resource_group.rg9999999.location
  version                       = "12.0"
  administrator_login           = "azureuser"
  administrator_login_password  = "passwordpassword"
  public_network_access_enabled = true
  
}


resource "azurerm_mssql_firewall_rule" "fw01" {
  name             = "fw01"
  server_id        = azurerm_mssql_server.server99999999.id
  start_ip_address = "192.0.2.1"
  end_ip_address   = "192.0.2.1"
}


resource "azurerm_mssql_database" "db99999999" {
  name                 = "db99999999"
  server_id            = azurerm_mssql_server.server99999999.id
  collation            = "Japanese_BIN2"
  license_type         = "LicenseIncluded"
  sku_name             = "Basic"
  storage_account_type = "Local"

  tags = {
    environment  = "dev"
  }
}


-- 4. terraform 実行

terraform init -upgrade

terraform plan
terraform apply
terraform output

terraform state list


az sql server list \
--resource-group rg9999999


az sql server firewall-rule list \
--resource-group rg9999999 \
--server server99999999


az sql db list \
--resource-group rg9999999 \
--server server99999999

 

sqlcmd -U azureuser -S server99999999.database.windows.net -d db99999999

 

-- 5. Azureクリーンアップ

terraform destroy


az group list