terraform環境
$ terraform -version
Terraform v1.6.1
on darwin_arm64
+ provider registry.terraform.io/hashicorp/aws v5.34.0
Your version of Terraform is out of date! The latest version
is 1.7.1. You can update by downloading from https://www.terraform.io/downloads.html
network.tf
# ----------------------------------
# Create a VPC
# ----------------------------------
resource "aws_vpc" "vpc" {
cidr_block = "192.168.0.0/20"
instance_tenancy = "default"
enable_dns_support = true
enable_dns_hostnames = true
assign_generated_ipv6_cidr_block = false
tags = {
Name = "${var.project}-${var.environment}-vpc"
Project = var.project
Env = var.environment
}
}
terraform.tfvars
project = "tastylog"
environment = "dev"
terraform plan の結果
╷
│ Warning: Value for undeclared variable
│
│ The root module does not declare a variable named "environment" but a value was found in file "terraform.tfvars". If you meant to use this value, add a "variable" block to the configuration.
│
│ To silence these warnings, use TF_VAR_... environment variables to provide certain "global" settings to all configurations in your organization. To reduce the verbosity of these warnings, use the
│ -compact-warnings option.
╵
╷
│ Warning: Value for undeclared variable
│
│ The root module does not declare a variable named "project" but a value was found in file "terraform.tfvars". If you meant to use this value, add a "variable" block to the configuration.
│
│ To silence these warnings, use TF_VAR_... environment variables to provide certain "global" settings to all configurations in your organization. To reduce the verbosity of these warnings, use the
│ -compact-warnings option.
╵
╷
│ Error: Reference to undeclared input variable
│
│ on network.tf line 12, in resource "aws_vpc" "vpc":
│ 12: Name = "${var.project}-${var.environment}-vpc"
│
│ An input variable with the name "project" has not been declared. This variable can be declared with a variable "project" {} block.
╵
╷
│ Error: Reference to undeclared input variable
│
│ on network.tf line 12, in resource "aws_vpc" "vpc":
│ 12: Name = "${var.project}-${var.environment}-vpc"
│
│ An input variable with the name "environment" has not been declared. This variable can be declared with a variable "environment" {} block.
╵
╷
│ Error: Reference to undeclared input variable
│
│ on network.tf line 13, in resource "aws_vpc" "vpc":
│ 13: Project = var.project
│
│ An input variable with the name "project" has not been declared. This variable can be declared with a variable "project" {} block.
╵
╷
│ Error: Reference to undeclared input variable
│
│ on network.tf line 14, in resource "aws_vpc" "vpc":
│ 14: Env = var.environment
│
│ An input variable with the name "environment" has not been declared. This variable can be declared with a variable "environment" {} block.
network.tfに variable を追記した。
terraform.tfvars に書いてるやん!って思ったけど、こうやって呼び出して上げるお作法がないと動かないみたい。
# ----------------------------------
# Create a VPC
# ----------------------------------
resource "aws_vpc" "vpc" {
cidr_block = "192.168.0.0/20"
instance_tenancy = "default"
enable_dns_support = true
enable_dns_hostnames = true
assign_generated_ipv6_cidr_block = false
tags = {
Name = "${var.project}-${var.environment}-vpc"
Project = var.project
Env = var.environment
}
}
variable "project" {
type = string
}
variable "environment" {
type = string
}