3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Terraformで学ぶIaC ~入門編~

3
Posted at

はじめに

IaCについて知識が全く無いので、手を動かしながら学んでいきます。
入門編かつ学習記録となるのであしからず。

IaCとは?

サーバー・ネットワーク・ストレージ等のインフラ構成をコードで定義する手法。
コードで定義することによって、迅速な環境構築や再利用が可能となる。
主に以下の種類がある。

  • Terraform
  • CloudFormation
  • AWS CDK

今回は、複数のクラウドサービスプロバイダーに対応で汎用性があるTerraformを学びます。

S3バケットを作ってみる

ひとまずインストール

# パッケージ管理にmiseを使ってます
mise install terraform

Terraformは、「.tf」形式のファイルで記述するようなので作ります

touch main.tf

少し中身に触れます。
"aws_s3_bucket"は、リソースタイプで、S3を指します。
"sample"は、リソース名で、Terraform内部でリソースを指定するときに使用する任意の名前です。
aws_s3_bucket.sampleで参照可能です。

main.tf
provider "aws" {
  region = "ap-northeast-1"
}

resource "aws_s3_bucket" "sample" {
  # バケット名(任意)
  bucket = "terraform-sample-bucket-0123"
}

main.tfの作成が完了したら、実際に動かしてみます

# 初期化
terraform init

# 実行計画の確認
terraform plan

terraform plan を実行すると、以下がコンソールに出てきました。
何がどういう構成で構築されるのか事前に確認できるようです。
今回は、リージョンとバケット名のみの設定です。

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_s3_bucket.sample will be created
  + resource "aws_s3_bucket" "sample" {
      + acceleration_status         = (known after apply)
      + acl                         = (known after apply)
      + arn                         = (known after apply)
      + bucket                      = "terraform-sample-bucket-0123"
      + bucket_domain_name          = (known after apply)
      + bucket_namespace            = (known after apply)
      + bucket_prefix               = (known after apply)
      + bucket_region               = (known after apply)
      + bucket_regional_domain_name = (known after apply)
      + force_destroy               = false
      + hosted_zone_id              = (known after apply)
      + id                          = (known after apply)
      + object_lock_enabled         = (known after apply)
      + policy                      = (known after apply)
      + region                      = "ap-northeast-1"
      + request_payer               = (known after apply)
      + tags_all                    = (known after apply)
      + website_domain              = (known after apply)
      + website_endpoint            = (known after apply)

      + cors_rule (known after apply)

      + grant (known after apply)

      + lifecycle_rule (known after apply)

      + logging (known after apply)

      + object_lock_configuration (known after apply)

      + replication_configuration (known after apply)

      + server_side_encryption_configuration (known after apply)

      + versioning (known after apply)

      + website (known after apply)
    }

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

実行計画が問題無ければ、実行します。

terraform apply

マネジメントコンソールで確認すると、無事S3バケットが作成されていました。

image.png

コンソールでも確認可能です。

terraform show

先程の構成を更新してみます。
試しにタグを付けてみます。

main.tf
provider "aws" {
  region = "ap-northeast-1"
}

resource "aws_s3_bucket" "sample" {
  bucket = "terraform-sample-bucket-0123"

  # タグを追加
  tags = {
    Name = "terraform-sample"
    Environment = "dev"
  }
}

もう一度、実行計画を取得します。

terraform plan

実行計画を確認すると、タグ追加されることが分かります。
update in-place とは、リソースを削除せずに更新する意味です。
リソースタイプによっては、一度リソースを削除して再作成することもあるので、注意して確認しましょう。

aws_s3_bucket.sample: Refreshing state... [id=terraform-sample-bucket-0123]

Terraform used the selected providers to generate the following execution plan. Resource
actions are indicated with the following symbols:
  ~ update in-place

Terraform will perform the following actions:

  # aws_s3_bucket.sample will be updated in-place
  ~ resource "aws_s3_bucket" "sample" {
        id                          = "terraform-sample-bucket-0123"
      ~ tags                        = {
          + "Environment" = "dev"
          + "Name"        = "terraform-sample"
        }
      ~ tags_all                    = {
          + "Environment" = "dev"
          + "Name"        = "terraform-sample"
        }
        # (14 unchanged attributes hidden)

        # (3 unchanged blocks hidden)
    }

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

実行します。

terraform apply

無事タグ追加できました。
image.png

最後に削除します。

terraform destroy

最後に

S3を作るだけなので単純でしたが、Terraformの導入としてはためになりました。
今まで個人の開発でAWSを使用する際は、Amplifyを使ってサーバーレスで実装していましたが、今後ネットワーク周りも学び、EC2を自走で立ち上げられるように学んでいきます。
学ぶ道中で引き続き記事を上げていきます。

3
1
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
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?