アキタイムズ

イベント参加レポと小ネタを中心に投稿しています。

AWS CDKユーザーが初めてのTerraformで、VPCを作成するまでを試す

普段はAWS CDKユーザーなのですが、今回は初めてTerraformを触ってみました。
全くの初学者の為、その点はご容赦ください。

セットアップ手順

普段はWindowsを開発環境として利用している為、chocolateyを使ってインストールします。

choco install terraform
terraform -v 
Terraform v1.4.6

備考

  • 既にawsのクレデンシャルは設定済の状態です。

構築

こちらのハンズオンを参考に構築しました。

今回はterraform-sampleディレクトリを作成し、まずvpc.tfを用意。
記述は以下です。

resource "aws_vpc" "test_create" {
  cidr_block           = "17.0.0.0/24"
}

VSCode拡張機能HashiCorp Terraformを入れると、vpc.tfファイル内の記述の視認性が上がったので、入れておくと便利そう。

terraform init

terraform initにより、構成ファイルの作業ディレクトリが初期化される。

PS C:\Users\yoyoyo-pg\git\public-repo\terraform-sample> terraform init

Initializing the backend...

Initializing provider plugins...
- Finding latest version of hashicorp/aws...
- Installing hashicorp/aws v5.1.0...
- Installed hashicorp/aws v5.1.0 (signed by HashiCorp)

Terraform has created a lock file .terraform.lock.hcl to record the provider
selections it made above. Include this file in your version control repository
so that Terraform can guarantee to make the same selections by default when
you run "terraform init" in the future.

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.

実施後、.terraform.terraform.lock.hclが自動生成。

terraform plan

terraform planを実行して、インフラに必要な変更を確認してみてください。

とあるので、terraform planを実行。

PS C:\Users\yoyoyo-pg\git\public-repo\terraform-sample> terraform init

Initializing the backend...

Initializing provider plugins...
- Finding latest version of hashicorp/aws...
- Installing hashicorp/aws v5.1.0...
- Installed hashicorp/aws v5.1.0 (signed by HashiCorp)

Terraform has created a lock file .terraform.lock.hcl to record the provider
selections it made above. Include this file in your version control repository
so that Terraform can guarantee to make the same selections by default when
you run "terraform init" in the future.

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,
  + create

Terraform will perform the following actions:

  # aws_vpc.test_create will be created
  + resource "aws_vpc" "test_create" {
      + arn                                  = (known after apply)
      + cidr_block                           = "17.0.0.0/24"
      + default_network_acl_id               = (known after apply)
      + default_route_table_id               = (known after apply)
      + default_security_group_id            = (known after apply)
      + dhcp_options_id                      = (known after apply)
      + enable_dns_hostnames                 = (known after apply)
      + enable_dns_support                   = true
      + enable_network_address_usage_metrics = (known after apply)
      + id                                   = (known after apply)
      + instance_tenancy                     = "default"
      + ipv6_association_id                  = (known after apply)
      + ipv6_cidr_block                      = (known after apply)
      + ipv6_cidr_block_network_border_group = (known after apply)
      + main_route_table_id                  = (known after apply)
      + owner_id                             = (known after apply)
      + tags_all                             = (known after apply)
    }

Plan: 1 to add, 0 to change, 0 to destroy.

────────────────────────────────────────────────────────────────────────────────────────────────── 

Note: You didn't use the -out option to save this plan, so Terraform can't guarantee to take       
exactly these actions if you run "terraform apply" now.

teffaform planの結果を任意のファイル出力できるらしい。 developer.hashicorp.com

terraform apply

確認後、早速terraform apply

S C:\Users\yoyoyo-pg\git\public-repo\terraform-sample> terraform apply

Terraform used the selected providers to generate the following execution plan. Resource actions   
are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # aws_vpc.test_create will be created
  + resource "aws_vpc" "test_create" {
      + arn                                  = (known after apply)
      + cidr_block                           = "17.0.0.0/24"
      + default_network_acl_id               = (known after apply)
      + default_route_table_id               = (known after apply)
      + default_security_group_id            = (known after apply)
      + dhcp_options_id                      = (known after apply)
      + enable_dns_hostnames                 = (known after apply)
      + enable_dns_support                   = true
      + enable_network_address_usage_metrics = (known after apply)
      + id                                   = (known after apply)
      + instance_tenancy                     = "default"
      + ipv6_association_id                  = (known after apply)
      + ipv6_cidr_block                      = (known after apply)
      + ipv6_cidr_block_network_border_group = (known after apply)
      + main_route_table_id                  = (known after apply)
      + owner_id                             = (known after apply)
      + tags_all                             = (known after apply)
    }

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を入力しEnter

  Enter a value: yes

ws_vpc.test_create: Creating...
aws_vpc.test_create: Creation complete after 1s [id=vpc-06206dacf6a2fe634]

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

実施後確認

AWSマネジメントコンソール上から見てみると、無事VPCが完成していました。

感想

  • AWS CDKだとインストールする物が多かったが、それに比べるとTerraformは環境構築がすぐ完了し、初学者にとってはかなり取っつきやすく非常に便利だと感じました。

    • 特に、ディレクトリを用意しtfファイル1つあればデプロイ出来る点には驚きました。
    • AWS CDKの事前知識があるお陰で、teffaform plan=cdk synthterraform apply=cdk deployといった形で(厳密には色々違う部分はあるでしょうが)、CDKの用語で補完しながら理解できた点は良かったです。
  • 自分は1つ目のIaCツールとして(CloudFormationを殆ど触った事が無い状態で)AWS CDKを使っていたので、「CloudFormation」「Typescript」「NodeJS」「CDKの概念」等々、最初は覚えることが多くてキャッチアップに少し時間がかかりました。

    • 取り組みやすさの観点からも、1つ目のIaCツールとしてTerraformを使ってみるのは良い選択肢なのでは、と感じました。

参考文献

https://qiita.com/souhei-etou/items/7876767f041543321006