前提条件
学習のため細かく分けて作成していきます。
今回使用するサブネット、ルートテーブルは下記の作業で作成したものです。
AWSにサブネットを作成してみる
AWSにルートテーブルとInternetGatewayを作成してみる
バージョン情報
- Terraform:
Terraform v1.10.5
- AWS CLI:
aws-cli/2.24.11 Python/3.12.9 Windows/11 exe/AMD64
Terraform ファイル
05_routetable_association.tf
以下の Terraform ファイルを作成しました。
provider "aws" {
region = "us-west-2"
}
#publicサブネット用ルートテーブルを参照する
data "aws_route_table" "terraform-public-routetable" {
filter {
name = "tag:Name"
values = ["terraform-public-routetable"]
}
}
#privateサブネット用ルートテーブルを参照する
data "aws_route_table" "terraform-private-routetable" {
filter {
name = "tag:Name"
values = ["terraform-private-routetable"]
}
}
#publicサブネットを参照する
data "aws_subnet" "terraform-public-subnet" {
filter {
name = "tag:Name"
values = ["terraform-public"]
}
}
#privateサブネットを参照する
data "aws_subnet" "terraform-private-subnet" {
filter {
name = "tag:Name"
values = ["terraform-private"]
}
}
#publicサブネットとpublicサブネット用ルートテーブルを関連付ける
resource "aws_route_table_association" "terraform-public-routetable-association" {
subnet_id = data.aws_subnet.terraform-public-subnet.id
route_table_id = data.aws_route_table.terraform-public-routetable.id
}
#privateサブネットとprivateサブネット用ルートテーブルを関連付ける
resource "aws_route_table_association" "terraform-private-routetable-association" {
subnet_id = data.aws_subnet.terraform-private-subnet.id
route_table_id = data.aws_route_table.terraform-private-routetable.id
}
実行コマンド
terraform init
Terraform を初期化します。
PS C:\work\Terraform\01_basic\05-routetable-association> terraform init
出力結果:
Terraform has been successfully initialized!
terraform plan
Terraform の適用計画を確認します。
PS C:\work\Terraform\01_basic\05-routetable-association> 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_route_table_association.terraform-private-routetable-association will be created
+ resource "aws_route_table_association" "terraform-private-routetable-association" {
+ id = (known after apply)
+ route_table_id = "rtb-xxxxxxxxxxxxxxxxx"
+ subnet_id = "subnet-xxxxxxxxxxxxxxxxx"
}
# aws_route_table_association.terraform-public-routetable-association will be created
+ resource "aws_route_table_association" "terraform-public-routetable-association" {
+ id = (known after apply)
+ route_table_id = "rtb-xxxxxxxxxxxxxxxxx"
+ subnet_id = "subnet-xxxxxxxxxxxxxxxxx"
}
Plan: 2 to add, 0 to change, 0 to destroy.
terraform apply
実際に適用します。
PS C:\work\Terraform\01_basic\05-routetable-association> terraform apply -auto-approve
出力結果:
Apply complete! Resources: 2 added, 0 changed, 0 destroyed.
作成したリソースの詳細 (パラメータ一覧)
パラメータ一覧
| パラメータ名 | 説明 | |-------------|------| | `subnet_id` | サブネット ID を指定して関連付けを作成します。`gateway_id` とは併用できません。 | | `gateway_id` | ゲートウェイ ID を指定して関連付けを作成します。`subnet_id` とは併用できません。 | | `route_table_id` | 関連付けを作成するルートテーブルの ID。必須パラメータです。 |エクスポートされる属性
属性名 | 説明 |
---|---|
id |
関連付けの ID。 |
タイムアウト設定
設定項目 | デフォルト値 |
---|---|
create |
5 分 |
update |
2 分 |
delete |
5 分 |
気づいたこと・勘違いしていたこと
- tagの管理、命名規則を統一させる
サブネットのName
タグを間違えてエラーが出ました。
誤:terraform-public-subnet
正:terraform-public
作成環境が増えたとき、リソースが増えたときはタグを分かりやすく管理することが大切だと思いました。
まとめ
- 既存のサブネットとルートテーブルを関連付けました。
次回は セキュリティグループの作成 を行います。