TerraformでAzureをいじってみました。

TerraformでAzureをいじってみたい、ということで超簡単なところから触ってみています。

  • Download Terraform – Terraform by HashiCorp よりバイナリダウンロード。とりあえずWindows版の64bitのバイナリをダウンロードしました。
  • パスの設定。exeファイル1つだけなので、どこからでも使えるようにDropbox上に格納してそこにPATHを通しました。
    image
  • 環境を整える際のサンプルスクリプトがAzure CLIなのでAzure CLIの環境を整えます。
  • Install the Azure CLI for Windows | Microsoft Docs よりMSIでAzure CLIをインストール
  • Visual Studio Code上で色々やろうと思うので、Azure CLI系やTerraform系の拡張を言われるがままに入れる。
    • Azure CLI Tools
    • Azure Resource Manager Tools
    • Azure Terraform
    • Terraform
  • 単純にリソースグループを作成するだけのtest.tfで試してみる

 

provider “azurerm” {

}

resource “azurerm_resource_group” “rg” {

name = “testResourceGroup”

location = “westus”

}

テキストファイルを用意しておいて

terraform init, plan, applyコマンドをそれぞれ投入するだけできちんと意図した状態になることを確認できました。素敵ですね。コンフィグファイルが読みやすくわかりやすいのが素敵です。そして、Visual Studio Codeがずいぶん素敵ですね、言われるがままに操作するだけで勝手に環境が整ってしまいました。

 

mebisuda@Azure:~/clouddrive/AzureManagement/Terraform/Demos$ az group list
[]
mebisuda@Azure:~/clouddrive/AzureManagement/Terraform/Demos$ terraform init

Initializing provider plugins…

The following providers do not have any version constraints in configuration,
so the latest version was installed.

To prevent automatic upgrades to new major versions that may contain breaking
changes, it is recommended to add version = “…” constraints to the
corresponding provider blocks in configuration, with the constraint strings
suggested below.

* provider.azurerm: version = “~> 1.5”

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running “terraform plan” to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.
mebisuda@Azure:~/clouddrive/AzureManagement/Terraform/Demos$ terraform plan
Refreshing Terraform state in-memory prior to plan…
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.
————————————————————————

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
+ create

Terraform will perform the following actions:

+ azurerm_resource_group.rg
id: <computed>
location: “westus”
name: “testResourceGroup”
tags.%: <computed>
Plan: 1 to add, 0 to change, 0 to destroy.

————————————————————————

Note: You didn’t specify an “-out” parameter to save this plan, so Terraform
can’t guarantee that exactly these actions will be performed if
“terraform apply” is subsequently run.
mebisuda@Azure:~/clouddrive/AzureManagement/Terraform/Demos$ terraform apply

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
+ create

Terraform will perform the following actions:

+ azurerm_resource_group.rg
id: <computed>
location: “westus”
name: “testResourceGroup”
tags.%: <computed>
Plan: 1 to add, 0 to change, 0 to destroy.

Do you want to perform these actions?
Terraform will perform the actions described above.
Only ‘yes’ will be accepted to approve.

Enter a value: yes

azurerm_resource_group.rg: Creating…
location: “” => “westus”
name: “” => “testResourceGroup”
tags.%: “” => “<computed>”
azurerm_resource_group.rg: Creation complete after 0s (ID: /subscriptions/3b99fb3a-a079-45cb-a557-…48f7d/resourceGroups/testResourceGroup)

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
mebisuda@Azure:~/clouddrive/AzureManagement/Terraform/Demos$ az group list
[
{
“id”: “/subscriptions/3b99fb3a-a079-45cb-a557-73faca548f7d/resourceGroups/testResourceGroup”,
“location”: “westus”,
“managedBy”: null,
“name”: “testResourceGroup”,
“properties”: {
“provisioningState”: “Succeeded”
},
“tags”: {}
}
]

次はもうちょっと複雑なものに手を出していきたいとおもいます。

子供3人。家族優先。都内SIer勤務。Windows系中心のインフラよりの何でも屋。脱原発。 Microsoft MVP for Cloud and Datacenter Management.

コメントを残す

メールアドレスが公開されることはありません。