知り合いが、構築をIaCで行っているそうで、関連知識を深めようと思います。
前回からの続きで、具体的なコーディングだと、例えば以下のようなファイルを作成します。
1) ディレクトリ構成
main.tf
variables.tf
outputs.tf
versions.tf
以下のファイルは、使用するモジュール等のバージョンを記載します。不一致だとエラーになる場合があります。
2) versions.tf
terraform {
required_version = “>= 1.5.0”
required_providers {
aws = {
source = “hashicorp/aws”
version = “~> 5.0”
}
}
}
provider “aws” {
region = var.aws_region
}
以下のファイルは、変数定義を記載します。
3) variables.tf
variable “aws_region” {
type = string
default = “ap-northeast-1”
}
variable “name” {
type = string
default = “demo”
}
variable “vpc_cidr” {
type = string
default = “10.0.0.0/16”
}
variable “azs” {
type = list(string)
default = [“ap-northeast-1a”, “ap-northeast-1c”]
}
variable “public_subnet_cidrs” {
type = list(string)
default = [“10.0.0.0/24”, “10.0.1.0/24”]
}
variable “private_subnet_cidrs” {
type = list(string)
default = [“10.0.100.0/24”, “10.0.101.0/24”]
}