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?

More than 3 years have passed since last update.

Terraformを利用して、Azure SQL serverの設定を実現

Posted at

背景:
お客様はTerraformを利用して、SQL serverに対してaudit logを設定しようとします。
しかし失敗しました。それを究明するため、自分のdev 環境でテストをしました。

テスト:
1.下記のtest.tfファイルを準備します。
============================
data "azurerm_client_config" "current" {}

provider "azurerm" {
features {}
}

terraform {
required_version = ">= 0.12"
}

locals {
primary_sql_server_name = "jcline-sqlsvr-primary"
secondary_sql_server_name = "jcline-sqlsrv-secondary"
}

SQL RG

resource "azurerm_resource_group" "pipeline" {
name = "jcline-cx-repro-pipeline"
location = "
"
}

AE RG

resource "azurerm_resource_group" "AE" {
name = "jcline-cx-repro-log-AE"
location = "eastus"
}

SE RG

resource "azurerm_resource_group" "SE" {
name = "jcline-cx-repro-log-SE"
location = "westus2"
}

SA AE

resource "azurerm_storage_account" "sa_ae" {
name = "jclineaesa01"
resource_group_name = azurerm_resource_group.AE.name

location = azurerm_resource_group.AE.location
account_tier = "Standard"
account_replication_type = "LRS"
}

SA SE

resource "azurerm_storage_account" "sa_se" {
name = "jclineaese01"
resource_group_name = azurerm_resource_group.SE.name

location = azurerm_resource_group.SE.location
account_tier = "Standard"
account_replication_type = "LRS"
}

Primary SQL Server

resource "azurerm_sql_server" "server" {
name = local.primary_sql_server_name
resource_group_name = azurerm_resource_group.pipeline.name
location = azurerm_resource_group.pipeline.location
version = "12.0"
administrator_login = "azureadmin"
administrator_login_password = "R@nd0mP4$$w0rd!"

extended_auditing_policy {
storage_endpoint = azurerm_storage_account.sa_ae.primary_blob_endpoint
storage_account_access_key = azurerm_storage_account.sa_ae.primary_access_key
storage_account_access_key_is_secondary = false
retention_in_days = 7
}
}

Secondary SQL Server

resource "azurerm_sql_server" "server_secondary" {
name = local.secondary_sql_server_name
resource_group_name = azurerm_resource_group.pipeline.name
location = azurerm_resource_group.pipeline.location
version = "12.0"
administrator_login = "azureadmin"
administrator_login_password = "R@nd0mP4$$w0rd!"

extended_auditing_policy {
storage_endpoint = azurerm_storage_account.sa_se.primary_blob_endpoint
storage_account_access_key = azurerm_storage_account.sa_se.primary_access_key
storage_account_access_key_is_secondary = false
retention_in_days = 7
}

}

resource "azurerm_sql_active_directory_administrator" "primary_AD" {
server_name = azurerm_sql_server.server.name
resource_group_name = azurerm_resource_group.pipeline.name
login = "sqladmin"
tenant_id = data.azurerm_client_config.current.tenant_id
object_id = data.azurerm_client_config.current.object_id
}

resource "azurerm_sql_active_directory_administrator" "secondary_AD" {
server_name = azurerm_sql_server.server_secondary.name
resource_group_name = azurerm_resource_group.pipeline.name
login = "sqladmin"
tenant_id = data.azurerm_client_config.current.tenant_id
object_id = data.azurerm_client_config.current.object_id
}

outputs

output "primary_server_id" {
value = azurerm_sql_server.server.id
}
output "primary_server_name" {
value = azurerm_sql_server.server.name
}
output "secondary_server_id" {
value = azurerm_sql_server.server_secondary.id
}
output "secondary_server_name" {
value = azurerm_sql_server.server_secondary.name
}
============================

2.Azure portalのCLIで下記のコマンドを実施して、情報を取得します。
コマンド1:az account show --query "{subscriptionId:id, tenantId:tenantId}"
コマンド2:az ad sp create-for-rbac --role="Contributor" --scopes="/subscriptions/${SUBSCRIPTION_ID}"

3.取得した情報を下記のコマンドで打ちます。
export ARM_SUBSCRIPTION_ID=サブスクリプションID
export ARM_CLIENT_ID=アップId
export ARM_CLIENT_SECRET=パスワード
export ARM_TENANT_ID=テナントID

4.vi test.tf でstep1で準備できたものを入れます。

5.コマンド:terraform init
※Terraform has been successfully initialized! と表示されたら成功。

6.コマンド:terraform apply -auto-approve

以上の流れで初めてterraformをやってみました。Azure portalで確認したら、確かにresoruce gourp, storage account ,SQL server は全部作成されて、また、Auditlogも設定されました。
なんという便利さ!関心しました。

しかし、お客様はFirewall/vnet ruleを設定したstorage accountの条件なので、まだお客様の問題を再現しております。その設定の分を
TFファイルに組み込まなければならないですね。また別の話でやります。

以上、簡単にterraformの使い方をさらしてみました。

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?