Upgrade to Pro — share decks privately, control downloads, hide ads and more …

terraform_module_ Beginner

Yona
January 21, 2024
71k

terraform_module_ Beginner

Yona

January 21, 2024
Tweet

Transcript

  1. root main.tf variables.tf EC2 VPC IAM CIDRブロック AMI ID タグ名

    etc... リソース定義 変数定義 分離
  2. network.tf ec2.tf resource "aws_instance" "ec2" { subnet_id = } aws_subnet.public_subnet.id

    resource "aws_vpc" "vpc" { cidr_block = var.vpc_cidr } resource " " " " { ... } aws_subnet public_subnet 参照 root network.tf ec2.tf リソース参照
  3. root main.tf variables.tf network module network.tf ec2 ... variables.tf outputs.tf

    どこで変数定義すればいい? 何書けばいい? 出力した値はどうやって使う?
  4. main.tf module "network" { az = var.az vpc_cidr = var.vpc_cidr

    public_cidr = var.public_cidr } module "ec2" { public_id = module.network.public_id az = var.az } source = "./module/network" source = "./module/ec2" network module network.tf ec2.tf ec2 variables.tf variables.tf outputs.tf outputs.tf
  5. ÇÄ 変数受け取り root main.tf variables.tf network module network.tf variables.tf module

    "network" { source = "./module/network” = } vpc_cidr var.vpc_cidr main.tf variables.tf variable " " { description = "The CIDR block for the VPC" type = string default = "10.0.0.0/16" } vpc_cidr variables.tf variable " " {} vpc_cidr ƒÄ 変数定義 ŒÄ 変数代入
  6. root main.tf variables.tf network module network.tf variables.tf resource "aws_vpc" "vpc"

    { cidr_block = } var.vpc_cidr network.tf variables.tf variable " " {} vpc_cidr 参照
  7. root network.tf ec2.tf ec2.tf リソース参照 リソース参照 network.tf network module ec2

    階層が違うので参照できない 同じ階層なので参照できる
  8. main.tf module "network" { source = "./module/network" az = var.az

    vpc_cidr = var.vpc_cidr public_cidr = var.public_cidr } module "ec2" { source = "./module/ec2" public_id = az = var.az } module.network.public_id network module network.tf ec2.tf ec2 variables.tf variables.tf outputs.tf outputs.tf
  9. outputs.tf output "public_id" { value = } aws_subnet.public.id resource "aws_subnet"

    " " { ... } public ec2.tf network.tf network.tf network module ec2 outputs.tf アウトプット
  10. outputs.tf output "public_id" { value = } aws_subnet.public.id module "ec2"

    { source = "./module/ec2" = } public_id module.network.public_id main.tf ec2.tf network module ec2 outputs.tf main.tf variables.tf variable " " {} public_id variables.tf
  11. variable " " {} public_id resource "aws_instance" "ec2" { subnet_id

    = } var.public_id ec2.tf ec2.tf network module ec2 outputs.tf main.tf variables.tf variables.tf 参照